diff --git a/README.md b/README.md index 787608a..d10fd8c 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,7 @@ stubby [-a ] [-c ] [-d ] [-h] [-k ] [-l ] [-m] -t, --tls Port for https stubs portal. Defaults to 7443. -v, --version Prints stubby's version number. -w, --watch Auto-reload data file when edits are made. +-o, --latency Global latency delay when not defined in a route. ``` When used from the command-line, `stubby` responds to the `SIGHUP` signal to reload its configuration. diff --git a/lib/console/cli.js b/lib/console/cli.js index db47d3b..70c45d1 100644 --- a/lib/console/cli.js +++ b/lib/console/cli.js @@ -64,12 +64,18 @@ var options = [{ },{ name: 'version', flag: 'v', - description: "Prints stubby's version number." + description: 'Prints stubby\'s version number.' },{ name: 'watch', flag: 'w', - description: "Auto-reload data file when edits are made." -}] + description: 'Auto-reload data file when edits are made.' +},{ + name: 'latency', + flag: 'o', + param: 'milliseconds', + "default": 0, + description: 'Global latency delay when not defined in a route.' +}]; function help(go) { if (go == null) { go = false; } diff --git a/lib/models/endpoint.js b/lib/models/endpoint.js index 32bb4cb..b65881c 100644 --- a/lib/models/endpoint.js +++ b/lib/models/endpoint.js @@ -6,6 +6,7 @@ var http = require('http'); var url = require('url'); var q = require('querystring'); var out = require('../console/out'); +var cli = require('../console/cli'); function Endpoint(endpoint, datadir) { if (endpoint == null) { endpoint = {}; } @@ -127,7 +128,7 @@ function purifyResponse(me, incoming) { outgoing.push(pruneUndefined({ headers: purifyHeaders(response.headers), status: parseInt(response.status) || 200, - latency: parseInt(response.latency) || undefined, + latency: parseInt(response.latency || cli.getArgs().latency) || undefined, file: response.file, body: purifyBody(response.body) })); diff --git a/spec/cli.spec.coffee b/spec/cli.spec.coffee index 22c6288..b58955f 100644 --- a/spec/cli.spec.coffee +++ b/spec/cli.spec.coffee @@ -132,6 +132,26 @@ describe 'CLI', -> assert out.log.calledOnce + describe '-o, --latency', -> + it 'should return default if no flag provided', -> + expected = 0 + actual = sut.getArgs [] + + assert actual.latency is expected + + it 'should return supplied value when provided', -> + expected = '12345' + actual = sut.getArgs ['-o', expected] + + assert actual.latency is expected + + it 'should return supplied value when provided with full flag', -> + expected = '54321' + actual = sut.getArgs ['--latency', expected] + + assert actual.latency is expected + + describe 'data', -> expected = [ request: @@ -202,10 +222,12 @@ describe 'CLI', -> tls: "443" mute: true watch: filename + latency: 0 datadir: process.cwd() help: undefined version: (require '../package.json').version + sinon.stub(sut, 'data').returns expected.data sinon.stub(sut, 'key').returns expected.key sinon.stub(sut, 'cert').returns expected.cert @@ -222,8 +244,10 @@ describe 'CLI', -> '-t', expected.tls '-m' '-w' + '-o' ] + assert.deepEqual actual, expected sut.data.restore() diff --git a/spec/contract.spec.coffee b/spec/contract.spec.coffee index 30ee9b4..64adde5 100644 --- a/spec/contract.spec.coffee +++ b/spec/contract.spec.coffee @@ -299,6 +299,11 @@ describe 'contract', -> actual = sut data assert.deepEqual actual, expected + it 'should return no errors for latency undefined', -> + data.response.latency = undefined + result = sut data + assert result is null + it 'should return no errors for an empty body', -> delete data.response.body result = sut data diff --git a/spec/endpoint.spec.coffee b/spec/endpoint.spec.coffee index 484f819..7b447a4 100644 --- a/spec/endpoint.spec.coffee +++ b/spec/endpoint.spec.coffee @@ -1,6 +1,7 @@ waitsFor = require './helpers/waits-for' Endpoint = require '../lib/models/endpoint' assert = require 'assert' +cli = require '../lib/console/cli' compareOneWay = (left, right) -> for own key, value of left @@ -225,7 +226,7 @@ describe 'Endpoint', -> assert actual.request.headers.origin is expected - it 'should define aditional Cross-Origin headers', -> + it 'should define additional Cross-Origin headers', -> expected = 'http://example.org' @data.request.headers = Origin: 'http://example.org' @@ -237,3 +238,16 @@ describe 'Endpoint', -> assert actual.request.headers.origin is expected assert actual.request.headers['access-control-request-method'] is 'POST' assert actual.request.headers['access-control-request-headers'] is 'Content-Type, origin' + + it 'should allow custom latency', -> + expected = 5000 + + @data = + response: [ + latency: expected + ] + + actual = new Endpoint @data + + assert actual.response[0].latency is expected + diff --git a/spec/main.spec.coffee b/spec/main.spec.coffee index 849f66b..4224b17 100644 --- a/spec/main.spec.coffee +++ b/spec/main.spec.coffee @@ -132,3 +132,8 @@ describe 'main', -> sut.start options, -> assert options.cert is defaults.cert done() + + it 'should default latency to 0', (done) -> + sut.start options, -> + assert options.latency is defaults.latency + done() \ No newline at end of file