summaryrefslogtreecommitdiff
path: root/repoze/bfg/tests/test_wsgi.py
blob: 4c36850b6ca8a16dea93b48143db9515affa7823 (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
import unittest

class WSGIAppTests(unittest.TestCase):
    def test_decorator(self):
        body = 'Unauthorized'
        headerlist = [ ('Content-Type', 'text/plain'),
                       ('Content-Length', len(body)) ]
        status = '401 Unauthorized'
        def real_wsgiapp(environ, start_response):
            start_response(status, headerlist)
            return [body]
        from repoze.bfg.wsgi import wsgiapp
        wrapped = wsgiapp(real_wsgiapp)
        context = DummyContext()
        request = DummyRequest({})
        response = wrapped(context, request)
        self.assertEqual(response.status, status)
        self.assertEqual(response.headerlist, headerlist)
        self.assertEqual(response.app_iter, [body])

    def test_decorator_startresponse_uncalled(self):
        body = 'Unauthorized'
        headerlist = [ ('Content-Type', 'text/plain'),
                       ('Content-Length', len(body)) ]
        status = '401 Unauthorized'
        def real_wsgiapp(environ, start_response):
            return [body]
        from repoze.bfg.wsgi import wsgiapp
        wrapped = wsgiapp(real_wsgiapp)
        context = DummyContext()
        request = DummyRequest({})
        self.assertRaises(RuntimeError, wrapped, context, request)

class DummyContext:
    pass

class DummyRequest:
    def __init__(self, environ):
        self.environ = environ