xref: /unit/test/test_go_application.py (revision 1467:195fe0a92670)
1from unit.applications.lang.go import TestApplicationGo
2
3
4class TestGoApplication(TestApplicationGo):
5    prerequisites = {'modules': {'go': 'all'}}
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        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
104        self.assertEqual(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
117        self.assertEqual(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
130        self.assertEqual(resp['headers']['X-Cookie-1'], 'val1', 'cookie 1')
131        self.assertEqual(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
136        self.assertIn(
137            'error',
138            self.conf(
139                '' "a b c", 'applications/command_line_arguments/arguments'
140            ),
141            'arguments type',
142        )
143
144    def test_go_application_command_line_arguments_0(self):
145        self.load('command_line_arguments')
146
147        self.assertEqual(
148            self.get()['headers']['X-Arg-0'],
149            self.conf_get('applications/command_line_arguments/executable'),
150            'argument 0',
151        )
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
165        self.assertEqual(
166            self.get()['body'], arg1 + ',' + arg2 + ',' + arg3, 'arguments'
167        )
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
176        self.assertEqual(self.get()['body'], '0,a,$,', 'arguments')
177
178        self.conf('["-1", "b", "%"]', args_path)
179
180        self.assertEqual(self.get()['body'], '-1,b,%', 'arguments change')
181
182        self.conf('[]', args_path)
183
184        self.assertEqual(
185            self.get()['headers']['Content-Length'], '0', 'arguments empty'
186        )
187
188
189if __name__ == '__main__':
190    TestGoApplication.main()
191