import unittest import unit class TestUnitNodeApplication(unit.TestUnitApplicationNode): def setUpClass(): u = unit.TestUnit().check_modules('node') def test_node_application_basic(self): 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') def test_node_application_seq(self): self.load('basic') self.assertEqual(self.get()['status'], 200, 'seq') self.assertEqual(self.get()['status'], 200, 'seq 2') def test_node_application_variables(self): self.load('variables') body = 'Test body string.' resp = self.post(headers={ 'Host': 'localhost', 'Content-Type': 'text/html', 'Custom-Header': 'blah' }, body=body) self.assertEqual(resp['status'], 200, 'status') headers = resp['headers'] header_server = headers.pop('Server') self.assertRegex(header_server, r'Unit/[\d\.]+', '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') raw_headers = headers.pop('Request-Raw-Headers') self.assertRegex(raw_headers, r'^(?:Host|localhost|Content-Type|' \ 'text\/html|Custom-Header|blah|Content-Length|17|,)+$', 'raw headers') self.assertDictEqual(headers, { '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') 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') def test_node_application_post_variables(self): self.load('post_variables') resp = self.post(headers={ 'Content-Type': 'application/x-www-form-urlencoded', 'Host': 'localhost', 'Connection': 'close' }, 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') def test_node_application_404(self): self.load('404') resp = self.get() self.assertEqual(resp['status'], 404, '404 status') self.assertRegex(resp['body'], r'