diff options
Diffstat (limited to 'test/test_node_application.py')
-rw-r--r-- | test/test_node_application.py | 527 |
1 files changed, 276 insertions, 251 deletions
diff --git a/test/test_node_application.py b/test/test_node_application.py index 719afae8..e4226535 100644 --- a/test/test_node_application.py +++ b/test/test_node_application.py @@ -1,307 +1,332 @@ import re import pytest -from unit.applications.lang.node import TestApplicationNode +from unit.applications.lang.node import ApplicationNode from unit.utils import waitforfiles +prerequisites = {'modules': {'node': 'all'}} -class TestNodeApplication(TestApplicationNode): - prerequisites = {'modules': {'node': 'all'}} +client = ApplicationNode() - def assert_basic_application(self): - resp = self.get() - assert resp['headers']['Content-Type'] == 'text/plain', 'basic header' - assert resp['body'] == 'Hello World\n', 'basic body' - def test_node_application_basic(self): - self.load('basic') +def assert_basic_application(): + resp = client.get() + assert resp['headers']['Content-Type'] == 'text/plain', 'basic header' + assert resp['body'] == 'Hello World\n', 'basic body' - self.assert_basic_application() - def test_node_application_loader_unit_http(self): - self.load('loader/unit_http') +def test_node_application_basic(): + client.load('basic') - self.assert_basic_application() + assert_basic_application() - def test_node_application_loader_transitive_dependency(self): - self.load('loader/transitive_dependency') - self.assert_basic_application() +def test_node_application_loader_unit_http(): + client.load('loader/unit_http') - def test_node_application_seq(self): - self.load('basic') + assert_basic_application() - assert self.get()['status'] == 200, 'seq' - assert self.get()['status'] == 200, 'seq 2' - def test_node_application_variables(self): - self.load('variables') +def test_node_application_loader_transitive_dependency(): + client.load('loader/transitive_dependency') - body = 'Test body string.' + assert_basic_application() - resp = self.post( - headers={ - 'Host': 'localhost', - 'Content-Type': 'text/html', - 'Custom-Header': 'blah', - 'Connection': 'close', - }, - body=body, - ) - - assert resp['status'] == 200, 'status' - headers = resp['headers'] - header_server = headers.pop('Server') - assert re.search(r'Unit/[\d\.]+', header_server), 'server header' - - date = headers.pop('Date') - assert date[-4:] == ' GMT', 'date header timezone' - assert ( - abs(self.date_to_sec_epoch(date) - self.sec_epoch()) < 5 - ), 'date header' - - raw_headers = headers.pop('Request-Raw-Headers') - assert re.search( - r'^(?:Host|localhost|Content-Type|' - r'text\/html|Custom-Header|blah|Content-Length|17|Connection|' - r'close|,)+$', - raw_headers, - ), 'raw headers' - - assert headers == { - 'Connection': 'close', - 'Content-Length': str(len(body)), + +def test_node_application_seq(): + client.load('basic') + + assert client.get()['status'] == 200, 'seq' + assert client.get()['status'] == 200, 'seq 2' + + +def test_node_application_variables(date_to_sec_epoch, sec_epoch): + client.load('variables') + + body = 'Test body string.' + + resp = client.post( + headers={ + 'Host': 'localhost', 'Content-Type': 'text/html', - 'Request-Method': 'POST', - 'Request-Uri': '/', - 'Http-Host': 'localhost', - 'Server-Protocol': 'HTTP/1.1', 'Custom-Header': 'blah', - }, 'headers' - assert resp['body'] == body, 'body' + 'Connection': 'close', + }, + body=body, + ) + + assert resp['status'] == 200, 'status' + headers = resp['headers'] + header_server = headers.pop('Server') + assert re.search(r'Unit/[\d\.]+', header_server), 'server header' + + date = headers.pop('Date') + assert date[-4:] == ' GMT', 'date header timezone' + assert abs(date_to_sec_epoch(date) - sec_epoch) < 5, 'date header' + + raw_headers = headers.pop('Request-Raw-Headers') + assert re.search( + r'^(?:Host|localhost|Content-Type|' + r'text\/html|Custom-Header|blah|Content-Length|17|Connection|' + r'close|,)+$', + raw_headers, + ), 'raw headers' + + assert headers == { + 'Connection': 'close', + 'Content-Length': str(len(body)), + 'Content-Type': 'text/html', + 'Request-Method': 'POST', + 'Request-Uri': '/', + 'Http-Host': 'localhost', + 'Server-Protocol': 'HTTP/1.1', + 'Custom-Header': 'blah', + }, 'headers' + assert resp['body'] == body, 'body' + + +def test_node_application_get_variables(): + client.load('get_variables') + + resp = client.get(url='/?var1=val1&var2=&var3') + assert resp['headers']['X-Var-1'] == 'val1', 'GET variables' + assert resp['headers']['X-Var-2'] == '', 'GET variables 2' + assert resp['headers']['X-Var-3'] == '', 'GET variables 3' + + +def test_node_application_post_variables(): + client.load('post_variables') + + resp = client.post( + headers={ + 'Content-Type': 'application/x-www-form-urlencoded', + 'Host': 'localhost', + 'Connection': 'close', + }, + body='var1=val1&var2=&var3', + ) + + assert resp['headers']['X-Var-1'] == 'val1', 'POST variables' + assert resp['headers']['X-Var-2'] == '', 'POST variables 2' + assert resp['headers']['X-Var-3'] == '', 'POST variables 3' + + +def test_node_application_404(): + client.load('404') + + resp = client.get() + + assert resp['status'] == 404, '404 status' + assert re.search(r'<title>404 Not Found</title>', resp['body']), '404 body' + + +def test_node_keepalive_body(): + client.load('mirror') + + assert client.get()['status'] == 200, 'init' + + body = '0123456789' * 500 + (resp, sock) = client.post( + headers={ + 'Host': 'localhost', + 'Connection': 'keep-alive', + }, + start=True, + body=body, + read_timeout=1, + ) + + assert resp['body'] == '0123456789' * 500, 'keep-alive 1' + + body = '0123456789' + resp = client.post(sock=sock, body=body) + + assert resp['body'] == body, 'keep-alive 2' + + +def test_node_application_write_buffer(): + client.load('write_buffer') + + assert client.get()['body'] == 'buffer', 'write buffer' + + +def test_node_application_write_callback(temp_dir): + client.load('write_callback') + + assert client.get()['body'] == 'helloworld', 'write callback order' + assert waitforfiles(f'{temp_dir}/node/callback'), 'write callback' + + +def test_node_application_write_before_write_head(): + client.load('write_before_write_head') + + assert client.get()['status'] == 200, 'write before writeHead' + + +def test_node_application_double_end(): + client.load('double_end') - def test_node_application_get_variables(self): - self.load('get_variables') + assert client.get()['status'] == 200, 'double end' + assert client.get()['status'] == 200, 'double end 2' - resp = self.get(url='/?var1=val1&var2=&var3') - assert resp['headers']['X-Var-1'] == 'val1', 'GET variables' - assert resp['headers']['X-Var-2'] == '', 'GET variables 2' - assert resp['headers']['X-Var-3'] == '', 'GET variables 3' - def test_node_application_post_variables(self): - self.load('post_variables') +def test_node_application_write_return(): + client.load('write_return') - resp = self.post( + assert client.get()['body'] == 'bodytrue', 'write return' + + +def test_node_application_remove_header(): + client.load('remove_header') + + resp = client.get( + headers={ + 'Host': 'localhost', + 'X-Remove': 'X-Header', + 'Connection': 'close', + } + ) + assert resp['headers']['Was-Header'] == 'true', 'was header' + assert resp['headers']['Has-Header'] == 'false', 'has header' + assert not ('X-Header' in resp['headers']), 'remove header' + + +def test_node_application_remove_header_nonexisting(): + client.load('remove_header') + + assert ( + client.get( headers={ - 'Content-Type': 'application/x-www-form-urlencoded', 'Host': 'localhost', + 'X-Remove': 'blah', 'Connection': 'close', - }, - body='var1=val1&var2=&var3', - ) + } + )['headers']['Has-Header'] + == 'true' + ), 'remove header nonexisting' + + +def test_node_application_update_header(): + client.load('update_header') + + assert client.get()['headers']['X-Header'] == 'new', 'update header' + + +def test_node_application_set_header_array(): + client.load('set_header_array') + + assert client.get()['headers']['Set-Cookie'] == [ + 'tc=one,two,three', + 'tc=four,five,six', + ], 'set header array' + + +@pytest.mark.skip('not yet') +def test_node_application_status_message(): + client.load('status_message') + + assert re.search(r'200 blah', client.get(raw_resp=True)), 'status message' - assert resp['headers']['X-Var-1'] == 'val1', 'POST variables' - assert resp['headers']['X-Var-2'] == '', 'POST variables 2' - assert resp['headers']['X-Var-3'] == '', 'POST variables 3' - def test_node_application_404(self): - self.load('404') +def test_node_application_get_header_type(): + client.load('get_header_type') - resp = self.get() + assert client.get()['headers']['X-Type'] == 'number', 'get header type' - assert resp['status'] == 404, '404 status' - assert re.search( - r'<title>404 Not Found</title>', resp['body'] - ), '404 body' - def test_node_keepalive_body(self): - self.load('mirror') +def test_node_application_header_name_case(): + client.load('header_name_case') - assert self.get()['status'] == 200, 'init' + headers = client.get()['headers'] - body = '0123456789' * 500 - (resp, sock) = self.post( + assert headers['X-HEADER'] == '3', 'header value' + assert 'X-Header' not in headers, 'insensitive' + assert 'X-header' not in headers, 'insensitive 2' + + +def test_node_application_promise_handler_write_after_end(): + client.load('promise_handler') + + assert ( + client.post( headers={ 'Host': 'localhost', - 'Connection': 'keep-alive', + 'Content-Type': 'text/html', + 'X-Write-Call': '1', + 'Connection': 'close', }, - start=True, - body=body, - read_timeout=1, - ) + body='callback', + )['status'] + == 200 + ), 'promise handler request write after end' - assert resp['body'] == '0123456789' * 500, 'keep-alive 1' - body = '0123456789' - resp = self.post(sock=sock, body=body) +def test_node_application_promise_end(temp_dir): + client.load('promise_end') + + assert ( + client.post( + headers={ + 'Host': 'localhost', + 'Content-Type': 'text/html', + 'Connection': 'close', + }, + body='end', + )['status'] + == 200 + ), 'promise end request' + assert waitforfiles(f'{temp_dir}/node/callback'), 'promise end' - assert resp['body'] == body, 'keep-alive 2' - def test_node_application_write_buffer(self): - self.load('write_buffer') +@pytest.mark.skip('not yet') +def test_node_application_header_name_valid(): + client.load('header_name_valid') - assert self.get()['body'] == 'buffer', 'write buffer' + assert 'status' not in client.get(), 'header name valid' - def test_node_application_write_callback(self, temp_dir): - self.load('write_callback') - assert self.get()['body'] == 'helloworld', 'write callback order' - assert waitforfiles(f'{temp_dir}/node/callback'), 'write callback' +def test_node_application_header_value_object(): + client.load('header_value_object') - def test_node_application_write_before_write_head(self): - self.load('write_before_write_head') + assert 'X-Header' in client.get()['headers'], 'header value object' - assert self.get()['status'] == 200, 'write before writeHead' - def test_node_application_double_end(self): - self.load('double_end') +def test_node_application_get_header_names(): + client.load('get_header_names') - assert self.get()['status'] == 200, 'double end' - assert self.get()['status'] == 200, 'double end 2' + assert client.get()['headers']['X-Names'] == [ + 'date', + 'x-header', + ], 'get header names' - def test_node_application_write_return(self): - self.load('write_return') - assert self.get()['body'] == 'bodytrue', 'write return' +def test_node_application_has_header(): + client.load('has_header') - def test_node_application_remove_header(self): - self.load('remove_header') + assert ( + client.get( + headers={ + 'Host': 'localhost', + 'X-Header': 'length', + 'Connection': 'close', + } + )['headers']['X-Has-Header'] + == 'false' + ), 'has header length' - resp = self.get( + assert ( + client.get( headers={ 'Host': 'localhost', - 'X-Remove': 'X-Header', + 'X-Header': 'Date', 'Connection': 'close', } - ) - assert resp['headers']['Was-Header'] == 'true', 'was header' - assert resp['headers']['Has-Header'] == 'false', 'has header' - assert not ('X-Header' in resp['headers']), 'remove header' - - def test_node_application_remove_header_nonexisting(self): - self.load('remove_header') - - assert ( - self.get( - headers={ - 'Host': 'localhost', - 'X-Remove': 'blah', - 'Connection': 'close', - } - )['headers']['Has-Header'] - == 'true' - ), 'remove header nonexisting' - - def test_node_application_update_header(self): - self.load('update_header') - - assert self.get()['headers']['X-Header'] == 'new', 'update header' - - def test_node_application_set_header_array(self): - self.load('set_header_array') - - assert self.get()['headers']['Set-Cookie'] == [ - 'tc=one,two,three', - 'tc=four,five,six', - ], 'set header array' - - @pytest.mark.skip('not yet') - def test_node_application_status_message(self): - self.load('status_message') - - assert re.search(r'200 blah', self.get(raw_resp=True)), 'status message' - - def test_node_application_get_header_type(self): - self.load('get_header_type') - - assert self.get()['headers']['X-Type'] == 'number', 'get header type' - - def test_node_application_header_name_case(self): - self.load('header_name_case') - - headers = self.get()['headers'] - - assert headers['X-HEADER'] == '3', 'header value' - assert 'X-Header' not in headers, 'insensitive' - assert 'X-header' not in headers, 'insensitive 2' - - def test_node_application_promise_handler_write_after_end(self): - self.load('promise_handler') - - assert ( - self.post( - headers={ - 'Host': 'localhost', - 'Content-Type': 'text/html', - 'X-Write-Call': '1', - 'Connection': 'close', - }, - body='callback', - )['status'] - == 200 - ), 'promise handler request write after end' - - def test_node_application_promise_end(self, temp_dir): - self.load('promise_end') - - assert ( - self.post( - headers={ - 'Host': 'localhost', - 'Content-Type': 'text/html', - 'Connection': 'close', - }, - body='end', - )['status'] - == 200 - ), 'promise end request' - assert waitforfiles(f'{temp_dir}/node/callback'), 'promise end' - - @pytest.mark.skip('not yet') - def test_node_application_header_name_valid(self): - self.load('header_name_valid') - - assert 'status' not in self.get(), 'header name valid' - - def test_node_application_header_value_object(self): - self.load('header_value_object') - - assert 'X-Header' in self.get()['headers'], 'header value object' - - def test_node_application_get_header_names(self): - self.load('get_header_names') - - assert self.get()['headers']['X-Names'] == [ - 'date', - 'x-header', - ], 'get header names' - - def test_node_application_has_header(self): - self.load('has_header') - - assert ( - self.get( - headers={ - 'Host': 'localhost', - 'X-Header': 'length', - 'Connection': 'close', - } - )['headers']['X-Has-Header'] - == 'false' - ), 'has header length' - - assert ( - self.get( - headers={ - 'Host': 'localhost', - 'X-Header': 'Date', - 'Connection': 'close', - } - )['headers']['X-Has-Header'] - == 'false' - ), 'has header date' - - def test_node_application_write_multiple(self): - self.load('write_multiple') - - assert self.get()['body'] == 'writewrite2end', 'write multiple' + )['headers']['X-Has-Header'] + == 'false' + ), 'has header date' + + +def test_node_application_write_multiple(): + client.load('write_multiple') + + assert client.get()['body'] == 'writewrite2end', 'write multiple' |