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