UNPKG

proxkey

Version:

Proxy/Mock server for testing build on top of HAPI.js

231 lines (215 loc) 8.37 kB
/*jshint unused:true*/ var assert = require('assert'); var should = require('should'); var configuration = require('./test_config').configuration; var querystring = require('querystring'); var http = require('http'); var sys = require('sys'); var exec = require('child_process').exec; var child; request = require('supertest'); describe("Proxkey Server Start", function() { this.timeout(10000); /* before(function(done){ child = exec("pm2 restart test/server.js", function (error, stdout, stderr) { sys.print('stdout: ' + stdout); sys.print('stderr: ' + stderr); if (error) { console.log('exec error: ' + error); } setTimeout(function(){ done(); },200); }); }); */ describe.only("When requesting /test/delay", function() { afterEach(function(done){ setTimeout(done,500); }); it("CASE A - USE FUNCTION", function(done) { request(configuration.host + ':' + configuration.port) .post('/test/delay') .set('Content-Type', 'text/xml') .send('<root><a><b>AA</b></a></root>') .end(function(err, res) { if (err) { console.log(err); } setTimeout(function(){ res.should.have.status(200); assert.equal(res.text,'{"data": "AAAAAAAAA"}'); done(); }, 1250); }); }); it("CASE B - USE NUMBER", function(done) { request(configuration.host + ':' + configuration.port) .post('/test/delay') .set('Content-Type', 'text/xml') .send('<root><a><b>BB</b></a></root>') .end(function(err, res) { if (err) { console.log(err); } setTimeout(function(){ res.should.have.status(200); assert.equal(res.text,'{"data": "BBBBBBBBB"}'); done(); }, 1250); }); }); it("CASE C - USE STRING", function(done) { request(configuration.host + ':' + configuration.port) .post('/test/delay') .set('Content-Type', 'text/xml') .send('<root><a><b>CC</b></a></root>') .end(function(err, res) { if (err) { console.log(err); } setTimeout(function(){ res.should.have.status(200); assert.equal(res.text,'{"data": "CCCCCCCCCCCCCCCC"}'); done(); }, 1250); }); }); it("CASE D - DELAY IS NULL", function(done) { request(configuration.host + ':' + configuration.port) .post('/test/delay') .set('Content-Type', 'text/xml') .send('<root><a><b>DD</b></a></root>') .end(function(err, res) { if (err) { console.log(err); } res.should.have.status(200); assert.equal(res.text,'{"data": "DDDDDDDDDDD"}'); setTimeout(done, 100); }); }); it("CASE NULL, lets call it EEE ", function(done) { var request_body_string = 'EEEEEEEE'; request(configuration.host + ':' + configuration.port) .post('/test/delay') .set('Content-Type', 'text/xml') .send('<root><a><b>'+ request_body_string +'</b></a></root>') .end(function(err, res) { if (err) { console.log(err); } res.should.have.status(200); assert.equal(res.text,'{"data": "'+request_body_string+'"}'); setTimeout(done, 100); }); }); }); describe("When requesting /test/a", function(){ afterEach(function(done){ setTimeout(done,500); }); it("GET - Should return an error, becuase this route configure only for POST request", function(done){ request(configuration.host + ':' + configuration.port) .get('/test/a') .end(function(err, res) { if (err) { console.log(err); } res.should.have.status(404); assert.equal(res.clientError, true); setTimeout(done,250); }); }); it("POST - Should return CASE `A` when phone number start with 1", function(done){ request(configuration.host + ':' + configuration.port) .post('/test/a') .send({ 'phone_num' : 11}) .end(function(err, res) { if (err) { console.log(err); } res.should.have.status(200); assert.equal(res.text,'<root><phone>CASE A</phone></root>'); setTimeout(done,250); }); }); it("POST - Should return CASE `B` when phone number start with 2", function(done){ request(configuration.host + ':' + configuration.port) .post('/test/a') .send({ 'phone_num' : 222}) .end(function(err, res) { if (err) { console.log(err); } res.should.have.status(200); assert.equal(res.text,'<root><phone>CASE B</phone></root>'); setTimeout(done,250); }); }); it("POST - Should return success response with default response when phone_num is not defined", function(done){ request(configuration.host + ':' + configuration.port) .post('/test/a') .end(function(err, res) { if (err) { console.log(err); } res.should.have.status(200); assert.equal(res.text,'<root><phone>Success default value</phone></root>'); setTimeout(done,250); }); }); it("POST - Should return CASE `D` when phone number start with 333", function(done){ request(configuration.host + ':' + configuration.port) .post('/test/a') .send({ 'phone_num' : 3333}) .end(function(err, res) { if (err) { console.log(err); } res.should.have.status(200); assert.equal(res.text,'<root><phone>You ask for 3333</phone></root>'); setTimeout(done,250); }); }); it("POST - Should return default when phone number start with 3313 (pattern work only if 3 first numbers are 3)", function(done){ request(configuration.host + ':' + configuration.port) .post('/test/a') .send({ 'phone_num' : 3313}) .end(function(err, res) { if (err) { console.log(err); } res.should.have.status(200); assert.equal(res.text,'<root><phone>Success 3313</phone></root>'); setTimeout(done,250); }); }); it("POST - Should return CASE `C` when phone number Equal to 8888888888", function(done){ request(configuration.host + ':' + configuration.port) .post('/test/a') .send({ 'phone_num' : 8888888888}) .end(function(err, res) { if (err) { console.log(err); } res.should.have.status(200); assert.equal(res.text,'<root><phone>CASE C 8888888888</phone></root>'); setTimeout(done,250); }); }); it("Case Proxy When phone number start with 00", function(done){ request(configuration.host + ':' + configuration.port) .post('/test/a') .send({ 'phone_num' : '000'}) .end(function(err, res) { if (err) { console.log(err); } console.log(res.text); setTimeout(done,250); }); }); }); });