Ticket #1743: test_wsgi.py

File test_wsgi.py, 1.8 kB (added by pythy, 3 years ago)

unittest for nevow.wsgi, which failes if bug #1743 not fixed

Line 
1 #!/usr/bin/python
2 import os
3 from cStringIO import StringIO
4 from nevow import rend, wsgi, loaders, tags, testutil
5 from wsgiref.handlers import BaseHandler
6
7 from trace import Trace
8
9 class WSGITestHandler(BaseHandler):
10
11     def _write(self, data):
12         self.transport.write(data)
13
14     def _flush(self):
15         self.transport.flush()
16
17     def get_stdin(self):
18         return self.stdin
19
20     def get_stderr(self):
21         return self.stderr
22
23     def add_cgi_vars(self):
24         pass
25    
26     def handle_error(self):
27         raise
28
29 class WSGITestHandlerWithQuery(WSGITestHandler):
30     def __init__(self, method='GET', path='/', query='', proto='HTTP/1.1'):
31         self.stdin = StringIO(' '.join((method,path,proto)))
32         self.stderr = StringIO()
33         self.transport = StringIO()
34         self.additional_env = {
35             'HTTP_ACCEPT': 'text/xml,text/html,text/plain',
36             'HTTP_HOST': 'localhost:8080',
37             'PATH_INFO': path,
38             'QUERY_STRING': query,
39             'REMOTE_ADDR': '127.0.0.1',
40             'REMOTE_HOST': 'localhost',
41             'REQUEST_METHOD': method,
42             'SERVER_NAME': 'localhost',
43             'SERVER_PORT': '8080',
44             'SERVER_PROTOCOL': proto,
45         }
46         self.os_environ = dict(os.environ.items())
47         self.os_environ.update(self.additional_env)
48
49
50 class NevowIndex(rend.Page):
51     addSlash = True
52     docFactory = loaders.stan(tags.html())
53    
54     def childFactory(self, context, name):
55         return NevowIndex()
56
57 class WSGITestCase(testutil.TestCase):
58     def setUp(self):
59         self.app = wsgi.createWSGIApplication(NevowIndex())
60    
61     def testWSGIData(self):
62         handler = WSGITestHandlerWithQuery()
63         handler.run(self.app)
64    
65     def testWSGIHeader(self):
66         handler = WSGITestHandlerWithQuery(path='/a')
67         handler.run(self.app)
jethro@divmod.org