From f21ea269abe53d827a95d8aa83079fe0ad640a8b Mon Sep 17 00:00:00 2001 From: austburn Date: Fri, 20 Feb 2015 15:22:18 -0500 Subject: [PATCH] Add support for delaying responses to mimic real APIs. --- README.md | 2 ++ bin/api-mock | 5 +++++ src/api-mock.coffee | 4 +++- src/walker.coffee | 6 ++++-- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9f17cfa..41ec8d6 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/bin/api-mock b/bin/api-mock index cc3a7bb..48f04eb 100755 --- a/bin/api-mock +++ b/bin/api-mock @@ -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" }, diff --git a/src/api-mock.coffee b/src/api-mock.coffee index 70369cf..5de7f72 100644 --- a/src/api-mock.coffee +++ b/src/api-mock.coffee @@ -30,6 +30,8 @@ class ApiMock if !@configuration.options['cors-disable'] corsSupport = new CorsSupport @app + @delay = @configuration.options['delay'] + run: () -> app = @app @@ -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 diff --git a/src/walker.coffee b/src/walker.coffee index b79c79b..e5fb368 100644 --- a/src/walker.coffee +++ b/src/walker.coffee @@ -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) -> @@ -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 ( -> + res.send response.status, response.body + ), delay responses = []