summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorSergey Kandaurov <pluknet@nginx.com>2018-10-25 15:43:48 +0300
committerSergey Kandaurov <pluknet@nginx.com>2018-10-25 15:43:48 +0300
commit0fdc7c3a55daceb54c034a51b30f06a932236965 (patch)
treee55e5f320d13934f0dfb742b8c0ec19962f0220d
parent41d3d63758fc3846d5a09afd3b33aac19231942a (diff)
downloadunit-0fdc7c3a55daceb54c034a51b30f06a932236965.tar.gz
unit-0fdc7c3a55daceb54c034a51b30f06a932236965.tar.bz2
Tests: Node.js application tests.
-rw-r--r--test/node/404/404.html6
-rwxr-xr-xtest/node/404/app.js8
-rwxr-xr-xtest/node/basic/app.js6
-rwxr-xr-xtest/node/get_header_type/app.js7
-rwxr-xr-xtest/node/get_variables/app.js9
-rwxr-xr-xtest/node/mirror/app.js12
-rwxr-xr-xtest/node/post_variables/app.js15
-rwxr-xr-xtest/node/remove_header/app.js11
-rwxr-xr-xtest/node/set_header_array/app.js6
-rwxr-xr-xtest/node/status_message/app.js6
-rwxr-xr-xtest/node/update_header/app.js7
-rwxr-xr-xtest/node/variables/app.js20
-rwxr-xr-xtest/node/write_before_write_head/app.js6
-rwxr-xr-xtest/node/write_buffer/app.js6
-rwxr-xr-xtest/node/write_callback/app.js10
-rwxr-xr-xtest/node/write_return/app.js6
-rw-r--r--test/test_node_application.py166
-rw-r--r--test/unit.py35
18 files changed, 342 insertions, 0 deletions
diff --git a/test/node/404/404.html b/test/node/404/404.html
new file mode 100644
index 00000000..6d0c635a
--- /dev/null
+++ b/test/node/404/404.html
@@ -0,0 +1,6 @@
+<html>
+<head><title>404 Not Found</title></head>
+<body bgcolor="white">
+<center><h1>404 Not Found</h1></center>
+</body>
+</html>
diff --git a/test/node/404/app.js b/test/node/404/app.js
new file mode 100755
index 00000000..9600d486
--- /dev/null
+++ b/test/node/404/app.js
@@ -0,0 +1,8 @@
+#!/usr/bin/env node
+
+var fs = require('fs');
+
+require('unit-http').createServer(function (req, res) {
+ res.writeHead(404, {});
+ res.end(fs.readFileSync('404.html'));
+}).listen(7080);
diff --git a/test/node/basic/app.js b/test/node/basic/app.js
new file mode 100755
index 00000000..bc8d570a
--- /dev/null
+++ b/test/node/basic/app.js
@@ -0,0 +1,6 @@
+#!/usr/bin/env node
+
+require('unit-http').createServer(function (req, res) {
+ res.writeHead(200, {'Content-Length': 12, 'Content-Type': 'text/plain'});
+ res.end('Hello World\n');
+}).listen(7080);
diff --git a/test/node/get_header_type/app.js b/test/node/get_header_type/app.js
new file mode 100755
index 00000000..b606f142
--- /dev/null
+++ b/test/node/get_header_type/app.js
@@ -0,0 +1,7 @@
+#!/usr/bin/env node
+
+require('unit-http').createServer(function (req, res) {
+ res.setHeader('X-Number', 100);
+ res.setHeader('X-Type', typeof(res.getHeader('X-Number')));
+ res.end();
+}).listen(7080);
diff --git a/test/node/get_variables/app.js b/test/node/get_variables/app.js
new file mode 100755
index 00000000..5c1faf41
--- /dev/null
+++ b/test/node/get_variables/app.js
@@ -0,0 +1,9 @@
+#!/usr/bin/env node
+
+require('unit-http').createServer(function (req, res) {
+ let query = require('url').parse(req.url, true).query;
+ res.setHeader('X-Var-1', query.var1);
+ res.setHeader('X-Var-2', query.var2);
+ res.setHeader('X-Var-3', query.var3);
+ res.end();
+}).listen(7080);
diff --git a/test/node/mirror/app.js b/test/node/mirror/app.js
new file mode 100755
index 00000000..abcb87cb
--- /dev/null
+++ b/test/node/mirror/app.js
@@ -0,0 +1,12 @@
+#!/usr/bin/env node
+
+require('unit-http').createServer(function (req, res) {
+ let body = '';
+ req.on('data', chunk => {
+ body += chunk.toString();
+ });
+ req.on('end', () => {
+ res.writeHead(200, {'Content-Length': Buffer.byteLength(body)});
+ res.end(body);
+ });
+}).listen(7080);
diff --git a/test/node/post_variables/app.js b/test/node/post_variables/app.js
new file mode 100755
index 00000000..928a38cf
--- /dev/null
+++ b/test/node/post_variables/app.js
@@ -0,0 +1,15 @@
+#!/usr/bin/env node
+
+require('unit-http').createServer(function (req, res) {
+ let body = '';
+ req.on('data', chunk => {
+ body += chunk.toString();
+ });
+ req.on('end', () => {
+ let query = require('querystring').parse(body);
+ res.setHeader('X-Var-1', query.var1);
+ res.setHeader('X-Var-2', query.var2);
+ res.setHeader('X-Var-3', query.var3);
+ res.end();
+ });
+}).listen(7080);
diff --git a/test/node/remove_header/app.js b/test/node/remove_header/app.js
new file mode 100755
index 00000000..28fee16d
--- /dev/null
+++ b/test/node/remove_header/app.js
@@ -0,0 +1,11 @@
+#!/usr/bin/env node
+
+require('unit-http').createServer(function (req, res) {
+ res.setHeader('X-Header', 'test');
+ res.setHeader('Was-Header', res.hasHeader('X-Header').toString());
+
+ res.removeHeader('X-Header');
+ res.setHeader('Has-Header', res.hasHeader('X-Header').toString());
+
+ res.end();
+}).listen(7080);
diff --git a/test/node/set_header_array/app.js b/test/node/set_header_array/app.js
new file mode 100755
index 00000000..faac45c7
--- /dev/null
+++ b/test/node/set_header_array/app.js
@@ -0,0 +1,6 @@
+#!/usr/bin/env node
+
+require('unit-http').createServer(function (req, res) {
+ res.setHeader('Set-Cookie', ['tc=one,two,three', 'tc=four,five,six']);
+ res.end();
+}).listen(7080);
diff --git a/test/node/status_message/app.js b/test/node/status_message/app.js
new file mode 100755
index 00000000..4f3b064a
--- /dev/null
+++ b/test/node/status_message/app.js
@@ -0,0 +1,6 @@
+#!/usr/bin/env node
+
+require('unit-http').createServer(function (req, res) {
+ res.writeHead(200, 'blah', {'Content-Type': 'text/plain'});
+ res.end();
+}).listen(7080);
diff --git a/test/node/update_header/app.js b/test/node/update_header/app.js
new file mode 100755
index 00000000..0c5cd237
--- /dev/null
+++ b/test/node/update_header/app.js
@@ -0,0 +1,7 @@
+#!/usr/bin/env node
+
+require('unit-http').createServer(function (req, res) {
+ res.setHeader('X-Header', 'test');
+ res.setHeader('X-Header', 'new');
+ res.end();
+}).listen(7080);
diff --git a/test/node/variables/app.js b/test/node/variables/app.js
new file mode 100755
index 00000000..968afba5
--- /dev/null
+++ b/test/node/variables/app.js
@@ -0,0 +1,20 @@
+#!/usr/bin/env node
+
+require('unit-http').createServer(function (req, res) {
+ let body = '';
+ req.on('data', chunk => {
+ body += chunk.toString();
+ });
+ req.on('end', () => {
+ res.setHeader('Request-Method', req.method);
+ res.setHeader('Request-Uri', req.url);
+ res.setHeader('Server-Protocol', req.httpVersion);
+ res.setHeader('Request-Raw-Headers', req.rawHeaders.join());
+ res.setHeader('Content-Length', Buffer.byteLength(body));
+ res.setHeader('Content-Type', req.headers['Content-Type']);
+ res.setHeader('Custom-Header', req.headers['Custom-Header']);
+ res.setHeader('Http-Host', req.headers['Host']);
+ res.writeHead(200, {});
+ res.end(body);
+ });
+}).listen(7080);
diff --git a/test/node/write_before_write_head/app.js b/test/node/write_before_write_head/app.js
new file mode 100755
index 00000000..9fe3a58d
--- /dev/null
+++ b/test/node/write_before_write_head/app.js
@@ -0,0 +1,6 @@
+#!/usr/bin/env node
+
+require('unit-http').createServer(function (req, res) {
+ res.write('blah');
+ res.writeHead(200, {'Content-Type': 'text/plain'});
+}).listen(7080);
diff --git a/test/node/write_buffer/app.js b/test/node/write_buffer/app.js
new file mode 100755
index 00000000..f41de2a1
--- /dev/null
+++ b/test/node/write_buffer/app.js
@@ -0,0 +1,6 @@
+#!/usr/bin/env node
+
+require('unit-http').createServer(function (req, res) {
+ res.writeHead(200, {'Content-Type': 'text/plain'});
+ res.end(new Buffer([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]));
+}).listen(7080);
diff --git a/test/node/write_callback/app.js b/test/node/write_callback/app.js
new file mode 100755
index 00000000..9d4bc1c5
--- /dev/null
+++ b/test/node/write_callback/app.js
@@ -0,0 +1,10 @@
+#!/usr/bin/env node
+
+require('unit-http').createServer(function (req, res) {
+ res.writeHead(200, {'Content-Type': 'text/plain'});
+ var a = 'blah';
+ res.write('hello', 'utf8', function() {
+ a = 'world';
+ });
+ res.end(a);
+}).listen(7080);
diff --git a/test/node/write_return/app.js b/test/node/write_return/app.js
new file mode 100755
index 00000000..3ae967c6
--- /dev/null
+++ b/test/node/write_return/app.js
@@ -0,0 +1,6 @@
+#!/usr/bin/env node
+
+require('unit-http').createServer(function (req, res) {
+ res.writeHead(200, {'Content-Type': 'text/plain'});
+ res.end(res.write('body').toString());
+}).listen(7080);
diff --git a/test/test_node_application.py b/test/test_node_application.py
new file mode 100644
index 00000000..715173e6
--- /dev/null
+++ b/test/test_node_application.py
@@ -0,0 +1,166 @@
+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'<title>404 Not Found</title>',
+ '404 body')
+
+ def test_node_keepalive_body(self):
+ self.load('mirror')
+
+ (resp, sock) = self.post(headers={
+ 'Connection': 'keep-alive',
+ 'Content-Type': 'text/html',
+ 'Host': 'localhost'
+ }, start=True, body='0123456789' * 500)
+
+ self.assertEqual(resp['body'], '0123456789' * 500, 'keep-alive 1')
+
+ resp = self.post(headers={
+ 'Connection': 'close',
+ 'Content-Type': 'text/html',
+ 'Host': 'localhost'
+ }, sock=sock, body='0123456789')
+
+ self.assertEqual(resp['body'], '0123456789', 'keep-alive 2')
+
+ def test_node_application_write_buffer(self):
+ self.load('write_buffer')
+
+ self.assertEqual(self.get()['body'], '6\r\nbuffer\r\n0\r\n\r\n',
+ 'write buffer')
+
+ def test_node_application_write_callback(self):
+ self.load('write_callback')
+
+ self.assertEqual(self.get()['body'],
+ '5\r\nhello\r\n5\r\nworld\r\n0\r\n\r\n', 'write callback')
+
+ def test_node_application_write_before_writeHead(self):
+ self.skip_alerts.append(r'process \d+ exited on signal')
+ self.load('write_before_write_head')
+
+ self.get()
+
+ def test_node_application_write_return(self):
+ self.load('write_return')
+
+ self.assertEqual(self.get()['body'],
+ '4\r\nbody\r\n4\r\ntrue\r\n0\r\n\r\n', 'write return')
+
+ def test_node_application_remove_header(self):
+ self.load('remove_header')
+
+ resp = self.get()
+ 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')
+
+ def test_node_application_update_header(self):
+ self.load('update_header')
+
+ self.assertEqual(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')
+
+ @unittest.expectedFailure
+ def test_node_application_status_message(self):
+ self.load('status_message')
+
+ self.assertRegex(self.get(raw_resp=True), r'200 blah', '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')
+
+if __name__ == '__main__':
+ TestUnitNodeApplication.main()
diff --git a/test/unit.py b/test/unit.py
index c3efef26..e88ed684 100644
--- a/test/unit.py
+++ b/test/unit.py
@@ -117,6 +117,12 @@ class TestUnit(unittest.TestCase):
except:
m = None
+ elif module == 'node':
+ if os.path.isdir(self.pardir + '/node/node_modules'):
+ m = module
+ else:
+ m = None
+
elif module == 'openssl':
try:
subprocess.check_output(['which', 'openssl'])
@@ -558,6 +564,35 @@ class TestUnitApplicationGo(TestUnitApplicationProto):
}
})
+class TestUnitApplicationNode(TestUnitApplicationProto):
+ def load(self, script, name='app.js'):
+
+ # copy application
+
+ shutil.copytree(self.current_dir + '/node/' + script,
+ self.testdir + '/node')
+
+ # link modules
+
+ os.symlink(self.pardir + '/node/node_modules',
+ self.testdir + '/node/node_modules')
+
+ self.conf({
+ "listeners": {
+ "*:7080": {
+ "application": script
+ }
+ },
+ "applications": {
+ script: {
+ "type": "external",
+ "processes": { "spare": 0 },
+ "working_directory": self.testdir + '/node',
+ "executable": name
+ }
+ }
+ })
+
class TestUnitApplicationPerl(TestUnitApplicationProto):
def load(self, script, name='psgi.pl'):
self.conf({