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