Skip to content

Commit

Permalink
Initial work + waits Xms before responding
Browse files Browse the repository at this point in the history
  • Loading branch information
IamNguele committed Aug 11, 2017
0 parents commit 673e55f
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
1 change: 1 addition & 0 deletions README.md
@@ -0,0 +1 @@
# HttpResponseSimulator
35 changes: 35 additions & 0 deletions 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
36 changes: 36 additions & 0 deletions package.json
@@ -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"
}
}
17 changes: 17 additions & 0 deletions processors/text-processor.js
@@ -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
76 changes: 76 additions & 0 deletions tests/app.test.js
@@ -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.