xref: /unit/test/test_go_application.py (revision 1029:687f7cc7aae2)
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()
190