Ticket #1743: test_wsgi-2.py

File test_wsgi-2.py, 3.2 kB (added by pythy, 3 years ago)

Unit-test for ticket #1743, without wsgiref dependence

Line 
1 #!/usr/bin/python
2 import sys
3 import os
4 from cStringIO import StringIO
5 from nevow import wsgi, rend, loaders, stan, tags, testutil
6 # deprecated, but uses in wsgiref, so we test it too
7 from types import StringType
8
9 class NevowIndex(rend.Page):
10     addSlash = True
11     docFactory = loaders.stan(tags.html())
12    
13     def childFactory(self, context, name):
14         return NevowIndex()
15
16 def test_write(data):
17     assert isinstance(data, str), "Data must bet string instead of %r" % type(data)
18     # deprecated, but uses in wsgiref, so we test it too
19     assert type(data) is StringType, "Check str type of data in old-class style failed; it is %r type, not str" % type(data)
20
21 def test_response(status, headers, exc_info=None):
22     assert isinstance(status, str), "Status must be string instead of %r" % type(status)
23     # deprecated, but uses in wsgiref, so we test it too
24     assert type(status) is StringType, "Check str type of status in old-class style failed; it is %r type, not str" % type(status)
25     #
26     assert len(status) >= 4, "Status must be at least 4 characters"
27     assert int(status[:3]), "Status message mus begin with 3-digit code"
28     assert status[3]==" ", "Status message must have a space after code"
29     for name, val in headers:
30         assert isinstance(name, str), "Header names must be strings instead of %r" % type(name)
31         assert isinstance(val, str), "Header values must be string instead of %r" % type(val)
32         # deprecated, but uses in wsgiref, so we test it too
33         assert type(name) is StringType, "Check str type of header's name in old-class style failed; it is %r type, not str" % type(name)
34         assert type(val) is StringType, "Check str type of header's val in old-class style failed; it is %r type, not str" % type(val)
35        
36     return test_write
37
38 def get_wsgi_env(method='GET', path='/', query='', proto='HTTP/1.1'):
39     custom_env = {
40             'HTTP_ACCEPT': 'text/xml,text/html,text/plain',
41             'HTTP_HOST': 'localhost:8080',
42             'PATH_INFO': path,
43             'QUERY_STRING': query,
44             'REMOTE_ADDR': '127.0.0.1',
45             'REMOTE_HOST': 'localhost',
46             'REQUEST_METHOD': method,
47             'SERVER_NAME': 'localhost',
48             'SERVER_PORT': '8080',
49             'SERVER_PROTOCOL': proto,
50             'SERVER_SOFTWARE': 'Nevow WSGI test',
51             'wsgi.input': StringIO(' '.join((method,path,proto))),
52             'wsgi.errors': sys.stderr,
53             'wsgi.version': (1,0),
54             'wsgi.run_once': False,
55             'wsgi.url_scheme': 'http',
56             'wsgi.multithread': True,
57             'wsgi.multiprocess': True,
58            }
59     custom_env.update(os.environ.copy())
60     return custom_env
61
62 class WSGITestCase(testutil.TestCase):
63    
64     def setUp(self):
65         self.app = wsgi.createWSGIApplication(NevowIndex())
66    
67     def testWSGIData(self):
68         result = self.app(get_wsgi_env(), test_response)
69         for data in result:
70             test_write(data)
71         if hasattr(result, 'close'):
72             result.close()
73    
74     def testWSGIHeader(self):
75         result = self.app(get_wsgi_env(path='/a'), test_response)
76         for data in result:
77             test_write(data)
78         if hasattr(result, 'close'):
79             result.close()
jethro@divmod.org