xref: /unit/test/test_go_application.py (revision 1848)
11635Szelenkov@nginx.comimport re
21635Szelenkov@nginx.com
31019Szelenkov@nginx.comfrom unit.applications.lang.go import TestApplicationGo
4693Szelenkov@nginx.com
51017Szelenkov@nginx.com
61019Szelenkov@nginx.comclass TestGoApplication(TestApplicationGo):
71467Szelenkov@nginx.com    prerequisites = {'modules': {'go': 'all'}}
8693Szelenkov@nginx.com
9693Szelenkov@nginx.com    def test_go_application_variables(self):
10693Szelenkov@nginx.com        self.load('variables')
11693Szelenkov@nginx.com
12693Szelenkov@nginx.com        body = 'Test body string.'
13693Szelenkov@nginx.com
141017Szelenkov@nginx.com        resp = self.post(
151017Szelenkov@nginx.com            headers={
161017Szelenkov@nginx.com                'Host': 'localhost',
171017Szelenkov@nginx.com                'Content-Type': 'text/html',
181017Szelenkov@nginx.com                'Custom-Header': 'blah',
191017Szelenkov@nginx.com                'Connection': 'close',
201017Szelenkov@nginx.com            },
211017Szelenkov@nginx.com            body=body,
221017Szelenkov@nginx.com        )
23693Szelenkov@nginx.com
241596Szelenkov@nginx.com        assert resp['status'] == 200, 'status'
25693Szelenkov@nginx.com        headers = resp['headers']
26693Szelenkov@nginx.com        header_server = headers.pop('Server')
271596Szelenkov@nginx.com        assert re.search(r'Unit/[\d\.]+', header_server), 'server header'
28693Szelenkov@nginx.com
29693Szelenkov@nginx.com        date = headers.pop('Date')
301596Szelenkov@nginx.com        assert date[-4:] == ' GMT', 'date header timezone'
311596Szelenkov@nginx.com        assert (
321596Szelenkov@nginx.com            abs(self.date_to_sec_epoch(date) - self.sec_epoch()) < 5
331596Szelenkov@nginx.com        ), 'date header'
34693Szelenkov@nginx.com
351596Szelenkov@nginx.com        assert headers == {
361596Szelenkov@nginx.com            'Content-Length': str(len(body)),
371596Szelenkov@nginx.com            'Content-Type': 'text/html',
381596Szelenkov@nginx.com            'Request-Method': 'POST',
391596Szelenkov@nginx.com            'Request-Uri': '/',
401596Szelenkov@nginx.com            'Http-Host': 'localhost',
411596Szelenkov@nginx.com            'Server-Protocol': 'HTTP/1.1',
421596Szelenkov@nginx.com            'Server-Protocol-Major': '1',
431596Szelenkov@nginx.com            'Server-Protocol-Minor': '1',
441596Szelenkov@nginx.com            'Custom-Header': 'blah',
451596Szelenkov@nginx.com            'Connection': 'close',
461596Szelenkov@nginx.com        }, 'headers'
471596Szelenkov@nginx.com        assert resp['body'] == body, 'body'
48693Szelenkov@nginx.com
49693Szelenkov@nginx.com    def test_go_application_get_variables(self):
50693Szelenkov@nginx.com        self.load('get_variables')
51693Szelenkov@nginx.com
52693Szelenkov@nginx.com        resp = self.get(url='/?var1=val1&var2=&var3')
531596Szelenkov@nginx.com        assert resp['headers']['X-Var-1'] == 'val1', 'GET variables'
541596Szelenkov@nginx.com        assert resp['headers']['X-Var-2'] == '', 'GET variables 2'
551596Szelenkov@nginx.com        assert resp['headers']['X-Var-3'] == '', 'GET variables 3'
56693Szelenkov@nginx.com
57693Szelenkov@nginx.com    def test_go_application_post_variables(self):
58693Szelenkov@nginx.com        self.load('post_variables')
59693Szelenkov@nginx.com
601017Szelenkov@nginx.com        resp = self.post(
611017Szelenkov@nginx.com            headers={
621017Szelenkov@nginx.com                'Host': 'localhost',
631017Szelenkov@nginx.com                'Content-Type': 'application/x-www-form-urlencoded',
641017Szelenkov@nginx.com                'Connection': 'close',
651017Szelenkov@nginx.com            },
661017Szelenkov@nginx.com            body='var1=val1&var2=&var3',
671017Szelenkov@nginx.com        )
68693Szelenkov@nginx.com
691596Szelenkov@nginx.com        assert resp['headers']['X-Var-1'] == 'val1', 'POST variables'
701596Szelenkov@nginx.com        assert resp['headers']['X-Var-2'] == '', 'POST variables 2'
711596Szelenkov@nginx.com        assert resp['headers']['X-Var-3'] == '', 'POST variables 3'
72693Szelenkov@nginx.com
73693Szelenkov@nginx.com    def test_go_application_404(self):
74693Szelenkov@nginx.com        self.load('404')
75693Szelenkov@nginx.com
76693Szelenkov@nginx.com        resp = self.get()
77693Szelenkov@nginx.com
781596Szelenkov@nginx.com        assert resp['status'] == 404, '404 status'
791596Szelenkov@nginx.com        assert re.search(
801596Szelenkov@nginx.com            r'<title>404 Not Found</title>', resp['body']
811596Szelenkov@nginx.com        ), '404 body'
82693Szelenkov@nginx.com
83693Szelenkov@nginx.com    def test_go_keepalive_body(self):
84693Szelenkov@nginx.com        self.load('mirror')
85693Szelenkov@nginx.com
861596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'init'
871029Szelenkov@nginx.com
881453Szelenkov@nginx.com        body = '0123456789' * 500
891017Szelenkov@nginx.com        (resp, sock) = self.post(
901017Szelenkov@nginx.com            headers={
911017Szelenkov@nginx.com                'Host': 'localhost',
921017Szelenkov@nginx.com                'Connection': 'keep-alive',
931017Szelenkov@nginx.com                'Content-Type': 'text/html',
941017Szelenkov@nginx.com            },
951017Szelenkov@nginx.com            start=True,
961453Szelenkov@nginx.com            body=body,
971029Szelenkov@nginx.com            read_timeout=1,
981017Szelenkov@nginx.com        )
99693Szelenkov@nginx.com
1001596Szelenkov@nginx.com        assert resp['body'] == body, 'keep-alive 1'
101693Szelenkov@nginx.com
1021453Szelenkov@nginx.com        body = '0123456789'
1031017Szelenkov@nginx.com        resp = self.post(
1041017Szelenkov@nginx.com            headers={
1051017Szelenkov@nginx.com                'Host': 'localhost',
1061017Szelenkov@nginx.com                'Content-Type': 'text/html',
1071017Szelenkov@nginx.com                'Connection': 'close',
1081017Szelenkov@nginx.com            },
1091017Szelenkov@nginx.com            sock=sock,
1101453Szelenkov@nginx.com            body=body,
1111017Szelenkov@nginx.com        )
112693Szelenkov@nginx.com
1131596Szelenkov@nginx.com        assert resp['body'] == body, 'keep-alive 2'
114693Szelenkov@nginx.com
115693Szelenkov@nginx.com    def test_go_application_cookies(self):
116693Szelenkov@nginx.com        self.load('cookies')
117693Szelenkov@nginx.com
1181017Szelenkov@nginx.com        resp = self.get(
1191017Szelenkov@nginx.com            headers={
1201017Szelenkov@nginx.com                'Host': 'localhost',
1211017Szelenkov@nginx.com                'Cookie': 'var1=val1; var2=val2',
1221017Szelenkov@nginx.com                'Connection': 'close',
1231017Szelenkov@nginx.com            }
1241017Szelenkov@nginx.com        )
125693Szelenkov@nginx.com
1261596Szelenkov@nginx.com        assert resp['headers']['X-Cookie-1'] == 'val1', 'cookie 1'
1271596Szelenkov@nginx.com        assert resp['headers']['X-Cookie-2'] == 'val2', 'cookie 2'
128693Szelenkov@nginx.com
129693Szelenkov@nginx.com    def test_go_application_command_line_arguments_type(self):
130693Szelenkov@nginx.com        self.load('command_line_arguments')
131693Szelenkov@nginx.com
132*1848Szelenkov@nginx.com        assert 'error' in self.conf(
133*1848Szelenkov@nginx.com            '' "a b c", 'applications/command_line_arguments/arguments'
134*1848Szelenkov@nginx.com        ), 'arguments type'
135693Szelenkov@nginx.com
136693Szelenkov@nginx.com    def test_go_application_command_line_arguments_0(self):
137693Szelenkov@nginx.com        self.load('command_line_arguments')
138693Szelenkov@nginx.com
1391596Szelenkov@nginx.com        assert self.get()['headers']['X-Arg-0'] == self.conf_get(
1401596Szelenkov@nginx.com            'applications/command_line_arguments/executable'
1411596Szelenkov@nginx.com        ), 'argument 0'
142693Szelenkov@nginx.com
143693Szelenkov@nginx.com    def test_go_application_command_line_arguments(self):
144693Szelenkov@nginx.com        self.load('command_line_arguments')
145693Szelenkov@nginx.com
146693Szelenkov@nginx.com        arg1 = '--cc=gcc-7.2.0'
147693Szelenkov@nginx.com        arg2 = '--cc-opt=\'-O0 -DNXT_DEBUG_MEMORY=1 -fsanitize=address\''
148693Szelenkov@nginx.com        arg3 = '--debug'
149693Szelenkov@nginx.com
1501775Szelenkov@nginx.com        assert 'success' in self.conf(
1511017Szelenkov@nginx.com            '["' + arg1 + '", "' + arg2 + '", "' + arg3 + '"]',
1521017Szelenkov@nginx.com            'applications/command_line_arguments/arguments',
1531017Szelenkov@nginx.com        )
154693Szelenkov@nginx.com
1551596Szelenkov@nginx.com        assert (
1561596Szelenkov@nginx.com            self.get()['body'] == arg1 + ',' + arg2 + ',' + arg3
1571596Szelenkov@nginx.com        ), 'arguments'
158693Szelenkov@nginx.com
159693Szelenkov@nginx.com    def test_go_application_command_line_arguments_change(self):
160693Szelenkov@nginx.com        self.load('command_line_arguments')
161693Szelenkov@nginx.com
162760Szelenkov@nginx.com        args_path = 'applications/command_line_arguments/arguments'
163693Szelenkov@nginx.com
1641775Szelenkov@nginx.com        assert 'success' in self.conf('["0", "a", "$", ""]', args_path)
165693Szelenkov@nginx.com
1661596Szelenkov@nginx.com        assert self.get()['body'] == '0,a,$,', 'arguments'
167693Szelenkov@nginx.com
1681775Szelenkov@nginx.com        assert 'success' in self.conf('["-1", "b", "%"]', args_path)
169693Szelenkov@nginx.com
1701596Szelenkov@nginx.com        assert self.get()['body'] == '-1,b,%', 'arguments change'
171693Szelenkov@nginx.com
1721775Szelenkov@nginx.com        assert 'success' in self.conf('[]', args_path)
173693Szelenkov@nginx.com
1741596Szelenkov@nginx.com        assert (
1751596Szelenkov@nginx.com            self.get()['headers']['Content-Length'] == '0'
1761596Szelenkov@nginx.com        ), 'arguments empty'
177