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