test_go_application.py (970:2f4376c8f358) test_go_application.py (1017:887a4bbabf1e)
1import unittest
2import unit
3
1import unittest
2import unit
3
4class TestUnitGoApplication(unit.TestUnitApplicationGo):
5
4
5class TestUnitGoApplication(unit.TestUnitApplicationGo):
6 def setUpClass():
7 unit.TestUnit().check_modules('go')
8
9 def test_go_application_variables(self):
10 self.load('variables')
11
12 body = 'Test body string.'
13
6 def setUpClass():
7 unit.TestUnit().check_modules('go')
8
9 def test_go_application_variables(self):
10 self.load('variables')
11
12 body = 'Test body string.'
13
14 resp = self.post(headers={
15 'Host': 'localhost',
16 'Content-Type': 'text/html',
17 'Custom-Header': 'blah',
18 'Connection': 'close'
19 }, body=body)
14 resp = self.post(
15 headers={
16 'Host': 'localhost',
17 'Content-Type': 'text/html',
18 'Custom-Header': 'blah',
19 'Connection': 'close',
20 },
21 body=body,
22 )
20
21 self.assertEqual(resp['status'], 200, 'status')
22 headers = resp['headers']
23 header_server = headers.pop('Server')
24 self.assertRegex(header_server, r'Unit/[\d\.]+', 'server header')
25
26 date = headers.pop('Date')
27 self.assertEqual(date[-4:], ' GMT', 'date header timezone')
23
24 self.assertEqual(resp['status'], 200, 'status')
25 headers = resp['headers']
26 header_server = headers.pop('Server')
27 self.assertRegex(header_server, r'Unit/[\d\.]+', 'server header')
28
29 date = headers.pop('Date')
30 self.assertEqual(date[-4:], ' GMT', 'date header timezone')
28 self.assertLess(abs(self.date_to_sec_epoch(date) - self.sec_epoch()), 5,
29 'date header')
31 self.assertLess(
32 abs(self.date_to_sec_epoch(date) - self.sec_epoch()),
33 5,
34 'date header',
35 )
30
36
31 self.assertDictEqual(headers, {
32 'Content-Length': str(len(body)),
33 'Content-Type': 'text/html',
34 'Request-Method': 'POST',
35 'Request-Uri': '/',
36 'Http-Host': 'localhost',
37 'Server-Protocol': 'HTTP/1.1',
38 'Server-Protocol-Major': '1',
39 'Server-Protocol-Minor': '1',
40 'Custom-Header': 'blah',
41 'Connection': 'close'
42 }, 'headers')
37 self.assertDictEqual(
38 headers,
39 {
40 'Content-Length': str(len(body)),
41 'Content-Type': 'text/html',
42 'Request-Method': 'POST',
43 'Request-Uri': '/',
44 'Http-Host': 'localhost',
45 'Server-Protocol': 'HTTP/1.1',
46 'Server-Protocol-Major': '1',
47 'Server-Protocol-Minor': '1',
48 'Custom-Header': 'blah',
49 'Connection': 'close',
50 },
51 'headers',
52 )
43 self.assertEqual(resp['body'], body, 'body')
44
45 def test_go_application_get_variables(self):
46 self.load('get_variables')
47
48 resp = self.get(url='/?var1=val1&var2=&var3')
49 self.assertEqual(resp['headers']['X-Var-1'], 'val1', 'GET variables')
50 self.assertEqual(resp['headers']['X-Var-2'], '', 'GET variables 2')
51 self.assertEqual(resp['headers']['X-Var-3'], '', 'GET variables 3')
52
53 def test_go_application_post_variables(self):
54 self.load('post_variables')
55
53 self.assertEqual(resp['body'], body, 'body')
54
55 def test_go_application_get_variables(self):
56 self.load('get_variables')
57
58 resp = self.get(url='/?var1=val1&var2=&var3')
59 self.assertEqual(resp['headers']['X-Var-1'], 'val1', 'GET variables')
60 self.assertEqual(resp['headers']['X-Var-2'], '', 'GET variables 2')
61 self.assertEqual(resp['headers']['X-Var-3'], '', 'GET variables 3')
62
63 def test_go_application_post_variables(self):
64 self.load('post_variables')
65
56 resp = self.post(headers={
57 'Host': 'localhost',
58 'Content-Type': 'application/x-www-form-urlencoded',
59 'Connection': 'close'
60 }, body='var1=val1&var2=&var3')
66 resp = self.post(
67 headers={
68 'Host': 'localhost',
69 'Content-Type': 'application/x-www-form-urlencoded',
70 'Connection': 'close',
71 },
72 body='var1=val1&var2=&var3',
73 )
61
62 self.assertEqual(resp['headers']['X-Var-1'], 'val1', 'POST variables')
63 self.assertEqual(resp['headers']['X-Var-2'], '', 'POST variables 2')
64 self.assertEqual(resp['headers']['X-Var-3'], '', 'POST variables 3')
65
66 def test_go_application_404(self):
67 self.load('404')
68
69 resp = self.get()
70
71 self.assertEqual(resp['status'], 404, '404 status')
74
75 self.assertEqual(resp['headers']['X-Var-1'], 'val1', 'POST variables')
76 self.assertEqual(resp['headers']['X-Var-2'], '', 'POST variables 2')
77 self.assertEqual(resp['headers']['X-Var-3'], '', 'POST variables 3')
78
79 def test_go_application_404(self):
80 self.load('404')
81
82 resp = self.get()
83
84 self.assertEqual(resp['status'], 404, '404 status')
72 self.assertRegex(resp['body'], r'<title>404 Not Found</title>',
73 '404 body')
85 self.assertRegex(
86 resp['body'], r'<title>404 Not Found</title>', '404 body'
87 )
74
75 def test_go_keepalive_body(self):
76 self.load('mirror')
77
88
89 def test_go_keepalive_body(self):
90 self.load('mirror')
91
78 (resp, sock) = self.post(headers={
79 'Host': 'localhost',
80 'Connection': 'keep-alive',
81 'Content-Type': 'text/html'
82 }, start=True, body='0123456789' * 500)
92 (resp, sock) = self.post(
93 headers={
94 'Host': 'localhost',
95 'Connection': 'keep-alive',
96 'Content-Type': 'text/html',
97 },
98 start=True,
99 body='0123456789' * 500,
100 )
83
84 self.assertEqual(resp['body'], '0123456789' * 500, 'keep-alive 1')
85
101
102 self.assertEqual(resp['body'], '0123456789' * 500, 'keep-alive 1')
103
86 resp = self.post(headers={
87 'Host': 'localhost',
88 'Content-Type': 'text/html',
89 'Connection': 'close'
90 }, sock=sock, body='0123456789')
104 resp = self.post(
105 headers={
106 'Host': 'localhost',
107 'Content-Type': 'text/html',
108 'Connection': 'close',
109 },
110 sock=sock,
111 body='0123456789',
112 )
91
92 self.assertEqual(resp['body'], '0123456789', 'keep-alive 2')
93
94 def test_go_application_cookies(self):
95 self.load('cookies')
96
113
114 self.assertEqual(resp['body'], '0123456789', 'keep-alive 2')
115
116 def test_go_application_cookies(self):
117 self.load('cookies')
118
97 resp = self.get(headers={
98 'Host': 'localhost',
99 'Cookie': 'var1=val1; var2=val2',
100 'Connection': 'close'
101 })
119 resp = self.get(
120 headers={
121 'Host': 'localhost',
122 'Cookie': 'var1=val1; var2=val2',
123 'Connection': 'close',
124 }
125 )
102
103 self.assertEqual(resp['headers']['X-Cookie-1'], 'val1', 'cookie 1')
104 self.assertEqual(resp['headers']['X-Cookie-2'], 'val2', 'cookie 2')
105
106 def test_go_application_command_line_arguments_type(self):
107 self.load('command_line_arguments')
108
126
127 self.assertEqual(resp['headers']['X-Cookie-1'], 'val1', 'cookie 1')
128 self.assertEqual(resp['headers']['X-Cookie-2'], 'val2', 'cookie 2')
129
130 def test_go_application_command_line_arguments_type(self):
131 self.load('command_line_arguments')
132
109 self.assertIn('error', self.conf(''"a b c",
110 'applications/command_line_arguments/arguments'), 'arguments type')
133 self.assertIn(
134 'error',
135 self.conf(
136 '' "a b c", 'applications/command_line_arguments/arguments'
137 ),
138 'arguments type',
139 )
111
112 def test_go_application_command_line_arguments_0(self):
113 self.load('command_line_arguments')
114
140
141 def test_go_application_command_line_arguments_0(self):
142 self.load('command_line_arguments')
143
115 self.assertEqual(self.get()['headers']['X-Arg-0'],
144 self.assertEqual(
145 self.get()['headers']['X-Arg-0'],
116 self.conf_get('applications/command_line_arguments/executable'),
146 self.conf_get('applications/command_line_arguments/executable'),
117 'argument 0')
147 'argument 0',
148 )
118
119 def test_go_application_command_line_arguments(self):
120 self.load('command_line_arguments')
121
122 arg1 = '--cc=gcc-7.2.0'
123 arg2 = '--cc-opt=\'-O0 -DNXT_DEBUG_MEMORY=1 -fsanitize=address\''
124 arg3 = '--debug'
125
149
150 def test_go_application_command_line_arguments(self):
151 self.load('command_line_arguments')
152
153 arg1 = '--cc=gcc-7.2.0'
154 arg2 = '--cc-opt=\'-O0 -DNXT_DEBUG_MEMORY=1 -fsanitize=address\''
155 arg3 = '--debug'
156
126 self.conf('["' + arg1 + '", "' + arg2 + '", "' + arg3 + '"]',
127 'applications/command_line_arguments/arguments')
157 self.conf(
158 '["' + arg1 + '", "' + arg2 + '", "' + arg3 + '"]',
159 'applications/command_line_arguments/arguments',
160 )
128
161
129 self.assertEqual(self.get()['body'], arg1 + ',' + arg2 + ',' + arg3,
130 'arguments')
162 self.assertEqual(
163 self.get()['body'], arg1 + ',' + arg2 + ',' + arg3, 'arguments'
164 )
131
132 def test_go_application_command_line_arguments_change(self):
133 self.load('command_line_arguments')
134
135 args_path = 'applications/command_line_arguments/arguments'
136
137 self.conf('["0", "a", "$", ""]', args_path)
138
139 self.assertEqual(self.get()['body'], '0,a,$,', 'arguments')
140
141 self.conf('["-1", "b", "%"]', args_path)
142
143 self.assertEqual(self.get()['body'], '-1,b,%', 'arguments change')
144
145 self.conf('[]', args_path)
146
165
166 def test_go_application_command_line_arguments_change(self):
167 self.load('command_line_arguments')
168
169 args_path = 'applications/command_line_arguments/arguments'
170
171 self.conf('["0", "a", "$", ""]', args_path)
172
173 self.assertEqual(self.get()['body'], '0,a,$,', 'arguments')
174
175 self.conf('["-1", "b", "%"]', args_path)
176
177 self.assertEqual(self.get()['body'], '-1,b,%', 'arguments change')
178
179 self.conf('[]', args_path)
180
147 self.assertEqual(self.get()['headers']['Content-Length'], '0',
148 'arguments empty')
181 self.assertEqual(
182 self.get()['headers']['Content-Length'], '0', 'arguments empty'
183 )
149
184
185
150if __name__ == '__main__':
151 TestUnitGoApplication.main()
186if __name__ == '__main__':
187 TestUnitGoApplication.main()