From 6e75776a22c06c5141305cdc87104a91cdceb076 Mon Sep 17 00:00:00 2001 From: Eric Satterwhite Date: Thu, 29 Dec 2016 14:12:00 -0600 Subject: [PATCH] integration tests for invalid requests --- test/integration/post-timer.spec.js | 166 +++++++++++++++++++++------- 1 file changed, 127 insertions(+), 39 deletions(-) diff --git a/test/integration/post-timer.spec.js b/test/integration/post-timer.spec.js index fbda0bd2..b4f30509 100644 --- a/test/integration/post-timer.spec.js +++ b/test/integration/post-timer.spec.js @@ -6,6 +6,28 @@ const http = require('http') , Server = require('../../lib') ; +function toServer(port, expect = 'hello', method = 'post', time = 1000, cb){ + const start = Date.now() + const s = http.createServer((req, res) => { + let data = '' + , now = Date.now() + ; + + res.writeHead(200); + res.end('ok'); + assert.equal(req.method.toLowerCase(), method); + req.on('data', (chunk) => { + data += chunk + }); + req.on('end', () => { + assert.ok(now - start > time, `expected > ${time} got ${now-start}`) + assert.equal(data, expect); + s.close( cb ) + }); + }).listen(port); + return s +} + describe('skyring:api', function() { this.timeout(4000) let request, server @@ -20,44 +42,110 @@ describe('skyring:api', function() { }); describe('#POST /timer', function(){ - this.timeout(3000); - it('should set a timer postback - 1000ms', ( done ) => { - let start; - const s = http.createServer((req, res) => { - let data = '' - , now = Date.now() - ; - - res.writeHead(200); - res.end('ok'); - assert.equal(req.method.toLowerCase(), 'post'); - req.on('data', (chunk) => { - data += chunk - }); - req.on('end', () => { - assert.equal(data, 'hello'); - assert.ok(now - start > 1000, `expected > 1000 got ${now-start}`) - s.close( done ) - }); - - }).listen(9999); - start = Date.now() - request - .post('/timer') - .send({ - timeout: 1000 - , data: 'hello' - , callback: { - uri: 'http://node-5:9999' - , method: 'post' - , transport: 'http' - } - }) - .expect(201) - .end((err, res) => { - assert.ifError(err); - assert.ok(res.headers.location) - }); - }); + let sone, stwo, sthree + var os = require('os') + var hostname; + if(!process.env.TEST_HOST) { + hostname = os.hostname() + console.log(`env variable TEST_HOST not set. using ${hostname} as hostname`) + } else { + hostname = process.env.TEST_HOST; + } + describe('valid request', function(){ + this.timeout(3000); + it('should set a timer postback (201)', ( done ) => { + toServer(9999, 'hello', 'post', 1000, done) + request + .post('/timer') + .send({ + timeout: 1000 + , data: 'hello' + , callback: { + uri: `http://${hostname}:9999` + , method: 'post' + , transport: 'http' + } + }) + .expect(201) + .end((err, res) => { + assert.ifError(err); + assert.ok(res.headers.location) + }); + }); + + it('should allow request with no data - (201)', (done) => { + toServer(9999, '', 'post', 2000, done) + request + .post('/timer') + .send({ + timeout: 2000 + , callback: { + uri: `http://${hostname}:9999` + , method: 'post' + , transport: 'http' + } + }) + .expect(201) + .end((err, res) => { + assert.ifError(err); + assert.ok(res.headers.location) + }); + }); + + it('should allow request with timeout - (400)', (done) => { + request + .post('/timer') + .send({ + callback: { + uri: `http://${hostname}:9999` + , data: 'fake' + , method: 'post' + , transport: 'http' + } + }) + .expect(400) + .end((err, res) => { + assert.equal(typeof res.headers['x-skyring-reason'], 'string') + done() + }); + }); + + it('should allow request with no callback uri - (400)', (done) => { + request + .post('/timer') + .send({ + callback: { + timeout: 3000 + , data: 'fake' + , method: 'post' + , transport: 'http' + } + }) + .expect(400) + .end((err, res) => { + assert.equal(typeof res.headers['x-skyring-reason'], 'string') + done() + }); + }); + + it('should allow request with no transport - (400)', (done) => { + request + .post('/timer') + .send({ + callback: { + timeout: 3000 + , data: 'fake' + , method: 'post' + } + }) + .expect(400) + .end((err, res) => { + assert.equal(typeof res.headers['x-skyring-reason'], 'string') + done() + }); + }); + + }) + }); });