1import unittest
2import unit
3
4class TestUnitApplication(unit.TestUnitControl):
5
6 def setUpClass():
7 u = unit.TestUnit()
8
9 u.check_modules('python')
10 u.check_version('0.4')
11
12 conf = """
13 {
12 def conf_with_name(self, name):
13 self.conf({
14 "listeners": {
15 "*:7080": {
16 "application": "app"
17 }
18 },
19 "applications": {
20 "app": {
21 "type": "python",
22 "workers": 1,
23 "path": "%s",
23 "path": self.testdir + '/' + name,
24 "module": "wsgi"
25 }
26 }
27 }
28 """
27 })
28
29 def test_python_application_simple(self):
30 code, name = """
31
32def application(environ, start_response):
33
34 content_length = int(environ.get('CONTENT_LENGTH', 0))
35 body = bytes(environ['wsgi.input'].read(content_length))

--- 7 unchanged lines hidden (view full) ---

43 ('Server-Protocol', environ.get('SERVER_PROTOCOL')),
44 ('Custom-Header', environ.get('HTTP_CUSTOM_HEADER'))
45 ])
46 return [body]
47
48""", 'py_app'
49
50 self.python_application(name, code)
52 self.put('/', self.conf % (self.testdir + '/' + name))
51 self.conf_with_name(name)
52
53 body = 'Test body string.'
54
55 r = unit.TestUnitHTTP.post(headers={
56 'Host': 'localhost',
57 'Content-Type': 'text/html',
58 'Custom-Header': 'blah'
59 }, data=body)

--- 22 unchanged lines hidden (view full) ---

82 ('Content-Length', '0'),
83 ('Query-String', environ.get('QUERY_STRING'))
84 ])
85 return []
86
87""", 'py_app'
88
89 self.python_application(name, code)
91 self.put('/', self.conf % (self.testdir + '/' + name))
90 self.conf_with_name(name)
91
92 r = unit.TestUnitHTTP.get(uri='/?var1=val1&var2=val2', headers={
93 'Host': 'localhost'
94 })
95
96 self.assertEqual(r.status_code, 200, 'status')
97 headers = dict(r.headers)
98 headers.pop('Server')

--- 12 unchanged lines hidden (view full) ---

111 ('Content-Length', '0'),
112 ('Server-Port', environ.get('SERVER_PORT'))
113 ])
114 return []
115
116""", 'py_app'
117
118 self.python_application(name, code)
120 self.put('/', self.conf % (self.testdir + '/' + name))
119 self.conf_with_name(name)
120
121 r = unit.TestUnitHTTP.get(headers={'Host': 'localhost'})
122
123 self.assertEqual(r.headers.pop('Server-Port'), '7080',
124 'Server-Port header')
125
126 @unittest.expectedFailure
127 def test_python_application_204_transfer_encoding(self):
128 code, name = """
129
130def application(environ, start_response):
131
132 start_response('204 No Content', [])
133 return []
134
135""", 'py_app'
136
137 self.python_application(name, code)
139 self.put('/', self.conf % (self.testdir + '/' + name))
138 self.conf_with_name(name)
139
140 r = unit.TestUnitHTTP.get(headers={'Host': 'localhost'})
141 self.assertNotIn('Transfer-Encoding', r.headers,
142 '204 header transfer encoding')
143
144if __name__ == '__main__':
145 unittest.main()