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