Skip to content
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ or
[default: false]
--color, -k Colorize cli output.
[default: true]
--delay, -d Add a delay (in ms) to your response times to mimic interacting with external APIs.
[default: 0]
--help Show usage information.

--version Show version number.
Expand Down
5 changes: 5 additions & 0 deletions bin/api-mock
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ options = {
'default': true,
'boolean': true
},
'delay': {
alias: "d",
description: "Add a delay (in ms) to your response times to mimic interacting with external APIs.\n",
'default': 0
},
help: {
description: "Show usage information.\n"
},
Expand Down
4 changes: 3 additions & 1 deletion src/api-mock.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class ApiMock
if [email protected]['cors-disable']
corsSupport = new CorsSupport @app

@delay = @configuration.options['delay']

run: () ->
app = @app

Expand All @@ -46,7 +48,7 @@ class ApiMock

# Walk AST, add routes to app
try
walker app, ast_json['resourceGroups']
walker app, ast_json['resourceGroups'], @delay
catch error
throw error

Expand Down
6 changes: 4 additions & 2 deletions src/walker.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exampleToHttpPayloadPair = require './example-to-http-payload-pair'
ut = require 'uri-template'
winston = require 'winston'

walker = (app, resourceGroups) ->
walker = (app, resourceGroups, delay) ->

sendResponse = (responses) ->
(req, res) ->
Expand All @@ -25,7 +25,9 @@ walker = (app, resourceGroups) ->
headerValue = value['value']
res.setHeader headerName, headerValue
res.setHeader 'Content-Length', Buffer.byteLength(response.body)
res.send response.status, response.body
setTimeout ( ->
Copy link
Author

Choose a reason for hiding this comment

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

Not familiar with coffeescript, but this does produce some weirdness when compiled:

return setTimeout((function() {
    return res.send(response.status, response.body);
}), delay);

Choose a reason for hiding this comment

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

Looks normal to me. :)

res.send response.status, response.body
), delay

responses = []

Expand Down