xref: /unit/test/test_go_application.py (revision 2073)
11635Szelenkov@nginx.comimport re
21635Szelenkov@nginx.com
31999Smax.romanov@nginx.comimport pytest
41019Szelenkov@nginx.comfrom unit.applications.lang.go import TestApplicationGo
5693Szelenkov@nginx.com
61017Szelenkov@nginx.com
71019Szelenkov@nginx.comclass TestGoApplication(TestApplicationGo):
81467Szelenkov@nginx.com    prerequisites = {'modules': {'go': 'all'}}
9693Szelenkov@nginx.com
101999Smax.romanov@nginx.com    @pytest.fixture(autouse=True)
111999Smax.romanov@nginx.com    def setup_method_fixture(self, request, skip_alert):
121999Smax.romanov@nginx.com        skip_alert(r'\[unit\] close\(\d+\) failed: Bad file descriptor')
131999Smax.romanov@nginx.com
14693Szelenkov@nginx.com    def test_go_application_variables(self):
15693Szelenkov@nginx.com        self.load('variables')
16693Szelenkov@nginx.com
17693Szelenkov@nginx.com        body = 'Test body string.'
18693Szelenkov@nginx.com
191017Szelenkov@nginx.com        resp = self.post(
201017Szelenkov@nginx.com            headers={
211017Szelenkov@nginx.com                'Host': 'localhost',
221017Szelenkov@nginx.com                'Content-Type': 'text/html',
231017Szelenkov@nginx.com                'Custom-Header': 'blah',
241017Szelenkov@nginx.com                'Connection': 'close',
251017Szelenkov@nginx.com            },
261017Szelenkov@nginx.com            body=body,
271017Szelenkov@nginx.com        )
28693Szelenkov@nginx.com
291596Szelenkov@nginx.com        assert resp['status'] == 200, 'status'
30693Szelenkov@nginx.com        headers = resp['headers']
31693Szelenkov@nginx.com        header_server = headers.pop('Server')
321596Szelenkov@nginx.com        assert re.search(r'Unit/[\d\.]+', header_server), 'server header'
33693Szelenkov@nginx.com
34693Szelenkov@nginx.com        date = headers.pop('Date')
351596Szelenkov@nginx.com        assert date[-4:] == ' GMT', 'date header timezone'
361596Szelenkov@nginx.com        assert (
371596Szelenkov@nginx.com            abs(self.date_to_sec_epoch(date) - self.sec_epoch()) < 5
381596Szelenkov@nginx.com        ), 'date header'
39693Szelenkov@nginx.com
401596Szelenkov@nginx.com        assert headers == {
411596Szelenkov@nginx.com            'Content-Length': str(len(body)),
421596Szelenkov@nginx.com            'Content-Type': 'text/html',
431596Szelenkov@nginx.com            'Request-Method': 'POST',
441596Szelenkov@nginx.com            'Request-Uri': '/',
451596Szelenkov@nginx.com            'Http-Host': 'localhost',
461596Szelenkov@nginx.com            'Server-Protocol': 'HTTP/1.1',
471596Szelenkov@nginx.com            'Server-Protocol-Major': '1',
481596Szelenkov@nginx.com            'Server-Protocol-Minor': '1',
491596Szelenkov@nginx.com            'Custom-Header': 'blah',
501596Szelenkov@nginx.com            'Connection': 'close',
511596Szelenkov@nginx.com        }, 'headers'
521596Szelenkov@nginx.com        assert resp['body'] == body, 'body'
53693Szelenkov@nginx.com
54693Szelenkov@nginx.com    def test_go_application_get_variables(self):
55693Szelenkov@nginx.com        self.load('get_variables')
56693Szelenkov@nginx.com
57693Szelenkov@nginx.com        resp = self.get(url='/?var1=val1&var2=&var3')
581596Szelenkov@nginx.com        assert resp['headers']['X-Var-1'] == 'val1', 'GET variables'
591596Szelenkov@nginx.com        assert resp['headers']['X-Var-2'] == '', 'GET variables 2'
601596Szelenkov@nginx.com        assert resp['headers']['X-Var-3'] == '', 'GET variables 3'
61693Szelenkov@nginx.com
62693Szelenkov@nginx.com    def test_go_application_post_variables(self):
63693Szelenkov@nginx.com        self.load('post_variables')
64693Szelenkov@nginx.com
651017Szelenkov@nginx.com        resp = self.post(
661017Szelenkov@nginx.com            headers={
671017Szelenkov@nginx.com                'Host': 'localhost',
681017Szelenkov@nginx.com                'Content-Type': 'application/x-www-form-urlencoded',
691017Szelenkov@nginx.com                'Connection': 'close',
701017Szelenkov@nginx.com            },
711017Szelenkov@nginx.com            body='var1=val1&var2=&var3',
721017Szelenkov@nginx.com        )
73693Szelenkov@nginx.com
741596Szelenkov@nginx.com        assert resp['headers']['X-Var-1'] == 'val1', 'POST variables'
751596Szelenkov@nginx.com        assert resp['headers']['X-Var-2'] == '', 'POST variables 2'
761596Szelenkov@nginx.com        assert resp['headers']['X-Var-3'] == '', 'POST variables 3'
77693Szelenkov@nginx.com
78693Szelenkov@nginx.com    def test_go_application_404(self):
79693Szelenkov@nginx.com        self.load('404')
80693Szelenkov@nginx.com
81693Szelenkov@nginx.com        resp = self.get()
82693Szelenkov@nginx.com
831596Szelenkov@nginx.com        assert resp['status'] == 404, '404 status'
841596Szelenkov@nginx.com        assert re.search(
851596Szelenkov@nginx.com            r'<title>404 Not Found</title>', resp['body']
861596Szelenkov@nginx.com        ), '404 body'
87693Szelenkov@nginx.com
88693Szelenkov@nginx.com    def test_go_keepalive_body(self):
89693Szelenkov@nginx.com        self.load('mirror')
90693Szelenkov@nginx.com
911596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'init'
921029Szelenkov@nginx.com
931453Szelenkov@nginx.com        body = '0123456789' * 500
941017Szelenkov@nginx.com        (resp, sock) = self.post(
951017Szelenkov@nginx.com            headers={
961017Szelenkov@nginx.com                'Host': 'localhost',
971017Szelenkov@nginx.com                'Connection': 'keep-alive',
981017Szelenkov@nginx.com                'Content-Type': 'text/html',
991017Szelenkov@nginx.com            },
1001017Szelenkov@nginx.com            start=True,
1011453Szelenkov@nginx.com            body=body,
1021029Szelenkov@nginx.com            read_timeout=1,
1031017Szelenkov@nginx.com        )
104693Szelenkov@nginx.com
1051596Szelenkov@nginx.com        assert resp['body'] == body, 'keep-alive 1'
106693Szelenkov@nginx.com
1071453Szelenkov@nginx.com        body = '0123456789'
1081017Szelenkov@nginx.com        resp = self.post(
1091017Szelenkov@nginx.com            headers={
1101017Szelenkov@nginx.com                'Host': 'localhost',
1111017Szelenkov@nginx.com                'Content-Type': 'text/html',
1121017Szelenkov@nginx.com                'Connection': 'close',
1131017Szelenkov@nginx.com            },
1141017Szelenkov@nginx.com            sock=sock,
1151453Szelenkov@nginx.com            body=body,
1161017Szelenkov@nginx.com        )
117693Szelenkov@nginx.com
1181596Szelenkov@nginx.com        assert resp['body'] == body, 'keep-alive 2'
119693Szelenkov@nginx.com
120693Szelenkov@nginx.com    def test_go_application_cookies(self):
121693Szelenkov@nginx.com        self.load('cookies')
122693Szelenkov@nginx.com
1231017Szelenkov@nginx.com        resp = self.get(
1241017Szelenkov@nginx.com            headers={
1251017Szelenkov@nginx.com                'Host': 'localhost',
1261017Szelenkov@nginx.com                'Cookie': 'var1=val1; var2=val2',
1271017Szelenkov@nginx.com                'Connection': 'close',
1281017Szelenkov@nginx.com            }
1291017Szelenkov@nginx.com        )
130693Szelenkov@nginx.com
1311596Szelenkov@nginx.com        assert resp['headers']['X-Cookie-1'] == 'val1', 'cookie 1'
1321596Szelenkov@nginx.com        assert resp['headers']['X-Cookie-2'] == 'val2', 'cookie 2'
133693Szelenkov@nginx.com
134693Szelenkov@nginx.com    def test_go_application_command_line_arguments_type(self):
135693Szelenkov@nginx.com        self.load('command_line_arguments')
136693Szelenkov@nginx.com
1371848Szelenkov@nginx.com        assert 'error' in self.conf(
1381848Szelenkov@nginx.com            '' "a b c", 'applications/command_line_arguments/arguments'
1391848Szelenkov@nginx.com        ), 'arguments type'
140693Szelenkov@nginx.com
141693Szelenkov@nginx.com    def test_go_application_command_line_arguments_0(self):
142693Szelenkov@nginx.com        self.load('command_line_arguments')
143693Szelenkov@nginx.com
1441596Szelenkov@nginx.com        assert self.get()['headers']['X-Arg-0'] == self.conf_get(
1451596Szelenkov@nginx.com            'applications/command_line_arguments/executable'
1461596Szelenkov@nginx.com        ), 'argument 0'
147693Szelenkov@nginx.com
148693Szelenkov@nginx.com    def test_go_application_command_line_arguments(self):
149693Szelenkov@nginx.com        self.load('command_line_arguments')
150693Szelenkov@nginx.com
151693Szelenkov@nginx.com        arg1 = '--cc=gcc-7.2.0'
152693Szelenkov@nginx.com        arg2 = '--cc-opt=\'-O0 -DNXT_DEBUG_MEMORY=1 -fsanitize=address\''
153693Szelenkov@nginx.com        arg3 = '--debug'
154693Szelenkov@nginx.com
1551775Szelenkov@nginx.com        assert 'success' in self.conf(
1561017Szelenkov@nginx.com            '["' + arg1 + '", "' + arg2 + '", "' + arg3 + '"]',
1571017Szelenkov@nginx.com            'applications/command_line_arguments/arguments',
1581017Szelenkov@nginx.com        )
159693Szelenkov@nginx.com
160*2073Szelenkov@nginx.com        assert self.get()['body'] == arg1 + ',' + arg2 + ',' + arg3, 'arguments'
161693Szelenkov@nginx.com
162693Szelenkov@nginx.com    def test_go_application_command_line_arguments_change(self):
163693Szelenkov@nginx.com        self.load('command_line_arguments')
164693Szelenkov@nginx.com
165760Szelenkov@nginx.com        args_path = 'applications/command_line_arguments/arguments'
166693Szelenkov@nginx.com
1671775Szelenkov@nginx.com        assert 'success' in self.conf('["0", "a", "$", ""]', args_path)
168693Szelenkov@nginx.com
1691596Szelenkov@nginx.com        assert self.get()['body'] == '0,a,$,', 'arguments'
170693Szelenkov@nginx.com
1711775Szelenkov@nginx.com        assert 'success' in self.conf('["-1", "b", "%"]', args_path)
172693Szelenkov@nginx.com
1731596Szelenkov@nginx.com        assert self.get()['body'] == '-1,b,%', 'arguments change'
174693Szelenkov@nginx.com
1751775Szelenkov@nginx.com        assert 'success' in self.conf('[]', args_path)
176693Szelenkov@nginx.com
177*2073Szelenkov@nginx.com        assert self.get()['headers']['Content-Length'] == '0', 'arguments empty'
178