xref: /unit/test/test_ruby_application.py (revision 1848:4bd548074e2c)
1import re
2import subprocess
3
4import pytest
5
6from unit.applications.lang.ruby import TestApplicationRuby
7
8
9class TestRubyApplication(TestApplicationRuby):
10    prerequisites = {'modules': {'ruby': 'all'}}
11
12    def test_ruby_application(self):
13        self.load('variables')
14
15        body = 'Test body string.'
16
17        resp = self.post(
18            headers={
19                'Host': 'localhost',
20                'Content-Type': 'text/html',
21                'Custom-Header': 'blah',
22                'Connection': 'close',
23            },
24            body=body,
25        )
26
27        assert resp['status'] == 200, 'status'
28        headers = resp['headers']
29        header_server = headers.pop('Server')
30        assert re.search(r'Unit/[\d\.]+', header_server), 'server header'
31        assert (
32            headers.pop('Server-Software') == header_server
33        ), 'server software header'
34
35        date = headers.pop('Date')
36        assert date[-4:] == ' GMT', 'date header timezone'
37        assert (
38            abs(self.date_to_sec_epoch(date) - self.sec_epoch()) < 5
39        ), 'date header'
40
41        assert headers == {
42            'Connection': 'close',
43            'Content-Length': str(len(body)),
44            'Content-Type': 'text/html',
45            'Request-Method': 'POST',
46            'Request-Uri': '/',
47            'Http-Host': 'localhost',
48            'Server-Protocol': 'HTTP/1.1',
49            'Custom-Header': 'blah',
50            'Rack-Version': '13',
51            'Rack-Url-Scheme': 'http',
52            'Rack-Multithread': 'false',
53            'Rack-Multiprocess': 'true',
54            'Rack-Run-Once': 'false',
55            'Rack-Hijack-Q': 'false',
56            'Rack-Hijack': '',
57            'Rack-Hijack-IO': '',
58        }, 'headers'
59        assert resp['body'] == body, 'body'
60
61    def test_ruby_application_query_string(self):
62        self.load('query_string')
63
64        resp = self.get(url='/?var1=val1&var2=val2')
65
66        assert (
67            resp['headers']['Query-String'] == 'var1=val1&var2=val2'
68        ), 'Query-String header'
69
70    def test_ruby_application_query_string_empty(self):
71        self.load('query_string')
72
73        resp = self.get(url='/?')
74
75        assert resp['status'] == 200, 'query string empty status'
76        assert resp['headers']['Query-String'] == '', 'query string empty'
77
78    def test_ruby_application_query_string_absent(self):
79        self.load('query_string')
80
81        resp = self.get()
82
83        assert resp['status'] == 200, 'query string absent status'
84        assert resp['headers']['Query-String'] == '', 'query string absent'
85
86    @pytest.mark.skip('not yet')
87    def test_ruby_application_server_port(self):
88        self.load('server_port')
89
90        assert (
91            self.get()['headers']['Server-Port'] == '7080'
92        ), 'Server-Port header'
93
94    def test_ruby_application_status_int(self):
95        self.load('status_int')
96
97        assert self.get()['status'] == 200, 'status int'
98
99    def test_ruby_application_input_read_empty(self):
100        self.load('input_read_empty')
101
102        assert self.get()['body'] == '', 'read empty'
103
104    def test_ruby_application_input_read_parts(self):
105        self.load('input_read_parts')
106
107        assert (
108            self.post(body='0123456789')['body'] == '012345678'
109        ), 'input read parts'
110
111    def test_ruby_application_input_read_buffer(self):
112        self.load('input_read_buffer')
113
114        assert (
115            self.post(body='0123456789')['body'] == '0123456789'
116        ), 'input read buffer'
117
118    def test_ruby_application_input_read_buffer_not_empty(self):
119        self.load('input_read_buffer_not_empty')
120
121        assert (
122            self.post(body='0123456789')['body'] == '0123456789'
123        ), 'input read buffer not empty'
124
125    def test_ruby_application_input_gets(self):
126        self.load('input_gets')
127
128        body = '0123456789'
129
130        assert self.post(body=body)['body'] == body, 'input gets'
131
132    def test_ruby_application_input_gets_2(self):
133        self.load('input_gets')
134
135        assert (
136            self.post(body='01234\n56789\n')['body'] == '01234\n'
137        ), 'input gets 2'
138
139    def test_ruby_application_input_gets_all(self):
140        self.load('input_gets_all')
141
142        body = '\n01234\n56789\n\n'
143
144        assert self.post(body=body)['body'] == body, 'input gets all'
145
146    def test_ruby_application_input_each(self):
147        self.load('input_each')
148
149        body = '\n01234\n56789\n\n'
150
151        assert self.post(body=body)['body'] == body, 'input each'
152
153    @pytest.mark.skip('not yet')
154    def test_ruby_application_input_rewind(self):
155        self.load('input_rewind')
156
157        body = '0123456789'
158
159        assert self.post(body=body)['body'] == body, 'input rewind'
160
161    @pytest.mark.skip('not yet')
162    def test_ruby_application_syntax_error(self, skip_alert):
163        skip_alert(
164            r'Failed to parse rack script',
165            r'syntax error',
166            r'new_from_string',
167            r'parse_file',
168        )
169        self.load('syntax_error')
170
171        assert self.get()['status'] == 500, 'syntax error'
172
173    def test_ruby_application_errors_puts(self):
174        self.load('errors_puts')
175
176        self.get()
177
178        assert (
179            self.wait_for_record(r'\[error\].+Error in application')
180            is not None
181        ), 'errors puts'
182
183    def test_ruby_application_errors_puts_int(self):
184        self.load('errors_puts_int')
185
186        self.get()
187
188        assert (
189            self.wait_for_record(r'\[error\].+1234567890') is not None
190        ), 'errors puts int'
191
192    def test_ruby_application_errors_write(self):
193        self.load('errors_write')
194
195        self.get()
196
197        assert (
198            self.wait_for_record(r'\[error\].+Error in application')
199            is not None
200        ), 'errors write'
201
202    def test_ruby_application_errors_write_to_s_custom(self):
203        self.load('errors_write_to_s_custom')
204
205        assert self.get()['status'] == 200, 'errors write to_s custom'
206
207    def test_ruby_application_errors_write_int(self):
208        self.load('errors_write_int')
209
210        self.get()
211
212        assert (
213            self.wait_for_record(r'\[error\].+1234567890') is not None
214        ), 'errors write int'
215
216    def test_ruby_application_at_exit(self):
217        self.load('at_exit')
218
219        self.get()
220
221        assert 'success' in self.conf({"listeners": {}, "applications": {}})
222
223        assert (
224            self.wait_for_record(r'\[error\].+At exit called\.') is not None
225        ), 'at exit'
226
227    def test_ruby_application_encoding(self):
228        self.load('encoding')
229
230        try:
231            locales = (
232                subprocess.check_output(
233                    ['locale', '-a'], stderr=subprocess.STDOUT,
234                )
235                .decode()
236                .split('\n')
237            )
238
239        except (FileNotFoundError, subprocess.CalledProcessError):
240            pytest.skip('require locale')
241
242        def get_locale(pattern):
243            return next(
244                (
245                    l
246                    for l in locales
247                    if re.match(pattern, l.upper()) is not None
248                ),
249                None,
250            )
251
252        utf8 = get_locale(r'.*UTF[-_]?8')
253        iso88591 = get_locale(r'.*ISO[-_]?8859[-_]?1')
254
255        def check_locale(enc):
256            assert 'success' in self.conf(
257                {"LC_CTYPE": enc, "LC_ALL": ""},
258                '/config/applications/encoding/environment',
259            )
260
261            resp = self.get()
262            assert resp['status'] == 200, 'status'
263
264            enc_default = re.sub(r'[-_]', '', resp['headers']['X-Enc']).upper()
265            assert (
266                enc_default == re.sub(r'[-_]', '', enc.split('.')[-1]).upper()
267            )
268
269        if utf8:
270            check_locale(utf8)
271
272        if iso88591:
273            check_locale(iso88591)
274
275        if not utf8 and not iso88591:
276            pytest.skip('no available locales')
277
278    def test_ruby_application_header_custom(self):
279        self.load('header_custom')
280
281        resp = self.post(body="\ntc=one,two\ntc=three,four,\n\n")
282
283        assert resp['headers']['Custom-Header'] == [
284            '',
285            'tc=one,two',
286            'tc=three,four,',
287            '',
288            '',
289        ], 'header custom'
290
291    @pytest.mark.skip('not yet')
292    def test_ruby_application_header_custom_non_printable(self):
293        self.load('header_custom')
294
295        assert (
296            self.post(body='\b')['status'] == 500
297        ), 'header custom non printable'
298
299    def test_ruby_application_header_status(self):
300        self.load('header_status')
301
302        assert self.get()['status'] == 200, 'header status'
303
304    @pytest.mark.skip('not yet')
305    def test_ruby_application_header_rack(self):
306        self.load('header_rack')
307
308        assert self.get()['status'] == 500, 'header rack'
309
310    def test_ruby_application_body_empty(self):
311        self.load('body_empty')
312
313        assert self.get()['body'] == '', 'body empty'
314
315    def test_ruby_application_body_array(self):
316        self.load('body_array')
317
318        assert self.get()['body'] == '0123456789', 'body array'
319
320    def test_ruby_application_body_large(self):
321        self.load('mirror')
322
323        body = '0123456789' * 1000
324
325        assert self.post(body=body)['body'] == body, 'body large'
326
327    @pytest.mark.skip('not yet')
328    def test_ruby_application_body_each_error(self):
329        self.load('body_each_error')
330
331        assert self.get()['status'] == 500, 'body each error status'
332
333        assert (
334            self.wait_for_record(r'\[error\].+Failed to run ruby script')
335            is not None
336        ), 'body each error'
337
338    def test_ruby_application_body_file(self):
339        self.load('body_file')
340
341        assert self.get()['body'] == 'body\n', 'body file'
342
343    def test_ruby_keepalive_body(self):
344        self.load('mirror')
345
346        assert self.get()['status'] == 200, 'init'
347
348        body = '0123456789' * 500
349        (resp, sock) = self.post(
350            headers={
351                'Host': 'localhost',
352                'Connection': 'keep-alive',
353                'Content-Type': 'text/html',
354            },
355            start=True,
356            body=body,
357            read_timeout=1,
358        )
359
360        assert resp['body'] == body, 'keep-alive 1'
361
362        body = '0123456789'
363        resp = self.post(
364            headers={
365                'Host': 'localhost',
366                'Connection': 'close',
367                'Content-Type': 'text/html',
368            },
369            sock=sock,
370            body=body,
371        )
372
373        assert resp['body'] == body, 'keep-alive 2'
374
375    def test_ruby_application_constants(self):
376        self.load('constants')
377
378        resp = self.get()
379
380        assert resp['status'] == 200, 'status'
381
382        headers = resp['headers']
383        assert len(headers['X-Copyright']) > 0, 'RUBY_COPYRIGHT'
384        assert len(headers['X-Description']) > 0, 'RUBY_DESCRIPTION'
385        assert len(headers['X-Engine']) > 0, 'RUBY_ENGINE'
386        assert len(headers['X-Engine-Version']) > 0, 'RUBY_ENGINE_VERSION'
387        assert len(headers['X-Patchlevel']) > 0, 'RUBY_PATCHLEVEL'
388        assert len(headers['X-Platform']) > 0, 'RUBY_PLATFORM'
389        assert len(headers['X-Release-Date']) > 0, 'RUBY_RELEASE_DATE'
390        assert len(headers['X-Revision']) > 0, 'RUBY_REVISION'
391        assert len(headers['X-Version']) > 0, 'RUBY_VERSION'
392
393    def test_ruby_application_threads(self):
394        self.load('threads')
395
396        assert 'success' in self.conf(
397            '4', 'applications/threads/threads'
398        ), 'configure 4 threads'
399
400        socks = []
401
402        for i in range(4):
403            (_, sock) = self.get(
404                headers={
405                    'Host': 'localhost',
406                    'X-Delay': '2',
407                    'Connection': 'close',
408                },
409                no_recv=True,
410                start=True,
411            )
412
413            socks.append(sock)
414
415        threads = set()
416
417        for sock in socks:
418            resp = self.recvall(sock).decode('utf-8')
419
420            self.log_in(resp)
421
422            resp = self._resp_to_dict(resp)
423
424            assert resp['status'] == 200, 'status'
425
426            threads.add(resp['headers']['X-Thread'])
427
428            assert resp['headers']['Rack-Multithread'] == 'true', 'multithread'
429
430            sock.close()
431
432        assert len(socks) == len(threads), 'threads differs'
433