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