aboutsummaryrefslogtreecommitdiff
path: root/test/appengine/__init__.py
blob: 917544d90b890a916ce9790bd64d30debf4b3d7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
import sys
import unittest
from nose.plugins.skip import SkipTest


def activate_sandbox():
    """
    Enables parts of the GAE sandbox that are relevant.

    Inserts the stub module import hook which causes the usage of appengine-specific
    httplib, httplib2, socket, etc.
    """
    from google.appengine.tools.devappserver2.python import sandbox

    for name in list(sys.modules):
        if name in sandbox.dist27.MODULE_OVERRIDES:
            del sys.modules[name]
    sys.meta_path.insert(0, sandbox.StubModuleImportHook())
    sys.path_importer_cache = {}


def deactivate_sandbox():
    from google.appengine.tools.devappserver2.python import sandbox

    sys.meta_path = [
        x for x in sys.meta_path if not isinstance(x, sandbox.StubModuleImportHook)]
    sys.path_importer_cache = {}

    # Delete any instances of sandboxed modules.
    for name in list(sys.modules):
        if name in sandbox.dist27.MODULE_OVERRIDES:
            del sys.modules[name]


class AppEngineSandboxTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):

        if sys.version_info[:2] != (2, 7):
            raise SkipTest("App Engine only tests on py2.7")

        if 'APPLICATION_ID' not in os.environ:
            raise SkipTest("NoseGAE plugin not used.")

        try:
            activate_sandbox()
        except ImportError:
            raise SkipTest("App Engine SDK not available.")

    @classmethod
    def tearDownClass(self):
        try:
            deactivate_sandbox()
        except ImportError:
            pass


class MockResponse(object):
    def __init__(self, content, status_code, content_was_truncated, final_url, headers):
        import httplib
        from StringIO import StringIO

        self.content = content
        self.status_code = status_code
        self.content_was_truncated = content_was_truncated
        self.final_url = final_url
        self.header_msg = httplib.HTTPMessage(StringIO(''.join(
            ["%s: %s\n" % (k, v) for k, v in headers.iteritems()] + ["\n"])))
        self.headers = self.header_msg.items()