xref: /unit/test/test_perl_application.py (revision 2073:bc6ad31ce286)
11596Szelenkov@nginx.comimport re
21477Szelenkov@nginx.com
31635Szelenkov@nginx.comimport pytest
41019Szelenkov@nginx.comfrom unit.applications.lang.perl import TestApplicationPerl
5544Szelenkov@nginx.com
61017Szelenkov@nginx.com
71019Szelenkov@nginx.comclass TestPerlApplication(TestApplicationPerl):
81467Szelenkov@nginx.com    prerequisites = {'modules': {'perl': 'all'}}
9544Szelenkov@nginx.com
10544Szelenkov@nginx.com    def test_perl_application(self):
11544Szelenkov@nginx.com        self.load('variables')
12544Szelenkov@nginx.com
13544Szelenkov@nginx.com        body = 'Test body string.'
14544Szelenkov@nginx.com
151017Szelenkov@nginx.com        resp = self.post(
161017Szelenkov@nginx.com            headers={
171017Szelenkov@nginx.com                'Host': 'localhost',
181017Szelenkov@nginx.com                'Content-Type': 'text/html',
191017Szelenkov@nginx.com                'Custom-Header': 'blah',
201017Szelenkov@nginx.com                'Connection': 'close',
211017Szelenkov@nginx.com            },
221017Szelenkov@nginx.com            body=body,
231017Szelenkov@nginx.com        )
24544Szelenkov@nginx.com
251596Szelenkov@nginx.com        assert resp['status'] == 200, 'status'
26544Szelenkov@nginx.com        headers = resp['headers']
27674Szelenkov@nginx.com        header_server = headers.pop('Server')
281596Szelenkov@nginx.com        assert re.search(r'Unit/[\d\.]+', header_server), 'server header'
291596Szelenkov@nginx.com        assert (
301596Szelenkov@nginx.com            headers.pop('Server-Software') == header_server
311596Szelenkov@nginx.com        ), 'server software header'
32599Szelenkov@nginx.com
33599Szelenkov@nginx.com        date = headers.pop('Date')
341596Szelenkov@nginx.com        assert date[-4:] == ' GMT', 'date header timezone'
351596Szelenkov@nginx.com        assert (
361596Szelenkov@nginx.com            abs(self.date_to_sec_epoch(date) - self.sec_epoch()) < 5
371596Szelenkov@nginx.com        ), 'date header'
38599Szelenkov@nginx.com
391596Szelenkov@nginx.com        assert headers == {
401596Szelenkov@nginx.com            'Connection': 'close',
411596Szelenkov@nginx.com            'Content-Length': str(len(body)),
421596Szelenkov@nginx.com            'Content-Type': 'text/html',
431596Szelenkov@nginx.com            'Request-Method': 'POST',
441596Szelenkov@nginx.com            'Request-Uri': '/',
451596Szelenkov@nginx.com            'Http-Host': 'localhost',
461596Szelenkov@nginx.com            'Server-Protocol': 'HTTP/1.1',
471596Szelenkov@nginx.com            'Custom-Header': 'blah',
481596Szelenkov@nginx.com            'Psgi-Version': '11',
491596Szelenkov@nginx.com            'Psgi-Url-Scheme': 'http',
501596Szelenkov@nginx.com            'Psgi-Multithread': '',
511596Szelenkov@nginx.com            'Psgi-Multiprocess': '1',
521596Szelenkov@nginx.com            'Psgi-Run-Once': '',
531596Szelenkov@nginx.com            'Psgi-Nonblocking': '',
541596Szelenkov@nginx.com            'Psgi-Streaming': '1',
551596Szelenkov@nginx.com        }, 'headers'
561596Szelenkov@nginx.com        assert resp['body'] == body, 'body'
57544Szelenkov@nginx.com
58544Szelenkov@nginx.com    def test_perl_application_query_string(self):
59544Szelenkov@nginx.com        self.load('query_string')
60544Szelenkov@nginx.com
61544Szelenkov@nginx.com        resp = self.get(url='/?var1=val1&var2=val2')
62544Szelenkov@nginx.com
631596Szelenkov@nginx.com        assert (
641596Szelenkov@nginx.com            resp['headers']['Query-String'] == 'var1=val1&var2=val2'
651596Szelenkov@nginx.com        ), 'Query-String header'
66544Szelenkov@nginx.com
67894Szelenkov@nginx.com    def test_perl_application_query_string_empty(self):
68894Szelenkov@nginx.com        self.load('query_string')
69894Szelenkov@nginx.com
70894Szelenkov@nginx.com        resp = self.get(url='/?')
71894Szelenkov@nginx.com
721596Szelenkov@nginx.com        assert resp['status'] == 200, 'query string empty status'
731596Szelenkov@nginx.com        assert resp['headers']['Query-String'] == '', 'query string empty'
74894Szelenkov@nginx.com
75894Szelenkov@nginx.com    def test_perl_application_query_string_absent(self):
76894Szelenkov@nginx.com        self.load('query_string')
77894Szelenkov@nginx.com
78894Szelenkov@nginx.com        resp = self.get()
79894Szelenkov@nginx.com
801596Szelenkov@nginx.com        assert resp['status'] == 200, 'query string absent status'
811596Szelenkov@nginx.com        assert resp['headers']['Query-String'] == '', 'query string absent'
82894Szelenkov@nginx.com
831596Szelenkov@nginx.com    @pytest.mark.skip('not yet')
84544Szelenkov@nginx.com    def test_perl_application_server_port(self):
85544Szelenkov@nginx.com        self.load('server_port')
86544Szelenkov@nginx.com
871596Szelenkov@nginx.com        assert (
881596Szelenkov@nginx.com            self.get()['headers']['Server-Port'] == '7080'
891596Szelenkov@nginx.com        ), 'Server-Port header'
90544Szelenkov@nginx.com
91544Szelenkov@nginx.com    def test_perl_application_input_read_empty(self):
92544Szelenkov@nginx.com        self.load('input_read_empty')
93544Szelenkov@nginx.com
941596Szelenkov@nginx.com        assert self.get()['body'] == '', 'read empty'
95544Szelenkov@nginx.com
96554Szelenkov@nginx.com    def test_perl_application_input_read_parts(self):
97554Szelenkov@nginx.com        self.load('input_read_parts')
98554Szelenkov@nginx.com
991596Szelenkov@nginx.com        assert (
1001596Szelenkov@nginx.com            self.post(body='0123456789')['body'] == '0123456789'
1011596Szelenkov@nginx.com        ), 'input read parts'
102554Szelenkov@nginx.com
1032060Smax.romanov@nginx.com    def test_perl_application_input_buffered_read(self):
1042060Smax.romanov@nginx.com        self.load('input_buffered_read')
1052060Smax.romanov@nginx.com
1062066Szelenkov@nginx.com        assert self.post(body='012345')['body'] == '012345', 'buffered read #1'
1072060Smax.romanov@nginx.com        assert (
1082060Smax.romanov@nginx.com            self.post(body='9876543210')['body'] == '9876543210'
1092060Smax.romanov@nginx.com        ), 'buffered read #2'
1102060Smax.romanov@nginx.com
1112060Smax.romanov@nginx.com    def test_perl_application_input_close(self):
1122060Smax.romanov@nginx.com        self.load('input_close')
1132060Smax.romanov@nginx.com
1142066Szelenkov@nginx.com        assert self.post(body='012345')['body'] == '012345', 'input close #1'
1152060Smax.romanov@nginx.com        assert (
1162060Smax.romanov@nginx.com            self.post(body='9876543210')['body'] == '9876543210'
1172060Smax.romanov@nginx.com        ), 'input close #2'
1182060Smax.romanov@nginx.com
1191596Szelenkov@nginx.com    @pytest.mark.skip('not yet')
120544Szelenkov@nginx.com    def test_perl_application_input_read_offset(self):
121544Szelenkov@nginx.com        self.load('input_read_offset')
122544Szelenkov@nginx.com
1231596Szelenkov@nginx.com        assert self.post(body='0123456789')['body'] == '4567', 'read offset'
124544Szelenkov@nginx.com
125544Szelenkov@nginx.com    def test_perl_application_input_copy(self):
126544Szelenkov@nginx.com        self.load('input_copy')
127544Szelenkov@nginx.com
128544Szelenkov@nginx.com        body = '0123456789'
1291596Szelenkov@nginx.com        assert self.post(body=body)['body'] == body, 'input copy'
130544Szelenkov@nginx.com
131544Szelenkov@nginx.com    def test_perl_application_errors_print(self):
132544Szelenkov@nginx.com        self.load('errors_print')
133544Szelenkov@nginx.com
1341596Szelenkov@nginx.com        assert self.get()['body'] == '1', 'errors result'
135544Szelenkov@nginx.com
1361596Szelenkov@nginx.com        assert (
137*2073Szelenkov@nginx.com            self.wait_for_record(r'\[error\].+Error in application') is not None
1381596Szelenkov@nginx.com        ), 'errors print'
139544Szelenkov@nginx.com
140554Szelenkov@nginx.com    def test_perl_application_header_equal_names(self):
141554Szelenkov@nginx.com        self.load('header_equal_names')
142554Szelenkov@nginx.com
1431596Szelenkov@nginx.com        assert self.get()['headers']['Set-Cookie'] == [
1441596Szelenkov@nginx.com            'tc=one,two,three',
1451596Szelenkov@nginx.com            'tc=four,five,six',
1461596Szelenkov@nginx.com        ], 'header equal names'
147554Szelenkov@nginx.com
148544Szelenkov@nginx.com    def test_perl_application_header_pairs(self):
149544Szelenkov@nginx.com        self.load('header_pairs')
150544Szelenkov@nginx.com
1511596Szelenkov@nginx.com        assert self.get()['headers']['blah'] == 'blah', 'header pairs'
152544Szelenkov@nginx.com
153544Szelenkov@nginx.com    def test_perl_application_body_empty(self):
154544Szelenkov@nginx.com        self.load('body_empty')
155544Szelenkov@nginx.com
1561596Szelenkov@nginx.com        assert self.get()['body'] == '', 'body empty'
157544Szelenkov@nginx.com
158544Szelenkov@nginx.com    def test_perl_application_body_array(self):
159544Szelenkov@nginx.com        self.load('body_array')
160544Szelenkov@nginx.com
1611596Szelenkov@nginx.com        assert self.get()['body'] == '0123456789', 'body array'
162544Szelenkov@nginx.com
163544Szelenkov@nginx.com    def test_perl_application_body_large(self):
164544Szelenkov@nginx.com        self.load('variables')
165544Szelenkov@nginx.com
166544Szelenkov@nginx.com        body = '0123456789' * 1000
167544Szelenkov@nginx.com
168554Szelenkov@nginx.com        resp = self.post(body=body)['body']
169544Szelenkov@nginx.com
1701596Szelenkov@nginx.com        assert resp == body, 'body large'
171544Szelenkov@nginx.com
172544Szelenkov@nginx.com    def test_perl_application_body_io_empty(self):
173544Szelenkov@nginx.com        self.load('body_io_empty')
174544Szelenkov@nginx.com
1751596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'body io empty'
176544Szelenkov@nginx.com
177544Szelenkov@nginx.com    def test_perl_application_body_io_file(self):
178544Szelenkov@nginx.com        self.load('body_io_file')
179544Szelenkov@nginx.com
1801596Szelenkov@nginx.com        assert self.get()['body'] == 'body\n', 'body io file'
181544Szelenkov@nginx.com
1821596Szelenkov@nginx.com    @pytest.mark.skip('not yet')
1831736Szelenkov@nginx.com    def test_perl_application_syntax_error(self, skip_alert):
1841596Szelenkov@nginx.com        skip_alert(r'PSGI: Failed to parse script')
185607Szelenkov@nginx.com        self.load('syntax_error')
186607Szelenkov@nginx.com
1871596Szelenkov@nginx.com        assert self.get()['status'] == 500, 'syntax error'
188607Szelenkov@nginx.com
189581Szelenkov@nginx.com    def test_perl_keepalive_body(self):
190581Szelenkov@nginx.com        self.load('variables')
191581Szelenkov@nginx.com
1921596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'init'
1931029Szelenkov@nginx.com
1941453Szelenkov@nginx.com        body = '0123456789' * 500
1951017Szelenkov@nginx.com        (resp, sock) = self.post(
1961017Szelenkov@nginx.com            headers={
1971017Szelenkov@nginx.com                'Host': 'localhost',
1981017Szelenkov@nginx.com                'Connection': 'keep-alive',
1991017Szelenkov@nginx.com                'Content-Type': 'text/html',
2001017Szelenkov@nginx.com            },
2011017Szelenkov@nginx.com            start=True,
2021453Szelenkov@nginx.com            body=body,
2031029Szelenkov@nginx.com            read_timeout=1,
2041017Szelenkov@nginx.com        )
205581Szelenkov@nginx.com
2061596Szelenkov@nginx.com        assert resp['body'] == body, 'keep-alive 1'
207581Szelenkov@nginx.com
2081453Szelenkov@nginx.com        body = '0123456789'
2091017Szelenkov@nginx.com        resp = self.post(
2101017Szelenkov@nginx.com            headers={
2111017Szelenkov@nginx.com                'Host': 'localhost',
2121017Szelenkov@nginx.com                'Connection': 'close',
2131017Szelenkov@nginx.com                'Content-Type': 'text/html',
2141017Szelenkov@nginx.com            },
2151017Szelenkov@nginx.com            sock=sock,
2161453Szelenkov@nginx.com            body=body,
2171017Szelenkov@nginx.com        )
218581Szelenkov@nginx.com
2191596Szelenkov@nginx.com        assert resp['body'] == body, 'keep-alive 2'
220581Szelenkov@nginx.com
221916Szelenkov@nginx.com    def test_perl_body_io_fake(self):
222916Szelenkov@nginx.com        self.load('body_io_fake')
223916Szelenkov@nginx.com
2241596Szelenkov@nginx.com        assert self.get()['body'] == '21', 'body io fake'
225916Szelenkov@nginx.com
2261596Szelenkov@nginx.com        assert (
2271596Szelenkov@nginx.com            self.wait_for_record(r'\[error\].+IOFake getline\(\) \$\/ is \d+')
2281596Szelenkov@nginx.com            is not None
2291596Szelenkov@nginx.com        ), 'body io fake $/ value'
230916Szelenkov@nginx.com
2311596Szelenkov@nginx.com        assert (
2321596Szelenkov@nginx.com            self.wait_for_record(r'\[error\].+IOFake close\(\) called')
2331596Szelenkov@nginx.com            is not None
2341596Szelenkov@nginx.com        ), 'body io fake close'
235916Szelenkov@nginx.com
236988Szelenkov@nginx.com    def test_perl_delayed_response(self):
237988Szelenkov@nginx.com        self.load('delayed_response')
238988Szelenkov@nginx.com
239988Szelenkov@nginx.com        resp = self.get()
240988Szelenkov@nginx.com
2411596Szelenkov@nginx.com        assert resp['status'] == 200, 'status'
2421596Szelenkov@nginx.com        assert resp['body'] == 'Hello World!', 'body'
243988Szelenkov@nginx.com
244988Szelenkov@nginx.com    def test_perl_streaming_body(self):
245988Szelenkov@nginx.com        self.load('streaming_body')
246988Szelenkov@nginx.com
247988Szelenkov@nginx.com        resp = self.get()
248988Szelenkov@nginx.com
2491596Szelenkov@nginx.com        assert resp['status'] == 200, 'status'
2501596Szelenkov@nginx.com        assert resp['body'] == 'Hello World!', 'body'
2511690Smax.romanov@nginx.com
2521690Smax.romanov@nginx.com    def test_perl_application_threads(self):
2531690Smax.romanov@nginx.com        self.load('threads')
2541690Smax.romanov@nginx.com
2551690Smax.romanov@nginx.com        assert 'success' in self.conf(
2561690Smax.romanov@nginx.com            '4', 'applications/threads/threads'
2571690Smax.romanov@nginx.com        ), 'configure 4 threads'
2581690Smax.romanov@nginx.com
2591690Smax.romanov@nginx.com        socks = []
2601690Smax.romanov@nginx.com
2611690Smax.romanov@nginx.com        for i in range(4):
2621690Smax.romanov@nginx.com            (_, sock) = self.get(
2631690Smax.romanov@nginx.com                headers={
2641690Smax.romanov@nginx.com                    'Host': 'localhost',
2651690Smax.romanov@nginx.com                    'X-Delay': '2',
2661690Smax.romanov@nginx.com                    'Connection': 'close',
2671690Smax.romanov@nginx.com                },
2681690Smax.romanov@nginx.com                no_recv=True,
2691690Smax.romanov@nginx.com                start=True,
2701690Smax.romanov@nginx.com            )
2711690Smax.romanov@nginx.com
2721690Smax.romanov@nginx.com            socks.append(sock)
2731690Smax.romanov@nginx.com
2741690Smax.romanov@nginx.com        threads = set()
2751690Smax.romanov@nginx.com
2761690Smax.romanov@nginx.com        for sock in socks:
2771690Smax.romanov@nginx.com            resp = self.recvall(sock).decode('utf-8')
2781690Smax.romanov@nginx.com
2791690Smax.romanov@nginx.com            self.log_in(resp)
2801690Smax.romanov@nginx.com
2811690Smax.romanov@nginx.com            resp = self._resp_to_dict(resp)
2821690Smax.romanov@nginx.com
2831690Smax.romanov@nginx.com            assert resp['status'] == 200, 'status'
2841690Smax.romanov@nginx.com
2851690Smax.romanov@nginx.com            threads.add(resp['headers']['X-Thread'])
2861690Smax.romanov@nginx.com
2871690Smax.romanov@nginx.com            assert resp['headers']['Psgi-Multithread'] == '1', 'multithread'
2881690Smax.romanov@nginx.com
2891690Smax.romanov@nginx.com            sock.close()
2901690Smax.romanov@nginx.com
2911690Smax.romanov@nginx.com        assert len(socks) == len(threads), 'threads differs'
292