Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for global --latency #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ stubby [-a <port>] [-c <file>] [-d <file>] [-h] [-k <file>] [-l <hostname>] [-m]
-t, --tls <port> 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.
Expand Down
12 changes: 9 additions & 3 deletions lib/console/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
3 changes: 2 additions & 1 deletion lib/models/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}; }
Expand Down Expand Up @@ -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,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need to do a bit more than this for cases where new Stubby().start(options) is used, as it is not always started from a command-line invocation.

file: response.file,
body: purifyBody(response.body)
}));
Expand Down
24 changes: 24 additions & 0 deletions spec/cli.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -222,8 +244,10 @@ describe 'CLI', ->
'-t', expected.tls
'-m'
'-w'
'-o'
]


assert.deepEqual actual, expected

sut.data.restore()
Expand Down
5 changes: 5 additions & 0 deletions spec/contract.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion spec/endpoint.spec.coffee
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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'
Expand All @@ -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

5 changes: 5 additions & 0 deletions spec/main.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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()