Permalink
Browse files

Initial work + waits Xms before responding

  • Loading branch information...
CodingNagger committed Aug 11, 2017
0 parents commit 673e55ffcd6597491eacd0bb5585b0bad16d06d8
Showing with 166 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +1 −0 README.md
  3. +35 −0 app.js
  4. +36 −0 package.json
  5. +17 −0 processors/text-processor.js
  6. +76 −0 tests/app.test.js
@@ -0,0 +1 @@
node_modules
@@ -0,0 +1 @@
# HttpResponseSimulator
35 app.js
@@ -0,0 +1,35 @@
var url = require('url')
var processQuery = require('./processors/text-processor')
var express = require('express')
var app = express()
var bodyParser = require('body-parser')
app.use( bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}))
app.get('/', function (req, res) {
var query = url.parse(req.url, true).query
res.send(processQuery(query))
})
app.post('/', function (req, res) {
res.send(processQuery(req.body))
})
app.put('/', function (req, res) {
res.send(processQuery(req.body))
})
var port = process.env.PORT || 8080;
var server = app.listen(port, function () {
var host = server.address().address
var port = server.address().port
console.log("Http Client Simulator listening at http://%s:%s", host, port)
})
module.exports = app
@@ -0,0 +1,36 @@
{
"name": "http-response-simulator",
"version": "1.0.0",
"description": "Just a tool to validate your http client implementation",
"main": "app.js",
"scripts": {
"test": "mocha --recursive tests"
},
"repository": {
"type": "git",
"url": "git+https://github.com/IamNguele/HttpResponseSimulator.git"
},
"keywords": [
"http",
"simulator",
"mock",
"http",
"response"
],
"author": "Jean-Dominique Nguele",
"license": "MIT",
"bugs": {
"url": "https://github.com/IamNguele/HttpResponseSimulator/issues"
},
"homepage": "https://github.com/IamNguele/HttpResponseSimulator#readme",
"dependencies": {
"body-parser": "^1.17.2",
"express": "^4.15.4",
"sleep": "^5.1.1",
"system-sleep": "^1.3.5"
},
"devDependencies": {
"mocha": "^3.5.0",
"supertest": "^3.0.0"
}
}
@@ -0,0 +1,17 @@
var sleep = require('system-sleep')
function processQuery(query) {
var response = ''
var wait = parseInt(query.wait)
if (!isNaN(wait)) {
sleep(wait)
response += 'Waited '+wait+'ms to respond.\
'
}
return response
}
module.exports = processQuery
@@ -0,0 +1,76 @@
var request = require('supertest');
var app = require("../app");
describe('app.js - GET /', function(){
it('Default GET returns 200', function(done){
request(app)
.get('/')
.expect(200, done);
});
it('Wait GET returns text wait message', function(done){
var wait = 50;
request(app)
.get('/?wait='+wait)
.expect(200, 'Waited '+wait+'ms to respond.\
', done);
});
});
describe('app.js - POST /', function(){
it('Default POST returns 200', function(done){
request(app)
.post('/')
.expect(200, done);
});
it('Text Wait POST returns text wait message', function(done){
var wait = 50;
request(app)
.post('/')
.send('wait='+wait)
.expect(200, 'Waited '+wait+'ms to respond.\
', done);
});
it('JSON Wait POST returns text wait message', function(done){
var wait = 50;
request(app)
.post('/')
.send({wait: wait})
.expect(200, 'Waited '+wait+'ms to respond.\
', done);
});
});
describe('app.js - PUT /', function(){
it('Default PUT returns 200', function(done){
request(app)
.put('/')
.expect(200, done);
});
it('Text Wait PUT returns text wait message', function(done){
var wait = 50;
request(app)
.put('/')
.send('wait='+wait)
.expect(200, 'Waited '+wait+'ms to respond.\
', done);
});
it('JSON Wait PUT returns text wait message', function(done){
var wait = 50;
request(app)
.put('/')
.send({wait: wait})
.expect(200, 'Waited '+wait+'ms to respond.\
', done);
});
});

0 comments on commit 673e55f

Please sign in to comment.