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