From c53dd2d64481edefe76cfc1d8cdaf609f821033d Mon Sep 17 00:00:00 2001 From: Dan Hough Date: Wed, 2 Apr 2014 23:40:47 +0200 Subject: [PATCH] A couple of linter fixes and updated readme --- lib/server.js | 13 ---- readme.md | 28 +++----- tests/javascript.test.js | 136 +++++++++++++++++++-------------------- 3 files changed, 78 insertions(+), 99 deletions(-) diff --git a/lib/server.js b/lib/server.js index 27ceb69..f0ae9ce 100644 --- a/lib/server.js +++ b/lib/server.js @@ -55,16 +55,6 @@ function Interfake(o) { app.routes[request.method].splice(i); } - function setRouteProperty(request, property, value) { - var i = indexOfRequestRoute(request); - - if (i === -1) { - return; - } - - app.routes[request.method][i][property] = value; - } - function determineDelay(delayInput) { var result = 0, range, upper, lower; if(util.isNumber(delayInput)) { @@ -167,9 +157,6 @@ function Interfake(o) { }, responseDelay); }); - setRouteProperty(specifiedRequest, 'responseData', data.response); - setRouteProperty(specifiedRequest, 'afterResponseData', data.afterResponse); - if (data.response.body) { debug('Setup complete: ' + data.request.method + ' ' + data.request.url + ' to return ' + data.response.code + ' with a body of length ' + JSON.stringify(data.response.body).length); } else { diff --git a/readme.md b/readme.md index 96bee56..82e1952 100644 --- a/readme.md +++ b/readme.md @@ -108,22 +108,7 @@ Interfake can handle complex API structures, mutable endpoints and has three int ## Method 1: JavaScript -Make sure you've install Interfake as a local module using `npm install interfake --save`. If you `var Interfake = require('interfake')` in your JavaScript file, you can use the following API to spin up the Interfake server. - -### API - -* `new Interfake(options)`: creates an Interfake object. Options are: - * `debug`: If `true`, outputs lots of annoying but helpful log messages. Default is `false`. -* `#createRoute(route)`: Takes a JSON object with `request`, `response` and optionally `afterResponse` properties -* `#listen(port)`: Takes a port and starts the server -* `#stop()`: Stops the server if it's been started - -#### Fluent Interface - -* `#get|post|put|delete(url)`: Create an endpoint at the specified URL. Can then be followed by each of the following, which can follow each other too e.g. `get().body().status().body()` - * `#status(statusCode)`: Set the response status code for the endpoint - * `#body(body)`: Set the JSON response body of the end point - * `#create#get|post|put|delete(url)`: Specify an endpoint to create *after* the first execution of this one. API is the same as above. +Make sure you've installed Interfake as a local module using `npm install interfake --save`. If you `var Interfake = require('interfake')` in your JavaScript file, you can use the following API to spin up the Interfake server. ### Example ([more examples](/examples-javascript)) @@ -139,7 +124,10 @@ interfake.post('/items').status(201).body({ created: true }).creates.get('/next- interfake.createRoute({ request: { url: '/whats-next', - method: 'get' + method: 'get', + query: { // Optional querystring parameters + page: 2 + } }, response: { code: 200, // HTTP Status Code @@ -157,7 +145,10 @@ interfake.listen(3000); // The server will listen on port 3000 * `new Interfake(options)`: creates an Interfake object. Options are: * `debug`: If `true`, outputs lots of annoying but helpful log messages. Default is `false`. -* `#createRoute(route)`: Takes a JSON object with `request`, `response` and optionally `afterResponse` properties +* `#createRoute(route)`: Takes a JSON object with the following: + * `request` + * `response` + * `afterResponse` (optional) * `#listen(port)`: Takes a port and starts the server * `#stop()`: Stops the server if it's been started * `#serveStatic(path, directory)`: Serve static (usually a website) files from a certain path. This is useful for testing [SPAs](http://en.wikipedia.org/wiki/Single-page_application). ([Example use.](/examples-javascript/fluent-web-page-test.js)) @@ -170,6 +161,7 @@ interfake.listen(3000); // The server will listen on port 3000 * `#delay(milliseconds)`: Set the number of milliseconds to delay the response by to mimic network of processing lag * Also accepts a delay range in the format 'ms..ms' e.g. '50..100' * `#create#get|post|put|delete(url)`: Specify an endpoint to create *after* the first execution of this one. API is the same as above. + * -`#query`- This is **not yet supported** in the fluent interface ## Method 2: Command line diff --git a/tests/javascript.test.js b/tests/javascript.test.js index b8da0b8..97ed6f8 100644 --- a/tests/javascript.test.js +++ b/tests/javascript.test.js @@ -18,11 +18,11 @@ describe('Interfake JavaScript API', function () { beforeEach(function () { interfake = new Interfake(); }); - afterEach(function () { - if (interfake) { - interfake.stop(); - } - }); + afterEach(function () { + if (interfake) { + interfake.stop(); + } + }); describe('#createRoute()', function () { it('should create one GET endpoint', function (done) { interfake.createRoute({ @@ -46,74 +46,74 @@ describe('Interfake JavaScript API', function () { }); }); - it('should create a GET endpoint that accepts a query parameter', function (done) { - // interfake = new Interfake(); - interfake.createRoute({ - request: { - url: '/wantsQueryParameter', - query: { query: '1234' }, - method: 'get' + it('should create a GET endpoint that accepts a query parameter', function (done) { + // interfake = new Interfake(); + interfake.createRoute({ + request: { + url: '/wantsQueryParameter', + query: { query: '1234' }, + method: 'get' - }, - response: { - code: 200, - body: { - high: 'hoe' - } - } - }); - interfake.listen(3000); + }, + response: { + code: 200, + body: { + high: 'hoe' + } + } + }); + interfake.listen(3000); - request({ url : 'http://localhost:3000/wantsQueryParameter?query=1234', json : true }, function (error, response, body) { - assert.equal(error, undefined); - assert.equal(response.statusCode, 200); - assert.equal(body.high, 'hoe'); - done(); - }); - }); + request({ url : 'http://localhost:3000/wantsQueryParameter?query=1234', json : true }, function (error, response, body) { + assert.equal(error, undefined); + assert.equal(response.statusCode, 200); + assert.equal(body.high, 'hoe'); + done(); + }); + }); - it('should create one GET endpoint accepting query parameters with different responses', function () { - // interfake = new Interfake(); - interfake.createRoute({ - request: { - url: '/wantsQueryParameter', - query: { query: '1234' }, - method: 'get' + it('should create one GET endpoint accepting query parameters with different responses', function () { + // interfake = new Interfake(); + interfake.createRoute({ + request: { + url: '/wantsQueryParameter', + query: { query: '1234' }, + method: 'get' - }, - response: { - code: 200, - body: { - high: 'hoe' - } - } - }); - interfake.createRoute({ - request: { - url: '/wantsQueryParameter', - query: { query: '5678', anotherQuery: '4321' }, - method: 'get' - }, - response: { - code: 200, - body: { - loan: 'shark' - } - } - }); - interfake.listen(3000); + }, + response: { + code: 200, + body: { + high: 'hoe' + } + } + }); + interfake.createRoute({ + request: { + url: '/wantsQueryParameter', + query: { query: '5678', anotherQuery: '4321' }, + method: 'get' + }, + response: { + code: 200, + body: { + loan: 'shark' + } + } + }); + interfake.listen(3000); - return Q.all([get({url: 'http://localhost:3000/wantsQueryParameter?query=1234', json: true}), - get({url: 'http://localhost:3000/wantsQueryParameter?anotherQuery=4321&query=5678', json: true}), - get({url: 'http://localhost:3000/wantsQueryParameter', json: true}) - ]).then(function (results) { - assert.equal(results[0][0].statusCode, 200); - assert.equal(results[0][1].high, 'hoe'); - assert.equal(results[1][0].statusCode, 200); - assert.equal(results[1][1].loan, 'shark'); - assert.equal(results[2][0].statusCode, 404); - }); - }); + return Q.all([get({url: 'http://localhost:3000/wantsQueryParameter?query=1234', json: true}), + get({url: 'http://localhost:3000/wantsQueryParameter?anotherQuery=4321&query=5678', json: true}), + get({url: 'http://localhost:3000/wantsQueryParameter', json: true}) + ]).then(function (results) { + assert.equal(results[0][0].statusCode, 200); + assert.equal(results[0][1].high, 'hoe'); + assert.equal(results[1][0].statusCode, 200); + assert.equal(results[1][1].loan, 'shark'); + assert.equal(results[2][0].statusCode, 404); + }); + }); it('should create three GET endpoints with different status codes', function (done) { interfake.createRoute({