Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial work + waits Xms before responding
- Loading branch information
IamNguele
committed
Aug 11, 2017
0 parents
commit 673e55f
Showing
6 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# HttpResponseSimulator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); |