Comparing version 0.1.2 to 0.1.3
50
index.js
@@ -1,1 +0,49 @@ | ||
module.exports = require('./lib/simserver') | ||
"use strict" | ||
const SimServer = require('./lib/simserver') | ||
module.exports.SimServer = SimServer | ||
module.exports.run = function(callbackOrPort, maybePort) { | ||
if (typeof callbackOrPort === 'function') { | ||
return new SimServer().start(s => { | ||
callbackOrPort(s, s.address().port) | ||
}, maybePort) | ||
} | ||
return new SimServer().start(null, callbackOrPort) | ||
} | ||
module.exports.test = function(routine, done, timeout=10*1000) { | ||
if (!routine) { | ||
throw new Error('Test routine is required') | ||
} | ||
const server = new SimServer() | ||
const safety = setTimeout(() => server.close(), timeout) | ||
if (typeof done !== 'function') { | ||
return new Promise((resolve, reject) => { | ||
server.start(async s => { | ||
try { | ||
resolve(await routine(s, s.address().port)) | ||
} catch (e) { | ||
reject(e) | ||
} finally { | ||
server.close() | ||
clearTimeout(safety) | ||
} | ||
}) | ||
}) | ||
} else { | ||
const closeAndDone = e => { | ||
server.close(_ => done(e)) | ||
clearTimeout(safety) | ||
} | ||
server.start(async s => { | ||
try { | ||
routine(s, s.address().port, closeAndDone) | ||
} catch (e) { | ||
closeAndDone(e) | ||
} | ||
}) | ||
} | ||
} |
@@ -6,10 +6,10 @@ "use strict" | ||
module.exports.parseInt = function(value) { | ||
const v = +value | ||
return v && v >= 0 ? v : null | ||
module.exports.parseUInt = function(value) { | ||
const v = parseInt(value) | ||
return !isNaN(v) && v >= 0 ? v : null | ||
} | ||
module.exports.parseArgInt = function(position, defaultValue) { | ||
module.exports.parseUIntArg = function(position, defaultValue) { | ||
return process.argv && process.argv.length > position | ||
? this.parseInt(process.argv[position]) || defaultValue | ||
? this.parseUInt(process.argv[position]) || defaultValue | ||
: defaultValue | ||
@@ -23,14 +23,14 @@ } | ||
latency: { | ||
carrier: this.parseInt(q.l), | ||
noise: this.parseInt(q.n), | ||
carrier: this.parseUInt(q.l), | ||
noise: this.parseUInt(q.n), | ||
actual: 0, | ||
}, | ||
response: { | ||
status: this.parseInt(q.s), | ||
bodySize: this.parseInt(q.bs), | ||
echoHeaders: this.parseInt(q.eh) === 1, | ||
status: this.parseUInt(q.s), | ||
bodySize: this.parseUInt(q.bs), | ||
echoHeaders: this.parseUInt(q.eh) === 1, | ||
body: undefined, | ||
}, | ||
socket: { | ||
destroy: this.parseInt(q.d) === 1, | ||
destroy: this.parseUInt(q.d) === 1, | ||
} | ||
@@ -37,0 +37,0 @@ } |
@@ -8,3 +8,2 @@ "use strict" | ||
const limits = require('./limits') | ||
const defaultPort = 9999 | ||
@@ -19,3 +18,3 @@ class SimServer extends EventEmitter { | ||
this.server = http.createServer((req, res) => this.acceptRequest(req, res)) | ||
this.server.listen(port || settings.parseArgInt(2, defaultPort)) | ||
this.server.listen(settings.parseUInt(port) || settings.parseUIntArg(2, 0)) | ||
if (callback) { | ||
@@ -27,3 +26,3 @@ this.server.once('listening', () => callback(this.server)) | ||
stop(callback) { | ||
close(callback) { | ||
if (this.server) { | ||
@@ -30,0 +29,0 @@ this.server.close(callback) |
{ | ||
"name": "simdummy", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Simple tool for HTTP response simulation, automated and load testing", | ||
"main": "index.js", | ||
"scripts": { | ||
"start": "node entry.js" | ||
"start": "node entry.js 9999" | ||
}, | ||
@@ -9,0 +9,0 @@ "author": "Alex Pereverzyev", |
@@ -15,17 +15,48 @@ | ||
## Usage | ||
To simulate echo headers response with approximately 200 ms latency, start the server: | ||
To simulate HTTP response with latency, start listener at custom port: | ||
``` | ||
const SimServer = require('simdummy') | ||
const server = new SimServer().start() | ||
const run = require('simdummy').run | ||
run((server, port) => { | ||
console.log(`Simulation server started at ${port}`) | ||
}, 9999) | ||
``` | ||
Make HTTP call with simulation settings: | ||
Then make HTTP call and specify required latency (eg: 200ms) in query string: | ||
``` | ||
time curl http://localhost:9999/?l=200\&n=50\&eh=1 | ||
time curl http://localhost:9999/?l=200\&eh=1 | ||
``` | ||
To start HTTP listener to test asynchronous code with promises and async/await: | ||
``` | ||
const test = require('simdummy').test | ||
it('test async promise', async () => { | ||
await test(async (server, port) => { | ||
const res = await yourFavoriteHttpClient.get(`http://localhost:${port}`) | ||
assert.equal(res.status, 200) | ||
}) | ||
}) | ||
``` | ||
To start HTTP listener to test asynchronous code with callbacks, pass _done_ to _test_ call: | ||
``` | ||
it('test async callback', done => { | ||
test((server, port, done) => { | ||
yourFavoriteHttpClient.get(`http://localhost:${port}`, res => { | ||
assert.equal(res.status, 200) | ||
done() | ||
}) | ||
}, done) | ||
}) | ||
``` | ||
## Simulation Options | ||
@@ -35,8 +66,8 @@ | ||
- l - base latency | ||
- n - latency noise | ||
- s - response status | ||
- bs - response body size in bytes, rounded to octet | ||
- eh - request headers are stringified to response body | ||
- d - destroy socket | ||
- l=200 - base latency | ||
- n=50 - latency noise | ||
- s=404 - response status | ||
- bs=1024 - response body size in bytes, rounded to octet | ||
- eh=1 - request headers are stringified to response body | ||
- d=1 - destroy socket | ||
@@ -46,4 +77,6 @@ | ||
Simulation server can be started directly: | ||
``` | ||
const SimServer = require('simdummy') | ||
const SimServer = require('simdummy').SimServer | ||
@@ -63,3 +96,3 @@ // set latency and response body size limits | ||
// request event is emited after settings parsed and limits applied | ||
// request event is emitted after settings parsed and limits applied | ||
server.on('request', (req, res, settings) => { | ||
@@ -87,6 +120,6 @@ console.log('Simulating response') | ||
To start simdummy in [Docker container](https://hub.docker.com/r/alexpereverzyev/simdummy) do: | ||
To start simdummy in [Docker container](https://hub.docker.com/r/alexpereverzyev/simdummy): | ||
``` | ||
docker run --name sd -p 9999:9999 -d alexpereverzyev/simdummy:0.1.1 | ||
docker run --name sd -p 9999:9999 -d alexpereverzyev/simdummy | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10064
187
121