xref: /unit/test/test_go_application.py (revision 1165:998b521bbdb8)
1from unit.applications.lang.go import TestApplicationGo
2
3
4class TestGoApplication(TestApplicationGo):
5    prerequisites = {'modules': ['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()
189