Comparing version 0.3.0 to 0.4.0
{ | ||
"name": "mock-yeah", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "An invaluable service mocking platform built on Express.", | ||
@@ -34,2 +34,3 @@ "main": "index.js", | ||
"devDependencies": { | ||
"chai": "^3.4.1", | ||
"eslint": "^1.10.3", | ||
@@ -36,0 +37,0 @@ "eslint-config-airbnb": "^2.1.1", |
@@ -35,3 +35,3 @@ # Mock Yeah | ||
mockyeah.get('/hello-world', {text: 'Hello World'}); | ||
mockyeah.get('/hello-world', { text: 'Hello World' }); | ||
``` | ||
@@ -44,5 +44,5 @@ | ||
1. Open http://localhost:4001/hello-world | ||
1. Open [http://localhost:4001/hello-world](http://localhost:4001/hello-world) | ||
1. Profit. You should see "Hello World" returned from the mock server. | ||
1. Profit. You should see "Hello World" returned from your mock server. | ||
@@ -49,0 +49,0 @@ ### Testing example |
@@ -17,2 +17,8 @@ 'use strict'; | ||
return (req, res) => { | ||
let send; | ||
// Default latency to 0 when undefined | ||
response.latency = response.latency || 0; | ||
// Default response status to 200 when undefined | ||
res.status(response.status || 200); | ||
@@ -23,22 +29,24 @@ | ||
if (response.filePath) { // if filePath, send filedefaultResponseType('json'); | ||
if (response.filePath) { // if filePath, send file | ||
if (response.type) res.type(response.type); | ||
res.sendFile(path.resolve(response.filePath)); | ||
send = res.sendFile.bind(res, path.resolve(response.filePath)); | ||
} else if (response.fixture) { // if fixture, send fixture file | ||
if (response.type) res.type(response.type); | ||
fixturesDir = fixturesDir || app.config.get('fixturesDir'); | ||
res.sendFile(path.resolve(fixturesDir, response.fixture)); | ||
send = res.sendFile.bind(res, path.resolve(fixturesDir, response.fixture)); | ||
} else if (response.html) { // if html, set Content-Type to application/html and send | ||
res.type(response.type || 'html'); | ||
res.send(response.html); | ||
send = res.send.bind(res, response.html); | ||
} else if (response.json) { // if json, set Content-Type to application/json and send | ||
res.type(response.type || 'json'); | ||
res.send(response.json); | ||
send = res.send.bind(res, response.json); | ||
} else if (response.text) { // if text, set Content-Type to text/plain and send | ||
res.type(response.type || 'text'); | ||
res.send(response.text); | ||
send = res.send.bind(res, response.text); | ||
} else { // else send empty response | ||
res.type(response.type || 'text'); | ||
res.send(); | ||
send = res.send.bind(res); | ||
} | ||
setTimeout(send, response.latency); | ||
}; | ||
@@ -45,0 +53,0 @@ }; |
'use strict'; | ||
require('../TestHelper'); | ||
const request = require('supertest')('http://localhost:4041'); | ||
@@ -4,0 +5,0 @@ const mockyeah = require('../../index.js'); |
@@ -6,2 +6,3 @@ 'use strict'; | ||
const mockyeah = TestHelper.mockyeah; | ||
const expect = require('chai').expect; | ||
@@ -188,2 +189,39 @@ describe('Response', () => { | ||
describe('Latency', () => { | ||
it('should response with latency', (done) => { | ||
const latency = 1000; | ||
const threshold = latency + 100; | ||
mockyeah.get('/slow/service', { text: 'Hello', latency }); | ||
const start = (new Date).getTime(); | ||
request | ||
.get('/slow/service') | ||
.expect(200, 'Hello', done) | ||
.expect(() => { | ||
const now = (new Date).getTime(); | ||
const duration = now - start; | ||
expect(duration).to.be.within(latency, threshold); | ||
}, done); | ||
}); | ||
it('should respond with no latency', (done) => { | ||
const threshold = 25; | ||
mockyeah.get('/fast/service', { text: 'Hello' }); | ||
const start = (new Date).getTime(); | ||
request | ||
.get('/fast/service') | ||
.expect(200, 'Hello', done) | ||
.expect(() => { | ||
const now = (new Date).getTime(); | ||
const duration = now - start; | ||
expect(duration).to.be.below(threshold); | ||
}, done); | ||
}); | ||
}); | ||
describe('Text', () => { | ||
@@ -190,0 +228,0 @@ it('should respond with text Content-Type for text', (done) => { |
@@ -8,7 +8,5 @@ 'use strict'; | ||
after(() => { | ||
mockyeah.close(); | ||
}); | ||
after(() => mockyeah.close()); | ||
module.exports.mockyeah = mockyeah; | ||
module.exports.request = request('http://localhost:4041'); |
22465
450
7