xref: /unit/test/test_perl_application.py (revision 1848:4bd548074e2c)
1import re
2
3import pytest
4
5from unit.applications.lang.perl import TestApplicationPerl
6
7
8class TestPerlApplication(TestApplicationPerl):
9    prerequisites = {'modules': {'perl': 'all'}}
10
11    def test_perl_application(self):
12        self.load('variables')
13
14        body = 'Test body string.'
15
16        resp = self.post(
17            headers={
18                'Host': 'localhost',
19                'Content-Type': 'text/html',
20                'Custom-Header': 'blah',
21                'Connection': 'close',
22            },
23            body=body,
24        )
25
26        assert resp['status'] == 200, 'status'
27        headers = resp['headers']
28        header_server = headers.pop('Server')
29        assert re.search(r'Unit/[\d\.]+', header_server), 'server header'
30        assert (
31            headers.pop('Server-Software') == header_server
32        ), 'server software header'
33
34        date = headers.pop('Date')
35        assert date[-4:] == ' GMT', 'date header timezone'
36        assert (
37            abs(self.date_to_sec_epoch(date) - self.sec_epoch()) < 5
38        ), 'date header'
39
40        assert headers == {
41            'Connection': 'close',
42            'Content-Length': str(len(body)),
43            'Content-Type': 'text/html',
44            'Request-Method': 'POST',
45            'Request-Uri': '/',
46            'Http-Host': 'localhost',
47            'Server-Protocol': 'HTTP/1.1',
48            'Custom-Header': 'blah',
49            'Psgi-Version': '11',
50            'Psgi-Url-Scheme': 'http',
51            'Psgi-Multithread': '',
52            'Psgi-Multiprocess': '1',
53            'Psgi-Run-Once': '',
54            'Psgi-Nonblocking': '',
55            'Psgi-Streaming': '1',
56        }, 'headers'
57        assert resp['body'] == body, 'body'
58
59    def test_perl_application_query_string(self):
60        self.load('query_string')
61
62        resp = self.get(url='/?var1=val1&var2=val2')
63
64        assert (
65            resp['headers']['Query-String'] == 'var1=val1&var2=val2'
66        ), 'Query-String header'
67
68    def test_perl_application_query_string_empty(self):
69        self.load('query_string')
70
71        resp = self.get(url='/?')
72
73        assert resp['status'] == 200, 'query string empty status'
74        assert resp['headers']['Query-String'] == '', 'query string empty'
75
76    def test_perl_application_query_string_absent(self):
77        self.load('query_string')
78
79        resp = self.get()
80
81        assert resp['status'] == 200, 'query string absent status'
82        assert resp['headers']['Query-String'] == '', 'query string absent'
83
84    @pytest.mark.skip('not yet')
85    def test_perl_application_server_port(self):
86        self.load('server_port')
87
88        assert (
89            self.get()['headers']['Server-Port'] == '7080'
90        ), 'Server-Port header'
91
92    def test_perl_application_input_read_empty(self):
93        self.load('input_read_empty')
94
95        assert self.get()['body'] == '', 'read empty'
96
97    def test_perl_application_input_read_parts(self):
98        self.load('input_read_parts')
99
100        assert (
101            self.post(body='0123456789')['body'] == '0123456789'
102        ), 'input read parts'
103
104    @pytest.mark.skip('not yet')
105    def test_perl_application_input_read_offset(self):
106        self.load('input_read_offset')
107
108        assert self.post(body='0123456789')['body'] == '4567', 'read offset'
109
110    def test_perl_application_input_copy(self):
111        self.load('input_copy')
112
113        body = '0123456789'
114        assert self.post(body=body)['body'] == body, 'input copy'
115
116    def test_perl_application_errors_print(self):
117        self.load('errors_print')
118
119        assert self.get()['body'] == '1', 'errors result'
120
121        assert (
122            self.wait_for_record(r'\[error\].+Error in application')
123            is not None
124        ), 'errors print'
125
126    def test_perl_application_header_equal_names(self):
127        self.load('header_equal_names')
128
129        assert self.get()['headers']['Set-Cookie'] == [
130            'tc=one,two,three',
131            'tc=four,five,six',
132        ], 'header equal names'
133
134    def test_perl_application_header_pairs(self):
135        self.load('header_pairs')
136
137        assert self.get()['headers']['blah'] == 'blah', 'header pairs'
138
139    def test_perl_application_body_empty(self):
140        self.load('body_empty')
141
142        assert self.get()['body'] == '', 'body empty'
143
144    def test_perl_application_body_array(self):
145        self.load('body_array')
146
147        assert self.get()['body'] == '0123456789', 'body array'
148
149    def test_perl_application_body_large(self):
150        self.load('variables')
151
152        body = '0123456789' * 1000
153
154        resp = self.post(body=body)['body']
155
156        assert resp == body, 'body large'
157
158    def test_perl_application_body_io_empty(self):
159        self.load('body_io_empty')
160
161        assert self.get()['status'] == 200, 'body io empty'
162
163    def test_perl_application_body_io_file(self):
164        self.load('body_io_file')
165
166        assert self.get()['body'] == 'body\n', 'body io file'
167
168    @pytest.mark.skip('not yet')
169    def test_perl_application_syntax_error(self, skip_alert):
170        skip_alert(r'PSGI: Failed to parse script')
171        self.load('syntax_error')
172
173        assert self.get()['status'] == 500, 'syntax error'
174
175    def test_perl_keepalive_body(self):
176        self.load('variables')
177
178        assert self.get()['status'] == 200, 'init'
179
180        body = '0123456789' * 500
181        (resp, sock) = self.post(
182            headers={
183                'Host': 'localhost',
184                'Connection': 'keep-alive',
185                'Content-Type': 'text/html',
186            },
187            start=True,
188            body=body,
189            read_timeout=1,
190        )
191
192        assert resp['body'] == body, 'keep-alive 1'
193
194        body = '0123456789'
195        resp = self.post(
196            headers={
197                'Host': 'localhost',
198                'Connection': 'close',
199                'Content-Type': 'text/html',
200            },
201            sock=sock,
202            body=body,
203        )
204
205        assert resp['body'] == body, 'keep-alive 2'
206
207    def test_perl_body_io_fake(self):
208        self.load('body_io_fake')
209
210        assert self.get()['body'] == '21', 'body io fake'
211
212        assert (
213            self.wait_for_record(r'\[error\].+IOFake getline\(\) \$\/ is \d+')
214            is not None
215        ), 'body io fake $/ value'
216
217        assert (
218            self.wait_for_record(r'\[error\].+IOFake close\(\) called')
219            is not None
220        ), 'body io fake close'
221
222    def test_perl_delayed_response(self):
223        self.load('delayed_response')
224
225        resp = self.get()
226
227        assert resp['status'] == 200, 'status'
228        assert resp['body'] == 'Hello World!', 'body'
229
230    def test_perl_streaming_body(self):
231        self.load('streaming_body')
232
233        resp = self.get()
234
235        assert resp['status'] == 200, 'status'
236        assert resp['body'] == 'Hello World!', 'body'
237
238    def test_perl_application_threads(self):
239        self.load('threads')
240
241        assert 'success' in self.conf(
242            '4', 'applications/threads/threads'
243        ), 'configure 4 threads'
244
245        socks = []
246
247        for i in range(4):
248            (_, sock) = self.get(
249                headers={
250                    'Host': 'localhost',
251                    'X-Delay': '2',
252                    'Connection': 'close',
253                },
254                no_recv=True,
255                start=True,
256            )
257
258            socks.append(sock)
259
260        threads = set()
261
262        for sock in socks:
263            resp = self.recvall(sock).decode('utf-8')
264
265            self.log_in(resp)
266
267            resp = self._resp_to_dict(resp)
268
269            assert resp['status'] == 200, 'status'
270
271            threads.add(resp['headers']['X-Thread'])
272
273            assert resp['headers']['Psgi-Multithread'] == '1', 'multithread'
274
275            sock.close()
276
277        assert len(socks) == len(threads), 'threads differs'
278