xref: /unit/test/test_go_application.py (revision 1596:b7e2d4d92624)
1from unit.applications.lang.go import TestApplicationGo
2import re
3
4
5class TestGoApplication(TestApplicationGo):
6    prerequisites = {'modules': {'go': 'all'}}
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        assert resp['status'] == 200, 'status'
24        headers = resp['headers']
25        header_server = headers.pop('Server')
26        assert re.search(r'Unit/[\d\.]+', header_server), 'server header'
27
28        date = headers.pop('Date')
29        assert date[-4:] == ' GMT', 'date header timezone'
30        assert (
31            abs(self.date_to_sec_epoch(date) - self.sec_epoch()) < 5
32        ), 'date header'
33
34        assert headers == {
35            'Content-Length': str(len(body)),
36            'Content-Type': 'text/html',
37            'Request-Method': 'POST',
38            'Request-Uri': '/',
39            'Http-Host': 'localhost',
40            'Server-Protocol': 'HTTP/1.1',
41            'Server-Protocol-Major': '1',
42            'Server-Protocol-Minor': '1',
43            'Custom-Header': 'blah',
44            'Connection': 'close',
45        }, 'headers'
46        assert 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        assert resp['headers']['X-Var-1'] == 'val1', 'GET variables'
53        assert resp['headers']['X-Var-2'] == '', 'GET variables 2'
54        assert 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(
60            headers={
61                'Host': 'localhost',
62                'Content-Type': 'application/x-www-form-urlencoded',
63                'Connection': 'close',
64            },
65            body='var1=val1&var2=&var3',
66        )
67
68        assert resp['headers']['X-Var-1'] == 'val1', 'POST variables'
69        assert resp['headers']['X-Var-2'] == '', 'POST variables 2'
70        assert resp['headers']['X-Var-3'] == '', 'POST variables 3'
71
72    def test_go_application_404(self):
73        self.load('404')
74
75        resp = self.get()
76
77        assert resp['status'] == 404, '404 status'
78        assert re.search(
79            r'<title>404 Not Found</title>', resp['body']
80        ), '404 body'
81
82    def test_go_keepalive_body(self):
83        self.load('mirror')
84
85        assert self.get()['status'] == 200, 'init'
86
87        body = '0123456789' * 500
88        (resp, sock) = self.post(
89            headers={
90                'Host': 'localhost',
91                'Connection': 'keep-alive',
92                'Content-Type': 'text/html',
93            },
94            start=True,
95            body=body,
96            read_timeout=1,
97        )
98
99        assert resp['body'] == body, 'keep-alive 1'
100
101        body = '0123456789'
102        resp = self.post(
103            headers={
104                'Host': 'localhost',
105                'Content-Type': 'text/html',
106                'Connection': 'close',
107            },
108            sock=sock,
109            body=body,
110        )
111
112        assert resp['body'] == body, 'keep-alive 2'
113
114    def test_go_application_cookies(self):
115        self.load('cookies')
116
117        resp = self.get(
118            headers={
119                'Host': 'localhost',
120                'Cookie': 'var1=val1; var2=val2',
121                'Connection': 'close',
122            }
123        )
124
125        assert resp['headers']['X-Cookie-1'] == 'val1', 'cookie 1'
126        assert resp['headers']['X-Cookie-2'] == 'val2', 'cookie 2'
127
128    def test_go_application_command_line_arguments_type(self):
129        self.load('command_line_arguments')
130
131        assert 'error' in \
132            self.conf(
133                '' "a b c", 'applications/command_line_arguments/arguments'
134            ), \
135            'arguments type'
136
137    def test_go_application_command_line_arguments_0(self):
138        self.load('command_line_arguments')
139
140        assert self.get()['headers']['X-Arg-0'] == self.conf_get(
141            'applications/command_line_arguments/executable'
142        ), 'argument 0'
143
144    def test_go_application_command_line_arguments(self):
145        self.load('command_line_arguments')
146
147        arg1 = '--cc=gcc-7.2.0'
148        arg2 = '--cc-opt=\'-O0 -DNXT_DEBUG_MEMORY=1 -fsanitize=address\''
149        arg3 = '--debug'
150
151        self.conf(
152            '["' + arg1 + '", "' + arg2 + '", "' + arg3 + '"]',
153            'applications/command_line_arguments/arguments',
154        )
155
156        assert (
157            self.get()['body'] == arg1 + ',' + arg2 + ',' + arg3
158        ), 'arguments'
159
160    def test_go_application_command_line_arguments_change(self):
161        self.load('command_line_arguments')
162
163        args_path = 'applications/command_line_arguments/arguments'
164
165        self.conf('["0", "a", "$", ""]', args_path)
166
167        assert self.get()['body'] == '0,a,$,', 'arguments'
168
169        self.conf('["-1", "b", "%"]', args_path)
170
171        assert self.get()['body'] == '-1,b,%', 'arguments change'
172
173        self.conf('[]', args_path)
174
175        assert (
176            self.get()['headers']['Content-Length'] == '0'
177        ), 'arguments empty'
178