xref: /unit/test/test_node_application.py (revision 2073:bc6ad31ce286)
11596Szelenkov@nginx.comimport re
21477Szelenkov@nginx.com
31635Szelenkov@nginx.comimport pytest
41019Szelenkov@nginx.comfrom unit.applications.lang.node import TestApplicationNode
51735Szelenkov@nginx.comfrom unit.utils import waitforfiles
6855Spluknet@nginx.com
71017Szelenkov@nginx.com
81019Szelenkov@nginx.comclass TestNodeApplication(TestApplicationNode):
91467Szelenkov@nginx.com    prerequisites = {'modules': {'node': 'all'}}
10855Spluknet@nginx.com
111865So.canty@f5.com    def assert_basic_application(self):
121865So.canty@f5.com        resp = self.get()
131865So.canty@f5.com        assert resp['headers']['Content-Type'] == 'text/plain', 'basic header'
141865So.canty@f5.com        assert resp['body'] == 'Hello World\n', 'basic body'
151865So.canty@f5.com
16855Spluknet@nginx.com    def test_node_application_basic(self):
17855Spluknet@nginx.com        self.load('basic')
18855Spluknet@nginx.com
191865So.canty@f5.com        self.assert_basic_application()
201865So.canty@f5.com
211876So.canty@f5.com    def test_node_application_loader_unit_http(self):
221876So.canty@f5.com        self.load('loader/unit_http')
231865So.canty@f5.com
241865So.canty@f5.com        self.assert_basic_application()
251865So.canty@f5.com
261876So.canty@f5.com    def test_node_application_loader_transitive_dependency(self):
271876So.canty@f5.com        self.load('loader/transitive_dependency')
281865So.canty@f5.com
291865So.canty@f5.com        self.assert_basic_application()
30855Spluknet@nginx.com
31855Spluknet@nginx.com    def test_node_application_seq(self):
32855Spluknet@nginx.com        self.load('basic')
33855Spluknet@nginx.com
341596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'seq'
351596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'seq 2'
36855Spluknet@nginx.com
37855Spluknet@nginx.com    def test_node_application_variables(self):
38855Spluknet@nginx.com        self.load('variables')
39855Spluknet@nginx.com
40855Spluknet@nginx.com        body = 'Test body string.'
41855Spluknet@nginx.com
421017Szelenkov@nginx.com        resp = self.post(
431017Szelenkov@nginx.com            headers={
441017Szelenkov@nginx.com                'Host': 'localhost',
451017Szelenkov@nginx.com                'Content-Type': 'text/html',
461017Szelenkov@nginx.com                'Custom-Header': 'blah',
471017Szelenkov@nginx.com                'Connection': 'close',
481017Szelenkov@nginx.com            },
491017Szelenkov@nginx.com            body=body,
501017Szelenkov@nginx.com        )
51855Spluknet@nginx.com
521596Szelenkov@nginx.com        assert resp['status'] == 200, 'status'
53855Spluknet@nginx.com        headers = resp['headers']
54855Spluknet@nginx.com        header_server = headers.pop('Server')
551596Szelenkov@nginx.com        assert re.search(r'Unit/[\d\.]+', header_server), 'server header'
56855Spluknet@nginx.com
57855Spluknet@nginx.com        date = headers.pop('Date')
581596Szelenkov@nginx.com        assert date[-4:] == ' GMT', 'date header timezone'
591596Szelenkov@nginx.com        assert (
601596Szelenkov@nginx.com            abs(self.date_to_sec_epoch(date) - self.sec_epoch()) < 5
611596Szelenkov@nginx.com        ), 'date header'
62855Spluknet@nginx.com
63855Spluknet@nginx.com        raw_headers = headers.pop('Request-Raw-Headers')
641596Szelenkov@nginx.com        assert re.search(
651017Szelenkov@nginx.com            r'^(?:Host|localhost|Content-Type|'
661635Szelenkov@nginx.com            r'text\/html|Custom-Header|blah|Content-Length|17|Connection|'
671635Szelenkov@nginx.com            r'close|,)+$',
681596Szelenkov@nginx.com            raw_headers,
691596Szelenkov@nginx.com        ), 'raw headers'
70855Spluknet@nginx.com
711596Szelenkov@nginx.com        assert headers == {
721596Szelenkov@nginx.com            'Connection': 'close',
731596Szelenkov@nginx.com            'Content-Length': str(len(body)),
741596Szelenkov@nginx.com            'Content-Type': 'text/html',
751596Szelenkov@nginx.com            'Request-Method': 'POST',
761596Szelenkov@nginx.com            'Request-Uri': '/',
771596Szelenkov@nginx.com            'Http-Host': 'localhost',
781596Szelenkov@nginx.com            'Server-Protocol': 'HTTP/1.1',
791596Szelenkov@nginx.com            'Custom-Header': 'blah',
801596Szelenkov@nginx.com        }, 'headers'
811596Szelenkov@nginx.com        assert resp['body'] == body, 'body'
82855Spluknet@nginx.com
83855Spluknet@nginx.com    def test_node_application_get_variables(self):
84855Spluknet@nginx.com        self.load('get_variables')
85855Spluknet@nginx.com
86855Spluknet@nginx.com        resp = self.get(url='/?var1=val1&var2=&var3')
871596Szelenkov@nginx.com        assert resp['headers']['X-Var-1'] == 'val1', 'GET variables'
881596Szelenkov@nginx.com        assert resp['headers']['X-Var-2'] == '', 'GET variables 2'
891596Szelenkov@nginx.com        assert resp['headers']['X-Var-3'] == '', 'GET variables 3'
90855Spluknet@nginx.com
91855Spluknet@nginx.com    def test_node_application_post_variables(self):
92855Spluknet@nginx.com        self.load('post_variables')
93855Spluknet@nginx.com
941017Szelenkov@nginx.com        resp = self.post(
951017Szelenkov@nginx.com            headers={
961017Szelenkov@nginx.com                'Content-Type': 'application/x-www-form-urlencoded',
971017Szelenkov@nginx.com                'Host': 'localhost',
981017Szelenkov@nginx.com                'Connection': 'close',
991017Szelenkov@nginx.com            },
1001017Szelenkov@nginx.com            body='var1=val1&var2=&var3',
1011017Szelenkov@nginx.com        )
102855Spluknet@nginx.com
1031596Szelenkov@nginx.com        assert resp['headers']['X-Var-1'] == 'val1', 'POST variables'
1041596Szelenkov@nginx.com        assert resp['headers']['X-Var-2'] == '', 'POST variables 2'
1051596Szelenkov@nginx.com        assert resp['headers']['X-Var-3'] == '', 'POST variables 3'
106855Spluknet@nginx.com
107855Spluknet@nginx.com    def test_node_application_404(self):
108855Spluknet@nginx.com        self.load('404')
109855Spluknet@nginx.com
110855Spluknet@nginx.com        resp = self.get()
111855Spluknet@nginx.com
1121596Szelenkov@nginx.com        assert resp['status'] == 404, '404 status'
1131596Szelenkov@nginx.com        assert re.search(
1141596Szelenkov@nginx.com            r'<title>404 Not Found</title>', resp['body']
1151596Szelenkov@nginx.com        ), '404 body'
116855Spluknet@nginx.com
117855Spluknet@nginx.com    def test_node_keepalive_body(self):
118855Spluknet@nginx.com        self.load('mirror')
119855Spluknet@nginx.com
1201596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'init'
1211029Szelenkov@nginx.com
1221453Szelenkov@nginx.com        body = '0123456789' * 500
1231017Szelenkov@nginx.com        (resp, sock) = self.post(
1241017Szelenkov@nginx.com            headers={
1251017Szelenkov@nginx.com                'Host': 'localhost',
1261017Szelenkov@nginx.com                'Connection': 'keep-alive',
1271017Szelenkov@nginx.com                'Content-Type': 'text/html',
1281017Szelenkov@nginx.com            },
1291017Szelenkov@nginx.com            start=True,
1301453Szelenkov@nginx.com            body=body,
1311029Szelenkov@nginx.com            read_timeout=1,
1321017Szelenkov@nginx.com        )
133855Spluknet@nginx.com
1341596Szelenkov@nginx.com        assert resp['body'] == '0123456789' * 500, 'keep-alive 1'
135855Spluknet@nginx.com
1361453Szelenkov@nginx.com        body = '0123456789'
1371017Szelenkov@nginx.com        resp = self.post(
1381017Szelenkov@nginx.com            headers={
1391017Szelenkov@nginx.com                'Host': 'localhost',
1401017Szelenkov@nginx.com                'Connection': 'close',
1411017Szelenkov@nginx.com                'Content-Type': 'text/html',
1421017Szelenkov@nginx.com            },
1431017Szelenkov@nginx.com            sock=sock,
1441453Szelenkov@nginx.com            body=body,
1451017Szelenkov@nginx.com        )
146855Spluknet@nginx.com
1471596Szelenkov@nginx.com        assert resp['body'] == body, 'keep-alive 2'
148855Spluknet@nginx.com
149855Spluknet@nginx.com    def test_node_application_write_buffer(self):
150855Spluknet@nginx.com        self.load('write_buffer')
151855Spluknet@nginx.com
1521596Szelenkov@nginx.com        assert self.get()['body'] == 'buffer', 'write buffer'
153855Spluknet@nginx.com
1541654Szelenkov@nginx.com    def test_node_application_write_callback(self, temp_dir):
155855Spluknet@nginx.com        self.load('write_callback')
156855Spluknet@nginx.com
1571596Szelenkov@nginx.com        assert self.get()['body'] == 'helloworld', 'write callback order'
1581654Szelenkov@nginx.com        assert waitforfiles(temp_dir + '/node/callback'), 'write callback'
159855Spluknet@nginx.com
160896Szelenkov@nginx.com    def test_node_application_write_before_write_head(self):
161855Spluknet@nginx.com        self.load('write_before_write_head')
162855Spluknet@nginx.com
1631596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'write before writeHead'
164855Spluknet@nginx.com
165866Szelenkov@nginx.com    def test_node_application_double_end(self):
166866Szelenkov@nginx.com        self.load('double_end')
167866Szelenkov@nginx.com
1681596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'double end'
1691596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'double end 2'
170866Szelenkov@nginx.com
171855Spluknet@nginx.com    def test_node_application_write_return(self):
172855Spluknet@nginx.com        self.load('write_return')
173855Spluknet@nginx.com
1741596Szelenkov@nginx.com        assert self.get()['body'] == 'bodytrue', 'write return'
175855Spluknet@nginx.com
176855Spluknet@nginx.com    def test_node_application_remove_header(self):
177855Spluknet@nginx.com        self.load('remove_header')
178855Spluknet@nginx.com
1791017Szelenkov@nginx.com        resp = self.get(
1801017Szelenkov@nginx.com            headers={
1811017Szelenkov@nginx.com                'Host': 'localhost',
1821017Szelenkov@nginx.com                'X-Remove': 'X-Header',
1831017Szelenkov@nginx.com                'Connection': 'close',
1841017Szelenkov@nginx.com            }
1851017Szelenkov@nginx.com        )
1861596Szelenkov@nginx.com        assert resp['headers']['Was-Header'] == 'true', 'was header'
1871596Szelenkov@nginx.com        assert resp['headers']['Has-Header'] == 'false', 'has header'
1881596Szelenkov@nginx.com        assert not ('X-Header' in resp['headers']), 'remove header'
189855Spluknet@nginx.com
190866Szelenkov@nginx.com    def test_node_application_remove_header_nonexisting(self):
191866Szelenkov@nginx.com        self.load('remove_header')
192866Szelenkov@nginx.com
1931596Szelenkov@nginx.com        assert (
1941017Szelenkov@nginx.com            self.get(
1951017Szelenkov@nginx.com                headers={
1961017Szelenkov@nginx.com                    'Host': 'localhost',
1971017Szelenkov@nginx.com                    'X-Remove': 'blah',
1981017Szelenkov@nginx.com                    'Connection': 'close',
1991017Szelenkov@nginx.com                }
2001596Szelenkov@nginx.com            )['headers']['Has-Header']
2011596Szelenkov@nginx.com            == 'true'
2021596Szelenkov@nginx.com        ), 'remove header nonexisting'
203866Szelenkov@nginx.com
204855Spluknet@nginx.com    def test_node_application_update_header(self):
205855Spluknet@nginx.com        self.load('update_header')
206855Spluknet@nginx.com
2071596Szelenkov@nginx.com        assert self.get()['headers']['X-Header'] == 'new', 'update header'
208855Spluknet@nginx.com
209855Spluknet@nginx.com    def test_node_application_set_header_array(self):
210855Spluknet@nginx.com        self.load('set_header_array')
211855Spluknet@nginx.com
2121596Szelenkov@nginx.com        assert self.get()['headers']['Set-Cookie'] == [
2131596Szelenkov@nginx.com            'tc=one,two,three',
2141596Szelenkov@nginx.com            'tc=four,five,six',
2151596Szelenkov@nginx.com        ], 'set header array'
216855Spluknet@nginx.com
2171596Szelenkov@nginx.com    @pytest.mark.skip('not yet')
218855Spluknet@nginx.com    def test_node_application_status_message(self):
219855Spluknet@nginx.com        self.load('status_message')
220855Spluknet@nginx.com
221*2073Szelenkov@nginx.com        assert re.search(r'200 blah', self.get(raw_resp=True)), 'status message'
222855Spluknet@nginx.com
223855Spluknet@nginx.com    def test_node_application_get_header_type(self):
224855Spluknet@nginx.com        self.load('get_header_type')
225855Spluknet@nginx.com
2261596Szelenkov@nginx.com        assert self.get()['headers']['X-Type'] == 'number', 'get header type'
227855Spluknet@nginx.com
228866Szelenkov@nginx.com    def test_node_application_header_name_case(self):
229866Szelenkov@nginx.com        self.load('header_name_case')
230866Szelenkov@nginx.com
231866Szelenkov@nginx.com        headers = self.get()['headers']
232866Szelenkov@nginx.com
2331596Szelenkov@nginx.com        assert headers['X-HEADER'] == '3', 'header value'
2341596Szelenkov@nginx.com        assert 'X-Header' not in headers, 'insensitive'
2351596Szelenkov@nginx.com        assert 'X-header' not in headers, 'insensitive 2'
236866Szelenkov@nginx.com
237866Szelenkov@nginx.com    def test_node_application_promise_handler_write_after_end(self):
238866Szelenkov@nginx.com        self.load('promise_handler')
239866Szelenkov@nginx.com
2401596Szelenkov@nginx.com        assert (
2411017Szelenkov@nginx.com            self.post(
2421017Szelenkov@nginx.com                headers={
2431017Szelenkov@nginx.com                    'Host': 'localhost',
2441017Szelenkov@nginx.com                    'Content-Type': 'text/html',
2451017Szelenkov@nginx.com                    'X-Write-Call': '1',
2461017Szelenkov@nginx.com                    'Connection': 'close',
2471017Szelenkov@nginx.com                },
2481017Szelenkov@nginx.com                body='callback',
2491596Szelenkov@nginx.com            )['status']
2501596Szelenkov@nginx.com            == 200
2511596Szelenkov@nginx.com        ), 'promise handler request write after end'
252866Szelenkov@nginx.com
2531654Szelenkov@nginx.com    def test_node_application_promise_end(self, temp_dir):
254866Szelenkov@nginx.com        self.load('promise_end')
255866Szelenkov@nginx.com
2561596Szelenkov@nginx.com        assert (
2571017Szelenkov@nginx.com            self.post(
2581017Szelenkov@nginx.com                headers={
2591017Szelenkov@nginx.com                    'Host': 'localhost',
2601017Szelenkov@nginx.com                    'Content-Type': 'text/html',
2611017Szelenkov@nginx.com                    'Connection': 'close',
2621017Szelenkov@nginx.com                },
2631017Szelenkov@nginx.com                body='end',
2641596Szelenkov@nginx.com            )['status']
2651596Szelenkov@nginx.com            == 200
2661596Szelenkov@nginx.com        ), 'promise end request'
2671654Szelenkov@nginx.com        assert waitforfiles(temp_dir + '/node/callback'), 'promise end'
268866Szelenkov@nginx.com
2691596Szelenkov@nginx.com    @pytest.mark.skip('not yet')
270866Szelenkov@nginx.com    def test_node_application_header_name_valid(self):
271866Szelenkov@nginx.com        self.load('header_name_valid')
272866Szelenkov@nginx.com
2731596Szelenkov@nginx.com        assert 'status' not in self.get(), 'header name valid'
274866Szelenkov@nginx.com
275866Szelenkov@nginx.com    def test_node_application_header_value_object(self):
276866Szelenkov@nginx.com        self.load('header_value_object')
277866Szelenkov@nginx.com
2781596Szelenkov@nginx.com        assert 'X-Header' in self.get()['headers'], 'header value object'
279866Szelenkov@nginx.com
280866Szelenkov@nginx.com    def test_node_application_get_header_names(self):
281866Szelenkov@nginx.com        self.load('get_header_names')
282866Szelenkov@nginx.com
2831596Szelenkov@nginx.com        assert self.get()['headers']['X-Names'] == [
2841596Szelenkov@nginx.com            'date',
2851596Szelenkov@nginx.com            'x-header',
2861596Szelenkov@nginx.com        ], 'get header names'
287866Szelenkov@nginx.com
288866Szelenkov@nginx.com    def test_node_application_has_header(self):
289866Szelenkov@nginx.com        self.load('has_header')
290866Szelenkov@nginx.com
2911596Szelenkov@nginx.com        assert (
2921017Szelenkov@nginx.com            self.get(
2931017Szelenkov@nginx.com                headers={
2941017Szelenkov@nginx.com                    'Host': 'localhost',
2951017Szelenkov@nginx.com                    'X-Header': 'length',
2961017Szelenkov@nginx.com                    'Connection': 'close',
2971017Szelenkov@nginx.com                }
2981596Szelenkov@nginx.com            )['headers']['X-Has-Header']
2991596Szelenkov@nginx.com            == 'false'
3001596Szelenkov@nginx.com        ), 'has header length'
301866Szelenkov@nginx.com
3021596Szelenkov@nginx.com        assert (
3031017Szelenkov@nginx.com            self.get(
3041017Szelenkov@nginx.com                headers={
3051017Szelenkov@nginx.com                    'Host': 'localhost',
3061017Szelenkov@nginx.com                    'X-Header': 'Date',
3071017Szelenkov@nginx.com                    'Connection': 'close',
3081017Szelenkov@nginx.com                }
3091596Szelenkov@nginx.com            )['headers']['X-Has-Header']
3101596Szelenkov@nginx.com            == 'false'
3111596Szelenkov@nginx.com        ), 'has header date'
312866Szelenkov@nginx.com
313866Szelenkov@nginx.com    def test_node_application_write_multiple(self):
314866Szelenkov@nginx.com        self.load('write_multiple')
315866Szelenkov@nginx.com
3161596Szelenkov@nginx.com        assert self.get()['body'] == 'writewrite2end', 'write multiple'
317