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