summaryrefslogtreecommitdiffhomepage
path: root/test/test_node_application.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/test_node_application.py')
-rw-r--r--test/test_node_application.py251
1 files changed, 102 insertions, 149 deletions
diff --git a/test/test_node_application.py b/test/test_node_application.py
index e46cc6a1..c2b0ed69 100644
--- a/test/test_node_application.py
+++ b/test/test_node_application.py
@@ -1,6 +1,8 @@
-import unittest
+import pytest
+import re
from unit.applications.lang.node import TestApplicationNode
+from conftest import waitforfiles
class TestNodeApplication(TestApplicationNode):
@@ -10,16 +12,14 @@ class TestNodeApplication(TestApplicationNode):
self.load('basic')
resp = self.get()
- self.assertEqual(
- resp['headers']['Content-Type'], 'text/plain', 'basic header'
- )
- self.assertEqual(resp['body'], 'Hello World\n', 'basic body')
+ assert resp['headers']['Content-Type'] == 'text/plain', 'basic header'
+ assert resp['body'] == 'Hello World\n', 'basic body'
def test_node_application_seq(self):
self.load('basic')
- self.assertEqual(self.get()['status'], 200, 'seq')
- self.assertEqual(self.get()['status'], 200, 'seq 2')
+ assert self.get()['status'] == 200, 'seq'
+ assert self.get()['status'] == 200, 'seq 2'
def test_node_application_variables(self):
self.load('variables')
@@ -36,51 +36,44 @@ class TestNodeApplication(TestApplicationNode):
body=body,
)
- self.assertEqual(resp['status'], 200, 'status')
+ assert resp['status'] == 200, 'status'
headers = resp['headers']
header_server = headers.pop('Server')
- self.assertRegex(header_server, r'Unit/[\d\.]+', 'server header')
+ assert re.search(r'Unit/[\d\.]+', header_server), 'server header'
date = headers.pop('Date')
- self.assertEqual(date[-4:], ' GMT', 'date header timezone')
- self.assertLess(
- abs(self.date_to_sec_epoch(date) - self.sec_epoch()),
- 5,
- 'date header',
- )
+ 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')
- self.assertRegex(
- raw_headers,
+ assert re.search(
r'^(?:Host|localhost|Content-Type|'
'text\/html|Custom-Header|blah|Content-Length|17|Connection|'
'close|,)+$',
- 'raw headers',
- )
-
- self.assertDictEqual(
- 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',
- )
- self.assertEqual(resp['body'], body, 'body')
+ 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(self):
self.load('get_variables')
resp = self.get(url='/?var1=val1&var2=&var3')
- self.assertEqual(resp['headers']['X-Var-1'], 'val1', 'GET variables')
- self.assertEqual(resp['headers']['X-Var-2'], '', 'GET variables 2')
- self.assertEqual(resp['headers']['X-Var-3'], '', 'GET variables 3')
+ 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')
@@ -94,24 +87,24 @@ class TestNodeApplication(TestApplicationNode):
body='var1=val1&var2=&var3',
)
- self.assertEqual(resp['headers']['X-Var-1'], 'val1', 'POST variables')
- self.assertEqual(resp['headers']['X-Var-2'], '', 'POST variables 2')
- self.assertEqual(resp['headers']['X-Var-3'], '', 'POST variables 3')
+ 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')
resp = self.get()
- self.assertEqual(resp['status'], 404, '404 status')
- self.assertRegex(
- resp['body'], r'<title>404 Not Found</title>', '404 body'
- )
+ 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')
- self.assertEqual(self.get()['status'], 200, 'init')
+ assert self.get()['status'] == 200, 'init'
body = '0123456789' * 500
(resp, sock) = self.post(
@@ -125,7 +118,7 @@ class TestNodeApplication(TestApplicationNode):
read_timeout=1,
)
- self.assertEqual(resp['body'], '0123456789' * 500, 'keep-alive 1')
+ assert resp['body'] == '0123456789' * 500, 'keep-alive 1'
body = '0123456789'
resp = self.post(
@@ -138,47 +131,34 @@ class TestNodeApplication(TestApplicationNode):
body=body,
)
- self.assertEqual(resp['body'], body, 'keep-alive 2')
+ assert resp['body'] == body, 'keep-alive 2'
def test_node_application_write_buffer(self):
self.load('write_buffer')
- self.assertEqual(
- self.get()['body'], 'buffer', 'write buffer'
- )
+ assert self.get()['body'] == 'buffer', 'write buffer'
def test_node_application_write_callback(self):
self.load('write_callback')
- self.assertEqual(
- self.get()['body'],
- 'helloworld',
- 'write callback order',
- )
- self.assertTrue(
- self.waitforfiles(self.testdir + '/node/callback'),
- 'write callback',
- )
+ assert self.get()['body'] == 'helloworld', 'write callback order'
+ assert waitforfiles(self.temp_dir + '/node/callback'), 'write callback'
def test_node_application_write_before_write_head(self):
self.load('write_before_write_head')
- self.assertEqual(self.get()['status'], 200, 'write before writeHead')
+ assert self.get()['status'] == 200, 'write before writeHead'
def test_node_application_double_end(self):
self.load('double_end')
- self.assertEqual(self.get()['status'], 200, 'double end')
- self.assertEqual(self.get()['status'], 200, 'double end 2')
+ assert self.get()['status'] == 200, 'double end'
+ assert self.get()['status'] == 200, 'double end 2'
def test_node_application_write_return(self):
self.load('write_return')
- self.assertEqual(
- self.get()['body'],
- 'bodytrue',
- 'write return',
- )
+ assert self.get()['body'] == 'bodytrue', 'write return'
def test_node_application_remove_header(self):
self.load('remove_header')
@@ -190,69 +170,61 @@ class TestNodeApplication(TestApplicationNode):
'Connection': 'close',
}
)
- self.assertEqual(resp['headers']['Was-Header'], 'true', 'was header')
- self.assertEqual(resp['headers']['Has-Header'], 'false', 'has header')
- self.assertFalse('X-Header' in resp['headers'], 'remove header')
+ 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')
- self.assertEqual(
+ assert (
self.get(
headers={
'Host': 'localhost',
'X-Remove': 'blah',
'Connection': 'close',
}
- )['headers']['Has-Header'],
- 'true',
- 'remove header nonexisting',
- )
+ )['headers']['Has-Header']
+ == 'true'
+ ), 'remove header nonexisting'
def test_node_application_update_header(self):
self.load('update_header')
- self.assertEqual(
- self.get()['headers']['X-Header'], 'new', 'update header'
- )
+ assert self.get()['headers']['X-Header'] == 'new', 'update header'
def test_node_application_set_header_array(self):
self.load('set_header_array')
- self.assertListEqual(
- self.get()['headers']['Set-Cookie'],
- ['tc=one,two,three', 'tc=four,five,six'],
- 'set header array',
- )
+ assert self.get()['headers']['Set-Cookie'] == [
+ 'tc=one,two,three',
+ 'tc=four,five,six',
+ ], 'set header array'
- @unittest.skip('not yet')
+ @pytest.mark.skip('not yet')
def test_node_application_status_message(self):
self.load('status_message')
- self.assertRegex(
- self.get(raw_resp=True), r'200 blah', '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')
- self.assertEqual(
- self.get()['headers']['X-Type'], 'number', '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']
- self.assertEqual(headers['X-HEADER'], '3', 'header value')
- self.assertNotIn('X-Header', headers, 'insensitive')
- self.assertNotIn('X-header', headers, 'insensitive 2')
+ 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(self):
self.load('promise_handler')
- self.assertEqual(
+ assert (
self.post(
headers={
'Host': 'localhost',
@@ -260,19 +232,15 @@ class TestNodeApplication(TestApplicationNode):
'Connection': 'close',
},
body='callback',
- )['status'],
- 200,
- 'promise handler request',
- )
- self.assertTrue(
- self.waitforfiles(self.testdir + '/node/callback'),
- 'promise handler',
- )
+ )['status']
+ == 200
+ ), 'promise handler request'
+ assert waitforfiles(self.temp_dir + '/node/callback'), 'promise handler'
def test_node_application_promise_handler_write_after_end(self):
self.load('promise_handler')
- self.assertEqual(
+ assert (
self.post(
headers={
'Host': 'localhost',
@@ -281,15 +249,14 @@ class TestNodeApplication(TestApplicationNode):
'Connection': 'close',
},
body='callback',
- )['status'],
- 200,
- 'promise handler request write after end',
- )
+ )['status']
+ == 200
+ ), 'promise handler request write after end'
def test_node_application_promise_end(self):
self.load('promise_end')
- self.assertEqual(
+ assert (
self.post(
headers={
'Host': 'localhost',
@@ -297,13 +264,10 @@ class TestNodeApplication(TestApplicationNode):
'Connection': 'close',
},
body='end',
- )['status'],
- 200,
- 'promise end request',
- )
- self.assertTrue(
- self.waitforfiles(self.testdir + '/node/callback'), 'promise end'
- )
+ )['status']
+ == 200
+ ), 'promise end request'
+ assert waitforfiles(self.temp_dir + '/node/callback'), 'promise end'
def test_node_application_promise_multiple_calls(self):
self.load('promise_handler')
@@ -317,10 +281,9 @@ class TestNodeApplication(TestApplicationNode):
body='callback1',
)
- self.assertTrue(
- self.waitforfiles(self.testdir + '/node/callback1'),
- 'promise first call',
- )
+ assert waitforfiles(
+ self.temp_dir + '/node/callback1'
+ ), 'promise first call'
self.post(
headers={
@@ -331,65 +294,55 @@ class TestNodeApplication(TestApplicationNode):
body='callback2',
)
- self.assertTrue(
- self.waitforfiles(self.testdir + '/node/callback2'),
- 'promise second call',
- )
+ assert waitforfiles(
+ self.temp_dir + '/node/callback2'
+ ), 'promise second call'
- @unittest.skip('not yet')
+ @pytest.mark.skip('not yet')
def test_node_application_header_name_valid(self):
self.load('header_name_valid')
- self.assertNotIn('status', self.get(), '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')
- self.assertIn('X-Header', self.get()['headers'], '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')
- self.assertListEqual(
- self.get()['headers']['X-Names'],
- ['date', 'x-header'],
- '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')
- self.assertEqual(
+ assert (
self.get(
headers={
'Host': 'localhost',
'X-Header': 'length',
'Connection': 'close',
}
- )['headers']['X-Has-Header'],
- 'false',
- 'has header length',
- )
+ )['headers']['X-Has-Header']
+ == 'false'
+ ), 'has header length'
- self.assertEqual(
+ assert (
self.get(
headers={
'Host': 'localhost',
'X-Header': 'Date',
'Connection': 'close',
}
- )['headers']['X-Has-Header'],
- 'false',
- 'has header date',
- )
+ )['headers']['X-Has-Header']
+ == 'false'
+ ), 'has header date'
def test_node_application_write_multiple(self):
self.load('write_multiple')
- self.assertEqual(
- self.get()['body'], 'writewrite2end', 'write multiple'
- )
-
-
-if __name__ == '__main__':
- TestNodeApplication.main()
+ assert self.get()['body'] == 'writewrite2end', 'write multiple'