xref: /unit/test/test_go_application.py (revision 1999:00d43b03d82f)
1import re
2
3import pytest
4
5from unit.applications.lang.go import TestApplicationGo
6
7
8class TestGoApplication(TestApplicationGo):
9    prerequisites = {'modules': {'go': 'all'}}
10
11    @pytest.fixture(autouse=True)
12    def setup_method_fixture(self, request, skip_alert):
13        skip_alert(r'\[unit\] close\(\d+\) failed: Bad file descriptor')
14
15    def test_go_application_variables(self):
16        self.load('variables')
17
18        body = 'Test body string.'
19
20        resp = self.post(
21            headers={
22                'Host': 'localhost',
23                'Content-Type': 'text/html',
24                'Custom-Header': 'blah',
25                'Connection': 'close',
26            },
27            body=body,
28        )
29
30        assert resp['status'] == 200, 'status'
31        headers = resp['headers']
32        header_server = headers.pop('Server')
33        assert re.search(r'Unit/[\d\.]+', header_server), 'server header'
34
35        date = headers.pop('Date')
36        assert date[-4:] == ' GMT', 'date header timezone'
37        assert (
38            abs(self.date_to_sec_epoch(date) - self.sec_epoch()) < 5
39        ), 'date header'
40
41        assert headers == {
42            'Content-Length': str(len(body)),
43            'Content-Type': 'text/html',
44            'Request-Method': 'POST',
45            'Request-Uri': '/',
46            'Http-Host': 'localhost',
47            'Server-Protocol': 'HTTP/1.1',
48            'Server-Protocol-Major': '1',
49            'Server-Protocol-Minor': '1',
50            'Custom-Header': 'blah',
51            'Connection': 'close',
52        }, 'headers'
53        assert 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        assert resp['headers']['X-Var-1'] == 'val1', 'GET variables'
60        assert resp['headers']['X-Var-2'] == '', 'GET variables 2'
61        assert 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        assert resp['headers']['X-Var-1'] == 'val1', 'POST variables'
76        assert resp['headers']['X-Var-2'] == '', 'POST variables 2'
77        assert 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        assert resp['status'] == 404, '404 status'
85        assert re.search(
86            r'<title>404 Not Found</title>', resp['body']
87        ), '404 body'
88
89    def test_go_keepalive_body(self):
90        self.load('mirror')
91
92        assert self.get()['status'] == 200, 'init'
93
94        body = '0123456789' * 500
95        (resp, sock) = self.post(
96            headers={
97                'Host': 'localhost',
98                'Connection': 'keep-alive',
99                'Content-Type': 'text/html',
100            },
101            start=True,
102            body=body,
103            read_timeout=1,
104        )
105
106        assert resp['body'] == body, 'keep-alive 1'
107
108        body = '0123456789'
109        resp = self.post(
110            headers={
111                'Host': 'localhost',
112                'Content-Type': 'text/html',
113                'Connection': 'close',
114            },
115            sock=sock,
116            body=body,
117        )
118
119        assert resp['body'] == body, 'keep-alive 2'
120
121    def test_go_application_cookies(self):
122        self.load('cookies')
123
124        resp = self.get(
125            headers={
126                'Host': 'localhost',
127                'Cookie': 'var1=val1; var2=val2',
128                'Connection': 'close',
129            }
130        )
131
132        assert resp['headers']['X-Cookie-1'] == 'val1', 'cookie 1'
133        assert resp['headers']['X-Cookie-2'] == 'val2', 'cookie 2'
134
135    def test_go_application_command_line_arguments_type(self):
136        self.load('command_line_arguments')
137
138        assert 'error' in self.conf(
139            '' "a b c", 'applications/command_line_arguments/arguments'
140        ), 'arguments type'
141
142    def test_go_application_command_line_arguments_0(self):
143        self.load('command_line_arguments')
144
145        assert self.get()['headers']['X-Arg-0'] == self.conf_get(
146            'applications/command_line_arguments/executable'
147        ), 'argument 0'
148
149    def test_go_application_command_line_arguments(self):
150        self.load('command_line_arguments')
151
152        arg1 = '--cc=gcc-7.2.0'
153        arg2 = '--cc-opt=\'-O0 -DNXT_DEBUG_MEMORY=1 -fsanitize=address\''
154        arg3 = '--debug'
155
156        assert 'success' in self.conf(
157            '["' + arg1 + '", "' + arg2 + '", "' + arg3 + '"]',
158            'applications/command_line_arguments/arguments',
159        )
160
161        assert (
162            self.get()['body'] == arg1 + ',' + arg2 + ',' + arg3
163        ), 'arguments'
164
165    def test_go_application_command_line_arguments_change(self):
166        self.load('command_line_arguments')
167
168        args_path = 'applications/command_line_arguments/arguments'
169
170        assert 'success' in self.conf('["0", "a", "$", ""]', args_path)
171
172        assert self.get()['body'] == '0,a,$,', 'arguments'
173
174        assert 'success' in self.conf('["-1", "b", "%"]', args_path)
175
176        assert self.get()['body'] == '-1,b,%', 'arguments change'
177
178        assert 'success' in self.conf('[]', args_path)
179
180        assert (
181            self.get()['headers']['Content-Length'] == '0'
182        ), 'arguments empty'
183