xref: /unit/test/test_go_application.py (revision 1999)
11635Szelenkov@nginx.comimport re
21635Szelenkov@nginx.com
3*1999Smax.romanov@nginx.comimport pytest
4*1999Smax.romanov@nginx.com
51019Szelenkov@nginx.comfrom unit.applications.lang.go import TestApplicationGo
6693Szelenkov@nginx.com
71017Szelenkov@nginx.com
81019Szelenkov@nginx.comclass TestGoApplication(TestApplicationGo):
91467Szelenkov@nginx.com    prerequisites = {'modules': {'go': 'all'}}
10693Szelenkov@nginx.com
11*1999Smax.romanov@nginx.com    @pytest.fixture(autouse=True)
12*1999Smax.romanov@nginx.com    def setup_method_fixture(self, request, skip_alert):
13*1999Smax.romanov@nginx.com        skip_alert(r'\[unit\] close\(\d+\) failed: Bad file descriptor')
14*1999Smax.romanov@nginx.com
15693Szelenkov@nginx.com    def test_go_application_variables(self):
16693Szelenkov@nginx.com        self.load('variables')
17693Szelenkov@nginx.com
18693Szelenkov@nginx.com        body = 'Test body string.'
19693Szelenkov@nginx.com
201017Szelenkov@nginx.com        resp = self.post(
211017Szelenkov@nginx.com            headers={
221017Szelenkov@nginx.com                'Host': 'localhost',
231017Szelenkov@nginx.com                'Content-Type': 'text/html',
241017Szelenkov@nginx.com                'Custom-Header': 'blah',
251017Szelenkov@nginx.com                'Connection': 'close',
261017Szelenkov@nginx.com            },
271017Szelenkov@nginx.com            body=body,
281017Szelenkov@nginx.com        )
29693Szelenkov@nginx.com
301596Szelenkov@nginx.com        assert resp['status'] == 200, 'status'
31693Szelenkov@nginx.com        headers = resp['headers']
32693Szelenkov@nginx.com        header_server = headers.pop('Server')
331596Szelenkov@nginx.com        assert re.search(r'Unit/[\d\.]+', header_server), 'server header'
34693Szelenkov@nginx.com
35693Szelenkov@nginx.com        date = headers.pop('Date')
361596Szelenkov@nginx.com        assert date[-4:] == ' GMT', 'date header timezone'
371596Szelenkov@nginx.com        assert (
381596Szelenkov@nginx.com            abs(self.date_to_sec_epoch(date) - self.sec_epoch()) < 5
391596Szelenkov@nginx.com        ), 'date header'
40693Szelenkov@nginx.com
411596Szelenkov@nginx.com        assert headers == {
421596Szelenkov@nginx.com            'Content-Length': str(len(body)),
431596Szelenkov@nginx.com            'Content-Type': 'text/html',
441596Szelenkov@nginx.com            'Request-Method': 'POST',
451596Szelenkov@nginx.com            'Request-Uri': '/',
461596Szelenkov@nginx.com            'Http-Host': 'localhost',
471596Szelenkov@nginx.com            'Server-Protocol': 'HTTP/1.1',
481596Szelenkov@nginx.com            'Server-Protocol-Major': '1',
491596Szelenkov@nginx.com            'Server-Protocol-Minor': '1',
501596Szelenkov@nginx.com            'Custom-Header': 'blah',
511596Szelenkov@nginx.com            'Connection': 'close',
521596Szelenkov@nginx.com        }, 'headers'
531596Szelenkov@nginx.com        assert resp['body'] == body, 'body'
54693Szelenkov@nginx.com
55693Szelenkov@nginx.com    def test_go_application_get_variables(self):
56693Szelenkov@nginx.com        self.load('get_variables')
57693Szelenkov@nginx.com
58693Szelenkov@nginx.com        resp = self.get(url='/?var1=val1&var2=&var3')
591596Szelenkov@nginx.com        assert resp['headers']['X-Var-1'] == 'val1', 'GET variables'
601596Szelenkov@nginx.com        assert resp['headers']['X-Var-2'] == '', 'GET variables 2'
611596Szelenkov@nginx.com        assert resp['headers']['X-Var-3'] == '', 'GET variables 3'
62693Szelenkov@nginx.com
63693Szelenkov@nginx.com    def test_go_application_post_variables(self):
64693Szelenkov@nginx.com        self.load('post_variables')
65693Szelenkov@nginx.com
661017Szelenkov@nginx.com        resp = self.post(
671017Szelenkov@nginx.com            headers={
681017Szelenkov@nginx.com                'Host': 'localhost',
691017Szelenkov@nginx.com                'Content-Type': 'application/x-www-form-urlencoded',
701017Szelenkov@nginx.com                'Connection': 'close',
711017Szelenkov@nginx.com            },
721017Szelenkov@nginx.com            body='var1=val1&var2=&var3',
731017Szelenkov@nginx.com        )
74693Szelenkov@nginx.com
751596Szelenkov@nginx.com        assert resp['headers']['X-Var-1'] == 'val1', 'POST variables'
761596Szelenkov@nginx.com        assert resp['headers']['X-Var-2'] == '', 'POST variables 2'
771596Szelenkov@nginx.com        assert resp['headers']['X-Var-3'] == '', 'POST variables 3'
78693Szelenkov@nginx.com
79693Szelenkov@nginx.com    def test_go_application_404(self):
80693Szelenkov@nginx.com        self.load('404')
81693Szelenkov@nginx.com
82693Szelenkov@nginx.com        resp = self.get()
83693Szelenkov@nginx.com
841596Szelenkov@nginx.com        assert resp['status'] == 404, '404 status'
851596Szelenkov@nginx.com        assert re.search(
861596Szelenkov@nginx.com            r'<title>404 Not Found</title>', resp['body']
871596Szelenkov@nginx.com        ), '404 body'
88693Szelenkov@nginx.com
89693Szelenkov@nginx.com    def test_go_keepalive_body(self):
90693Szelenkov@nginx.com        self.load('mirror')
91693Szelenkov@nginx.com
921596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'init'
931029Szelenkov@nginx.com
941453Szelenkov@nginx.com        body = '0123456789' * 500
951017Szelenkov@nginx.com        (resp, sock) = self.post(
961017Szelenkov@nginx.com            headers={
971017Szelenkov@nginx.com                'Host': 'localhost',
981017Szelenkov@nginx.com                'Connection': 'keep-alive',
991017Szelenkov@nginx.com                'Content-Type': 'text/html',
1001017Szelenkov@nginx.com            },
1011017Szelenkov@nginx.com            start=True,
1021453Szelenkov@nginx.com            body=body,
1031029Szelenkov@nginx.com            read_timeout=1,
1041017Szelenkov@nginx.com        )
105693Szelenkov@nginx.com
1061596Szelenkov@nginx.com        assert resp['body'] == body, 'keep-alive 1'
107693Szelenkov@nginx.com
1081453Szelenkov@nginx.com        body = '0123456789'
1091017Szelenkov@nginx.com        resp = self.post(
1101017Szelenkov@nginx.com            headers={
1111017Szelenkov@nginx.com                'Host': 'localhost',
1121017Szelenkov@nginx.com                'Content-Type': 'text/html',
1131017Szelenkov@nginx.com                'Connection': 'close',
1141017Szelenkov@nginx.com            },
1151017Szelenkov@nginx.com            sock=sock,
1161453Szelenkov@nginx.com            body=body,
1171017Szelenkov@nginx.com        )
118693Szelenkov@nginx.com
1191596Szelenkov@nginx.com        assert resp['body'] == body, 'keep-alive 2'
120693Szelenkov@nginx.com
121693Szelenkov@nginx.com    def test_go_application_cookies(self):
122693Szelenkov@nginx.com        self.load('cookies')
123693Szelenkov@nginx.com
1241017Szelenkov@nginx.com        resp = self.get(
1251017Szelenkov@nginx.com            headers={
1261017Szelenkov@nginx.com                'Host': 'localhost',
1271017Szelenkov@nginx.com                'Cookie': 'var1=val1; var2=val2',
1281017Szelenkov@nginx.com                'Connection': 'close',
1291017Szelenkov@nginx.com            }
1301017Szelenkov@nginx.com        )
131693Szelenkov@nginx.com
1321596Szelenkov@nginx.com        assert resp['headers']['X-Cookie-1'] == 'val1', 'cookie 1'
1331596Szelenkov@nginx.com        assert resp['headers']['X-Cookie-2'] == 'val2', 'cookie 2'
134693Szelenkov@nginx.com
135693Szelenkov@nginx.com    def test_go_application_command_line_arguments_type(self):
136693Szelenkov@nginx.com        self.load('command_line_arguments')
137693Szelenkov@nginx.com
1381848Szelenkov@nginx.com        assert 'error' in self.conf(
1391848Szelenkov@nginx.com            '' "a b c", 'applications/command_line_arguments/arguments'
1401848Szelenkov@nginx.com        ), 'arguments type'
141693Szelenkov@nginx.com
142693Szelenkov@nginx.com    def test_go_application_command_line_arguments_0(self):
143693Szelenkov@nginx.com        self.load('command_line_arguments')
144693Szelenkov@nginx.com
1451596Szelenkov@nginx.com        assert self.get()['headers']['X-Arg-0'] == self.conf_get(
1461596Szelenkov@nginx.com            'applications/command_line_arguments/executable'
1471596Szelenkov@nginx.com        ), 'argument 0'
148693Szelenkov@nginx.com
149693Szelenkov@nginx.com    def test_go_application_command_line_arguments(self):
150693Szelenkov@nginx.com        self.load('command_line_arguments')
151693Szelenkov@nginx.com
152693Szelenkov@nginx.com        arg1 = '--cc=gcc-7.2.0'
153693Szelenkov@nginx.com        arg2 = '--cc-opt=\'-O0 -DNXT_DEBUG_MEMORY=1 -fsanitize=address\''
154693Szelenkov@nginx.com        arg3 = '--debug'
155693Szelenkov@nginx.com
1561775Szelenkov@nginx.com        assert 'success' in self.conf(
1571017Szelenkov@nginx.com            '["' + arg1 + '", "' + arg2 + '", "' + arg3 + '"]',
1581017Szelenkov@nginx.com            'applications/command_line_arguments/arguments',
1591017Szelenkov@nginx.com        )
160693Szelenkov@nginx.com
1611596Szelenkov@nginx.com        assert (
1621596Szelenkov@nginx.com            self.get()['body'] == arg1 + ',' + arg2 + ',' + arg3
1631596Szelenkov@nginx.com        ), 'arguments'
164693Szelenkov@nginx.com
165693Szelenkov@nginx.com    def test_go_application_command_line_arguments_change(self):
166693Szelenkov@nginx.com        self.load('command_line_arguments')
167693Szelenkov@nginx.com
168760Szelenkov@nginx.com        args_path = 'applications/command_line_arguments/arguments'
169693Szelenkov@nginx.com
1701775Szelenkov@nginx.com        assert 'success' in self.conf('["0", "a", "$", ""]', args_path)
171693Szelenkov@nginx.com
1721596Szelenkov@nginx.com        assert self.get()['body'] == '0,a,$,', 'arguments'
173693Szelenkov@nginx.com
1741775Szelenkov@nginx.com        assert 'success' in self.conf('["-1", "b", "%"]', args_path)
175693Szelenkov@nginx.com
1761596Szelenkov@nginx.com        assert self.get()['body'] == '-1,b,%', 'arguments change'
177693Szelenkov@nginx.com
1781775Szelenkov@nginx.com        assert 'success' in self.conf('[]', args_path)
179693Szelenkov@nginx.com
1801596Szelenkov@nginx.com        assert (
1811596Szelenkov@nginx.com            self.get()['headers']['Content-Length'] == '0'
1821596Szelenkov@nginx.com        ), 'arguments empty'
183