summaryrefslogtreecommitdiff
path: root/repoze/bfg/tests/test_wsgiadapter.py
blob: 71e2f5c81c58df9c3c41270d38ee11e674f2d816 (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
72
73
74
75
import unittest

class NaiveWSGIAdapterTests(unittest.TestCase):
    def _getTargetClass(self):
        from repoze.bfg.wsgiadapter import NaiveWSGIViewAdapter
        return NaiveWSGIViewAdapter

    def _makeOne(self, *arg, **kw):
        klass = self._getTargetClass()
        return klass(*arg, **kw)

    def test_view_takes_no_args(self):
        response = DummyResponse()
        response.app_iter = ['Hello world']
        def view():
            return response
        request = DummyRequest()
        adapter = self._makeOne(view, request)
        environ = {}
        start_response = DummyStartResponse()
        result = adapter(environ, start_response)
        self.assertEqual(result, ['Hello world'])
        self.assertEqual(start_response.headers, ())
        self.assertEqual(start_response.status, '200 OK')

    def test_view_takes_pep_333_args(self):
        response = DummyResponse()
        response.app_iter = ['Hello world']
        def view(environ, start_response):
            response.environ = environ
            response.start_response = start_response
            return response
        request = DummyRequest()
        adapter = self._makeOne(view, request)
        environ = {}
        start_response = DummyStartResponse()
        result = adapter(environ, start_response)
        self.assertEqual(result, ['Hello world'])
        self.assertEqual(start_response.headers, ())
        self.assertEqual(start_response.status, '200 OK')
        self.assertEqual(response.environ, environ)
        self.assertEqual(response.start_response, start_response)

    def test_view_takes_zopey_args(self):
        request = DummyRequest()
        response = DummyResponse()
        response.app_iter = ['Hello world']
        def view(request):
            response.request = request
            return response
        adapter = self._makeOne(view, request)
        environ = {}
        start_response = DummyStartResponse()
        result = adapter(environ, start_response)
        self.assertEqual(result, ['Hello world'])
        self.assertEqual(start_response.headers, ())
        self.assertEqual(start_response.status, '200 OK')
        self.assertEqual(response.request, request)

class DummyRequest:
    pass

class DummyResponse:
    status = '200 OK'
    headerlist = ()
    app_iter = ()
    
class DummyStartResponse:
    status = None
    headers = None
    def __call__(self, status, headers):
        self.status = status
        self.headers = headers