test_go_application.py (1467:195fe0a92670) test_go_application.py (1596:b7e2d4d92624)
1from unit.applications.lang.go import TestApplicationGo
1from unit.applications.lang.go import TestApplicationGo
2import re
2
3
4class TestGoApplication(TestApplicationGo):
5 prerequisites = {'modules': {'go': 'all'}}
6
7 def test_go_application_variables(self):
8 self.load('variables')
9

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

14 'Host': 'localhost',
15 'Content-Type': 'text/html',
16 'Custom-Header': 'blah',
17 'Connection': 'close',
18 },
19 body=body,
20 )
21
3
4
5class TestGoApplication(TestApplicationGo):
6 prerequisites = {'modules': {'go': 'all'}}
7
8 def test_go_application_variables(self):
9 self.load('variables')
10

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

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