summaryrefslogtreecommitdiff
path: root/repoze/bfg/tests/test_wsgi.py
blob: ac02ec49f25464cad95f9bac2859463f42554b55 (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
from zope.testing.cleanup import cleanUp

class WSGIAppTests(unittest.TestCase):
    def setUp(self):
        cleanUp()

    def tearDown(self):
        cleanUp()
        
    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_alternate_iresponsefactory(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({})
        from repoze.bfg.interfaces import IResponseFactory
        from zope.component import getGlobalSiteManager
        from webob import Response
        class Response2(Response):
            pass
        gsm = getGlobalSiteManager()
        gsm.registerUtility(Response2, IResponseFactory)
        response = wrapped(context, request)
        self.failUnless(isinstance(response, Response2))

    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