xref: /unit/test/test_go_application.py (revision 1596)
11019Szelenkov@nginx.comfrom unit.applications.lang.go import TestApplicationGo
2*1596Szelenkov@nginx.comimport re
3693Szelenkov@nginx.com
41017Szelenkov@nginx.com
51019Szelenkov@nginx.comclass TestGoApplication(TestApplicationGo):
61467Szelenkov@nginx.com    prerequisites = {'modules': {'go': 'all'}}
7693Szelenkov@nginx.com
8693Szelenkov@nginx.com    def test_go_application_variables(self):
9693Szelenkov@nginx.com        self.load('variables')
10693Szelenkov@nginx.com
11693Szelenkov@nginx.com        body = 'Test body string.'
12693Szelenkov@nginx.com
131017Szelenkov@nginx.com        resp = self.post(
141017Szelenkov@nginx.com            headers={
151017Szelenkov@nginx.com                'Host': 'localhost',
161017Szelenkov@nginx.com                'Content-Type': 'text/html',
171017Szelenkov@nginx.com                'Custom-Header': 'blah',
181017Szelenkov@nginx.com                'Connection': 'close',
191017Szelenkov@nginx.com            },
201017Szelenkov@nginx.com            body=body,
211017Szelenkov@nginx.com        )
22693Szelenkov@nginx.com
23*1596Szelenkov@nginx.com        assert resp['status'] == 200, 'status'
24693Szelenkov@nginx.com        headers = resp['headers']
25693Szelenkov@nginx.com        header_server = headers.pop('Server')
26*1596Szelenkov@nginx.com        assert re.search(r'Unit/[\d\.]+', header_server), 'server header'
27693Szelenkov@nginx.com
28693Szelenkov@nginx.com        date = headers.pop('Date')
29*1596Szelenkov@nginx.com        assert date[-4:] == ' GMT', 'date header timezone'
30*1596Szelenkov@nginx.com        assert (
31*1596Szelenkov@nginx.com            abs(self.date_to_sec_epoch(date) - self.sec_epoch()) < 5
32*1596Szelenkov@nginx.com        ), 'date header'
33693Szelenkov@nginx.com
34*1596Szelenkov@nginx.com        assert headers == {
35*1596Szelenkov@nginx.com            'Content-Length': str(len(body)),
36*1596Szelenkov@nginx.com            'Content-Type': 'text/html',
37*1596Szelenkov@nginx.com            'Request-Method': 'POST',
38*1596Szelenkov@nginx.com            'Request-Uri': '/',
39*1596Szelenkov@nginx.com            'Http-Host': 'localhost',
40*1596Szelenkov@nginx.com            'Server-Protocol': 'HTTP/1.1',
41*1596Szelenkov@nginx.com            'Server-Protocol-Major': '1',
42*1596Szelenkov@nginx.com            'Server-Protocol-Minor': '1',
43*1596Szelenkov@nginx.com            'Custom-Header': 'blah',
44*1596Szelenkov@nginx.com            'Connection': 'close',
45*1596Szelenkov@nginx.com        }, 'headers'
46*1596Szelenkov@nginx.com        assert resp['body'] == body, 'body'
47693Szelenkov@nginx.com
48693Szelenkov@nginx.com    def test_go_application_get_variables(self):
49693Szelenkov@nginx.com        self.load('get_variables')
50693Szelenkov@nginx.com
51693Szelenkov@nginx.com        resp = self.get(url='/?var1=val1&var2=&var3')
52*1596Szelenkov@nginx.com        assert resp['headers']['X-Var-1'] == 'val1', 'GET variables'
53*1596Szelenkov@nginx.com        assert resp['headers']['X-Var-2'] == '', 'GET variables 2'
54*1596Szelenkov@nginx.com        assert resp['headers']['X-Var-3'] == '', 'GET variables 3'
55693Szelenkov@nginx.com
56693Szelenkov@nginx.com    def test_go_application_post_variables(self):
57693Szelenkov@nginx.com        self.load('post_variables')
58693Szelenkov@nginx.com
591017Szelenkov@nginx.com        resp = self.post(
601017Szelenkov@nginx.com            headers={
611017Szelenkov@nginx.com                'Host': 'localhost',
621017Szelenkov@nginx.com                'Content-Type': 'application/x-www-form-urlencoded',
631017Szelenkov@nginx.com                'Connection': 'close',
641017Szelenkov@nginx.com            },
651017Szelenkov@nginx.com            body='var1=val1&var2=&var3',
661017Szelenkov@nginx.com        )
67693Szelenkov@nginx.com
68*1596Szelenkov@nginx.com        assert resp['headers']['X-Var-1'] == 'val1', 'POST variables'
69*1596Szelenkov@nginx.com        assert resp['headers']['X-Var-2'] == '', 'POST variables 2'
70*1596Szelenkov@nginx.com        assert resp['headers']['X-Var-3'] == '', 'POST variables 3'
71693Szelenkov@nginx.com
72693Szelenkov@nginx.com    def test_go_application_404(self):
73693Szelenkov@nginx.com        self.load('404')
74693Szelenkov@nginx.com
75693Szelenkov@nginx.com        resp = self.get()
76693Szelenkov@nginx.com
77*1596Szelenkov@nginx.com        assert resp['status'] == 404, '404 status'
78*1596Szelenkov@nginx.com        assert re.search(
79*1596Szelenkov@nginx.com            r'<title>404 Not Found</title>', resp['body']
80*1596Szelenkov@nginx.com        ), '404 body'
81693Szelenkov@nginx.com
82693Szelenkov@nginx.com    def test_go_keepalive_body(self):
83693Szelenkov@nginx.com        self.load('mirror')
84693Szelenkov@nginx.com
85*1596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'init'
861029Szelenkov@nginx.com
871453Szelenkov@nginx.com        body = '0123456789' * 500
881017Szelenkov@nginx.com        (resp, sock) = self.post(
891017Szelenkov@nginx.com            headers={
901017Szelenkov@nginx.com                'Host': 'localhost',
911017Szelenkov@nginx.com                'Connection': 'keep-alive',
921017Szelenkov@nginx.com                'Content-Type': 'text/html',
931017Szelenkov@nginx.com            },
941017Szelenkov@nginx.com            start=True,
951453Szelenkov@nginx.com            body=body,
961029Szelenkov@nginx.com            read_timeout=1,
971017Szelenkov@nginx.com        )
98693Szelenkov@nginx.com
99*1596Szelenkov@nginx.com        assert resp['body'] == body, 'keep-alive 1'
100693Szelenkov@nginx.com
1011453Szelenkov@nginx.com        body = '0123456789'
1021017Szelenkov@nginx.com        resp = self.post(
1031017Szelenkov@nginx.com            headers={
1041017Szelenkov@nginx.com                'Host': 'localhost',
1051017Szelenkov@nginx.com                'Content-Type': 'text/html',
1061017Szelenkov@nginx.com                'Connection': 'close',
1071017Szelenkov@nginx.com            },
1081017Szelenkov@nginx.com            sock=sock,
1091453Szelenkov@nginx.com            body=body,
1101017Szelenkov@nginx.com        )
111693Szelenkov@nginx.com
112*1596Szelenkov@nginx.com        assert resp['body'] == body, 'keep-alive 2'
113693Szelenkov@nginx.com
114693Szelenkov@nginx.com    def test_go_application_cookies(self):
115693Szelenkov@nginx.com        self.load('cookies')
116693Szelenkov@nginx.com
1171017Szelenkov@nginx.com        resp = self.get(
1181017Szelenkov@nginx.com            headers={
1191017Szelenkov@nginx.com                'Host': 'localhost',
1201017Szelenkov@nginx.com                'Cookie': 'var1=val1; var2=val2',
1211017Szelenkov@nginx.com                'Connection': 'close',
1221017Szelenkov@nginx.com            }
1231017Szelenkov@nginx.com        )
124693Szelenkov@nginx.com
125*1596Szelenkov@nginx.com        assert resp['headers']['X-Cookie-1'] == 'val1', 'cookie 1'
126*1596Szelenkov@nginx.com        assert resp['headers']['X-Cookie-2'] == 'val2', 'cookie 2'
127693Szelenkov@nginx.com
128693Szelenkov@nginx.com    def test_go_application_command_line_arguments_type(self):
129693Szelenkov@nginx.com        self.load('command_line_arguments')
130693Szelenkov@nginx.com
131*1596Szelenkov@nginx.com        assert 'error' in \
1321017Szelenkov@nginx.com            self.conf(
1331017Szelenkov@nginx.com                '' "a b c", 'applications/command_line_arguments/arguments'
134*1596Szelenkov@nginx.com            ), \
135*1596Szelenkov@nginx.com            'arguments type'
136693Szelenkov@nginx.com
137693Szelenkov@nginx.com    def test_go_application_command_line_arguments_0(self):
138693Szelenkov@nginx.com        self.load('command_line_arguments')
139693Szelenkov@nginx.com
140*1596Szelenkov@nginx.com        assert self.get()['headers']['X-Arg-0'] == self.conf_get(
141*1596Szelenkov@nginx.com            'applications/command_line_arguments/executable'
142*1596Szelenkov@nginx.com        ), 'argument 0'
143693Szelenkov@nginx.com
144693Szelenkov@nginx.com    def test_go_application_command_line_arguments(self):
145693Szelenkov@nginx.com        self.load('command_line_arguments')
146693Szelenkov@nginx.com
147693Szelenkov@nginx.com        arg1 = '--cc=gcc-7.2.0'
148693Szelenkov@nginx.com        arg2 = '--cc-opt=\'-O0 -DNXT_DEBUG_MEMORY=1 -fsanitize=address\''
149693Szelenkov@nginx.com        arg3 = '--debug'
150693Szelenkov@nginx.com
1511017Szelenkov@nginx.com        self.conf(
1521017Szelenkov@nginx.com            '["' + arg1 + '", "' + arg2 + '", "' + arg3 + '"]',
1531017Szelenkov@nginx.com            'applications/command_line_arguments/arguments',
1541017Szelenkov@nginx.com        )
155693Szelenkov@nginx.com
156*1596Szelenkov@nginx.com        assert (
157*1596Szelenkov@nginx.com            self.get()['body'] == arg1 + ',' + arg2 + ',' + arg3
158*1596Szelenkov@nginx.com        ), 'arguments'
159693Szelenkov@nginx.com
160693Szelenkov@nginx.com    def test_go_application_command_line_arguments_change(self):
161693Szelenkov@nginx.com        self.load('command_line_arguments')
162693Szelenkov@nginx.com
163760Szelenkov@nginx.com        args_path = 'applications/command_line_arguments/arguments'
164693Szelenkov@nginx.com
165693Szelenkov@nginx.com        self.conf('["0", "a", "$", ""]', args_path)
166693Szelenkov@nginx.com
167*1596Szelenkov@nginx.com        assert self.get()['body'] == '0,a,$,', 'arguments'
168693Szelenkov@nginx.com
169693Szelenkov@nginx.com        self.conf('["-1", "b", "%"]', args_path)
170693Szelenkov@nginx.com
171*1596Szelenkov@nginx.com        assert self.get()['body'] == '-1,b,%', 'arguments change'
172693Szelenkov@nginx.com
173693Szelenkov@nginx.com        self.conf('[]', args_path)
174693Szelenkov@nginx.com
175*1596Szelenkov@nginx.com        assert (
176*1596Szelenkov@nginx.com            self.get()['headers']['Content-Length'] == '0'
177*1596Szelenkov@nginx.com        ), 'arguments empty'
178