xref: /unit/test/test_go_application.py (revision 1025:3469f5185562)
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        (resp, sock) = self.post(
92            headers={
93                'Host': 'localhost',
94                'Connection': 'keep-alive',
95                'Content-Type': 'text/html',
96            },
97            start=True,
98            body='0123456789' * 500,
99        )
100
101        self.assertEqual(resp['body'], '0123456789' * 500, 'keep-alive 1')
102
103        resp = self.post(
104            headers={
105                'Host': 'localhost',
106                'Content-Type': 'text/html',
107                'Connection': 'close',
108            },
109            sock=sock,
110            body='0123456789',
111        )
112
113        self.assertEqual(resp['body'], '0123456789', 'keep-alive 2')
114
115    def test_go_application_cookies(self):
116        self.load('cookies')
117
118        resp = self.get(
119            headers={
120                'Host': 'localhost',
121                'Cookie': 'var1=val1; var2=val2',
122                'Connection': 'close',
123            }
124        )
125
126        self.assertEqual(resp['headers']['X-Cookie-1'], 'val1', 'cookie 1')
127        self.assertEqual(resp['headers']['X-Cookie-2'], 'val2', 'cookie 2')
128
129    def test_go_application_command_line_arguments_type(self):
130        self.load('command_line_arguments')
131
132        self.assertIn(
133            'error',
134            self.conf(
135                '' "a b c", 'applications/command_line_arguments/arguments'
136            ),
137            'arguments type',
138        )
139
140    def test_go_application_command_line_arguments_0(self):
141        self.load('command_line_arguments')
142
143        self.assertEqual(
144            self.get()['headers']['X-Arg-0'],
145            self.conf_get('applications/command_line_arguments/executable'),
146            'argument 0',
147        )
148
149    def test_go_application_command_line_arguments(self):
150        self.load('command_line_arguments')
151
152        arg1 = '--cc=gcc-7.2.0'
153        arg2 = '--cc-opt=\'-O0 -DNXT_DEBUG_MEMORY=1 -fsanitize=address\''
154        arg3 = '--debug'
155
156        self.conf(
157            '["' + arg1 + '", "' + arg2 + '", "' + arg3 + '"]',
158            'applications/command_line_arguments/arguments',
159        )
160
161        self.assertEqual(
162            self.get()['body'], arg1 + ',' + arg2 + ',' + arg3, 'arguments'
163        )
164
165    def test_go_application_command_line_arguments_change(self):
166        self.load('command_line_arguments')
167
168        args_path = 'applications/command_line_arguments/arguments'
169
170        self.conf('["0", "a", "$", ""]', args_path)
171
172        self.assertEqual(self.get()['body'], '0,a,$,', 'arguments')
173
174        self.conf('["-1", "b", "%"]', args_path)
175
176        self.assertEqual(self.get()['body'], '-1,b,%', 'arguments change')
177
178        self.conf('[]', args_path)
179
180        self.assertEqual(
181            self.get()['headers']['Content-Length'], '0', 'arguments empty'
182        )
183
184
185if __name__ == '__main__':
186    TestGoApplication.main()
187