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