test_python_application.py (550:ce9e85f49c02) test_python_application.py (552:2e4ea9cc507e)
1import time
2import unittest
3import unit
4
1import time
2import unittest
3import unit
4
5class TestUnitPythonApplication(unit.TestUnitControl):
5class TestUnitPythonApplication(unit.TestUnitApplicationPython):
6
7 def setUpClass():
8 unit.TestUnit().check_modules('python')
9
6
7 def setUpClass():
8 unit.TestUnit().check_modules('python')
9
10 def conf_with_name(self, name):
11 self.conf({
12 "listeners": {
13 "*:7080": {
14 "application": "app"
15 }
16 },
17 "applications": {
18 "app": {
19 "type": "python",
20 "processes": { "spare": 0 },
21 "path": self.testdir + '/' + name,
22 "module": "wsgi"
23 }
24 }
25 })
10 def test_python_application_variables(self):
11 self.load('variables')
26
12
27 def test_python_application_simple(self):
28 code, name = """
29
30def application(environ, start_response):
31
32 content_length = int(environ.get('CONTENT_LENGTH', 0))
33 body = bytes(environ['wsgi.input'].read(content_length))
34
35 start_response('200', [
36 ('Content-Type', environ.get('CONTENT_TYPE')),
37 ('Content-Length', str(len(body))),
38 ('Request-Method', environ.get('REQUEST_METHOD')),
39 ('Request-Uri', environ.get('REQUEST_URI')),
40 ('Http-Host', environ.get('HTTP_HOST')),
41 ('Server-Protocol', environ.get('SERVER_PROTOCOL')),
42 ('Custom-Header', environ.get('HTTP_CUSTOM_HEADER'))
43 ])
44 return [body]
45
46""", 'py_app'
47
48 self.python_application(name, code)
49 self.conf_with_name(name)
50
51 body = 'Test body string.'
52
53 resp = self.post(headers={
54 'Host': 'localhost',
55 'Content-Type': 'text/html',
56 'Custom-Header': 'blah'
57 }, body=body)
58

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

70 'Request-Uri': '/',
71 'Http-Host': 'localhost',
72 'Server-Protocol': 'HTTP/1.1',
73 'Custom-Header': 'blah'
74 }, 'headers')
75 self.assertEqual(resp['body'], body, 'body')
76
77 def test_python_application_query_string(self):
13 body = 'Test body string.'
14
15 resp = self.post(headers={
16 'Host': 'localhost',
17 'Content-Type': 'text/html',
18 'Custom-Header': 'blah'
19 }, body=body)
20

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

32 'Request-Uri': '/',
33 'Http-Host': 'localhost',
34 'Server-Protocol': 'HTTP/1.1',
35 'Custom-Header': 'blah'
36 }, 'headers')
37 self.assertEqual(resp['body'], body, 'body')
38
39 def test_python_application_query_string(self):
78 code, name = """
40 self.load('query_string')
79
41
80def application(environ, start_response):
81
82 start_response('200', [
83 ('Content-Length', '0'),
84 ('Query-String', environ.get('QUERY_STRING'))
85 ])
86 return []
87
88""", 'py_app'
89
90 self.python_application(name, code)
91 self.conf_with_name(name)
92
93 resp = self.get(url='/?var1=val1&var2=val2')
94
95 self.assertEqual(resp['headers']['Query-String'], 'var1=val1&var2=val2',
96 'Query-String header')
97
98 @unittest.expectedFailure
99 def test_python_application_server_port(self):
42 resp = self.get(url='/?var1=val1&var2=val2')
43
44 self.assertEqual(resp['headers']['Query-String'], 'var1=val1&var2=val2',
45 'Query-String header')
46
47 @unittest.expectedFailure
48 def test_python_application_server_port(self):
100 code, name = """
49 self.load('server_port')
101
50
102def application(environ, start_response):
103
104 start_response('200', [
105 ('Content-Length', '0'),
106 ('Server-Port', environ.get('SERVER_PORT'))
107 ])
108 return []
109
110""", 'py_app'
111
112 self.python_application(name, code)
113 self.conf_with_name(name)
114
115 self.assertEqual(self.get()['headers']['Server-Port'], '7080',
116 'Server-Port header')
117
118 @unittest.expectedFailure
119 def test_python_application_204_transfer_encoding(self):
51 self.assertEqual(self.get()['headers']['Server-Port'], '7080',
52 'Server-Port header')
53
54 @unittest.expectedFailure
55 def test_python_application_204_transfer_encoding(self):
120 code, name = """
56 self.load('204_no_content')
121
57
122def application(environ, start_response):
123
124 start_response('204 No Content', [])
125 return []
126
127""", 'py_app'
128
129 self.python_application(name, code)
130 self.conf_with_name(name)
131
132 self.assertNotIn('Transfer-Encoding', self.get()['headers'],
133 '204 header transfer encoding')
134
135if __name__ == '__main__':
136 unittest.main()
58 self.assertNotIn('Transfer-Encoding', self.get()['headers'],
59 '204 header transfer encoding')
60
61if __name__ == '__main__':
62 unittest.main()