Comparing version 0.12.1 to 0.13.0
'use strict'; | ||
const express = require('express'); | ||
const app = express(); | ||
const Logger = require('./lib/Logger'); | ||
const RouteManager = require('./lib/RouteManager'); | ||
/** | ||
* Initialize RouteResolver with app | ||
* App is needed for unregistering routes on teardown | ||
* App module | ||
* @param {Object} config Application configuration. | ||
* @return {Instances} Instance of an Express application. | ||
*/ | ||
require('./lib/RouteResolver')(app); | ||
module.exports = function App(config) { | ||
const app = express(); | ||
/** | ||
* Attach RouteManager to app object | ||
* RouteManager serves as the primary set of Mock Yeah api methods | ||
*/ | ||
app.RouteManager = RouteManager(app); | ||
const defaultConfig = { | ||
name: 'mockyeah' | ||
}; | ||
app.get('/', (req, res) => { | ||
res.send('Hello, Mock Yeah!'); | ||
}); | ||
// Prepare configuration. Merge configuration with default configuration | ||
app.config = Object.assign({}, defaultConfig, config || {}); | ||
module.exports = app; | ||
// Instantiate new logger | ||
const logger = new Logger({ | ||
name: app.config.name | ||
}); | ||
// Attach log to app instance to bind output to app instance | ||
app.log = logger.log.bind(logger); | ||
// Attach RouteManager to app object, the primary set of mockyeah API methods. | ||
app.routeManager = new RouteManager(app); | ||
app.get('/', (req, res) => { | ||
res.send('Hello, mockyeah!'); | ||
}); | ||
return app; | ||
}; |
'use strict'; | ||
const tildify = require('tildify'); | ||
const FixturePlayer = require('./FixturePlayer'); | ||
const FixtureRecorder = require('./FixtureRecorder'); | ||
const RouteStore = require('./RouteStore'); | ||
/** | ||
* RouteManager | ||
* Primary Mock Yeah API (i.e. get, post, put, delete, reset). | ||
* Primary mockyeah API (i.e. get, post, put, delete, reset, record, play). | ||
*/ | ||
module.exports = function RouteManager(app) { | ||
const routeStore = new RouteStore(app); | ||
const path = require('path'); | ||
const RouteStore = require('./RouteStore'); | ||
let setsDir; | ||
module.exports = function RouteManager(app) { | ||
return { | ||
register: (method, _path, response) => { | ||
RouteStore.register(method, _path, response); | ||
register: function register(method, _path, response) { | ||
routeStore.register(method, _path, response); | ||
}, | ||
all: function all(_path, response) { | ||
this.register('all', _path, response); | ||
}, | ||
get: function get(_path, response) { | ||
@@ -35,10 +41,19 @@ this.register('get', _path, response); | ||
reset: function reset() { | ||
RouteStore.reset(); | ||
routeStore.reset(); | ||
}, | ||
loadSet: function loadSet(setName) { | ||
setsDir = setsDir || app.config.setsDir; | ||
const set = require(path.resolve(setsDir, setName)); | ||
record: function record(fixtureName) { | ||
const fixture = new FixtureRecorder(app, fixtureName); | ||
this.register('all', '*', fixture.record.bind(fixture)); | ||
}, | ||
set.forEach((route) => { | ||
play: function play(fixtureName) { | ||
const fixture = new FixturePlayer(app, fixtureName); | ||
app.log(['serve', 'fixture'], tildify(fixture.path)); | ||
fixture.files().forEach((route) => { | ||
app.log(['serve', 'mount', route.method], route.originalPath, false); | ||
app.log(['serve', 'mount', route.method], `${route.originalPath} at ${route.path}`, true); | ||
this.register(route.method, route.path, route.options); | ||
@@ -45,0 +60,0 @@ }); |
'use strict'; | ||
const path = require('path'); | ||
const _ = require('lodash'); | ||
/** | ||
* RouteResolver | ||
* Facilitates route registration and unregistration. | ||
* Implements Express route middleware based on Mock Yeah API options. | ||
* Implements Express route middleware based on mockyeah API options. | ||
*/ | ||
function RouteResolver(app) { | ||
this.app = app; | ||
} | ||
const path = require('path'); | ||
const _ = require('lodash'); | ||
let app; | ||
let fixturesDir; | ||
const validateResponse = function validateResponse(response) { | ||
function validateResponse(response) { | ||
const payloadKeysPresent = []; | ||
@@ -25,5 +26,5 @@ const payloadKeys = ['fixture', 'filePath', 'html', 'json', 'text']; | ||
} | ||
}; | ||
} | ||
const handler = function handler(response) { | ||
function handler(response) { | ||
response = response || {}; | ||
@@ -34,2 +35,3 @@ | ||
return (req, res) => { | ||
const start = (new Date).getTime(); | ||
let send; | ||
@@ -51,4 +53,3 @@ | ||
if (response.type) res.type(response.type); | ||
fixturesDir = fixturesDir || app.config.fixturesDir; | ||
send = res.sendFile.bind(res, path.resolve(fixturesDir, response.fixture)); | ||
send = res.sendFile.bind(res, path.resolve(this.app.config.fixturesDir, response.fixture)); | ||
} else if (response.html) { // if html, set Content-Type to application/html and send | ||
@@ -63,2 +64,4 @@ res.type(response.type || 'html'); | ||
send = res.send.bind(res, response.text); | ||
} else if (response.raw) { // if raw, don't set Content-Type | ||
send = res.send.bind(res, response.raw); | ||
} else { // else send empty response | ||
@@ -69,24 +72,26 @@ res.type(response.type || 'text'); | ||
setTimeout(send, response.latency); | ||
setTimeout(() => { | ||
const duration = (new Date).getTime() - start; | ||
send(); | ||
this.app.log(['request', req.method], `${req.url} (${duration}ms)`); | ||
}, response.latency); | ||
}; | ||
}; | ||
} | ||
module.exports = function RouteResolver(_app) { | ||
app = _app; | ||
}; | ||
module.exports.register = function register(route) { | ||
RouteResolver.prototype.register = function register(route) { | ||
if (!_.isFunction(route.response)) { | ||
route.response = handler(route.response); | ||
route.response = handler.call(this, route.response); | ||
} | ||
app[route.method](route.path, route.response); | ||
this.app[route.method](route.path, route.response); | ||
}; | ||
module.exports.unregister = function unregister(routes) { | ||
RouteResolver.prototype.unregister = function unregister(routes) { | ||
const routePaths = routes.map((route) => { return route.path; }); | ||
app._router.stack = app._router.stack.filter((layer) => { | ||
this.app._router.stack = this.app._router.stack.filter((layer) => { | ||
return !(layer.route && routePaths.indexOf(layer.route.path) >= 0); | ||
}); | ||
}; | ||
module.exports = RouteResolver; |
'use strict'; | ||
const RouteResolver = require('./RouteResolver'); | ||
/** | ||
@@ -8,23 +10,19 @@ * RouteStore | ||
*/ | ||
function RouteStore(app) { | ||
this.routeResolver = new RouteResolver(app); | ||
this.routes = []; | ||
return this; | ||
} | ||
const RouteResolver = require('./RouteResolver'); | ||
let routes = []; | ||
RouteStore.prototype.register = function register(method, path, response) { | ||
const route = { method, path, response }; | ||
this.routeResolver.register(route); | ||
this.routes.push(route); | ||
}; | ||
module.exports = { | ||
register: function register(method, path, response) { | ||
const route = { | ||
method, | ||
path, | ||
response | ||
}; | ||
RouteStore.prototype.reset = function reset() { | ||
this.routeResolver.unregister(this.routes); | ||
this.routes = []; | ||
}; | ||
RouteResolver.register(route); | ||
routes.push(route); | ||
}, | ||
reset: function reset() { | ||
RouteResolver.unregister(routes); | ||
routes = []; | ||
} | ||
}; | ||
module.exports = RouteStore; |
@@ -6,18 +6,8 @@ 'use strict'; | ||
const path = require('path'); | ||
const prepareConfig = require('./lib/prepareConfig'); | ||
const relativeRoot = require('./lib/relativeRoot'); | ||
let config; | ||
/** | ||
* Determine wrapping application root | ||
* Needed for searching for .mockyeah configuration file. | ||
*/ | ||
const root = path.resolve(__dirname, global.MOCK_YEAH_ROOT ? global.MOCK_YEAH_ROOT : '../..'); | ||
const configDefaults = { | ||
port: 4001, | ||
setsDir: './mockyeah/sets', | ||
fixturesDir: './mockyeah/fixtures' | ||
}; | ||
try { | ||
config = fs.readFileSync(path.resolve(root, '.mockyeah')); | ||
config = fs.readFileSync(path.resolve(relativeRoot, '.mockyeah')); | ||
config = JSON.parse(config); | ||
@@ -31,3 +21,3 @@ } catch (err) { | ||
if (!config) { | ||
config = fs.readFileSync(path.resolve(root, '.mock-yeah')); | ||
config = fs.readFileSync(path.resolve(relativeRoot, '.mock-yeah')); | ||
config = JSON.parse(config); | ||
@@ -39,2 +29,2 @@ } | ||
module.exports = Object.assign(configDefaults, config || {}); | ||
module.exports = prepareConfig(config); |
'use strict'; | ||
/* eslint-disable no-process-exit */ | ||
const gulp = require('gulp'); | ||
const eslint = require('gulp-eslint'); | ||
const mocha = require('gulp-mocha'); | ||
const PATH = require('./tasks/path'); | ||
const paths = { | ||
scripts: ['!node_modules/**', './**/*.js'], | ||
tests: ['./test/**/*-test.js'] | ||
}; | ||
// Load gulp tasks | ||
require('./tasks/lint'); | ||
require('./tasks/test'); | ||
gulp.task('lint', function lint() { | ||
return gulp.src(paths.scripts) | ||
.pipe(eslint()) | ||
.pipe(eslint.format()) | ||
.pipe(eslint.failAfterError()); | ||
gulp.task('tdd', ['lint:watch:run', 'test:watch:run'], () => { | ||
return gulp.watch(PATH.scripts, ['lint:watch:run', 'test:watch:run']); | ||
}); | ||
gulp.task('test', () => { | ||
return gulp.src(paths.tests) | ||
.pipe(mocha()) | ||
// Explicitly exits to avoid hanging | ||
.once('error', () => { | ||
process.exit(1); | ||
}) | ||
.once('end', () => { | ||
process.exit(); | ||
}); | ||
}); | ||
/** | ||
* Special `tdd-test` task exists due to `test` needing to explicitly exit | ||
*/ | ||
gulp.task('tdd-test', () => { | ||
return gulp.src(paths.tests) | ||
.pipe(mocha({ | ||
reporter: 'dot' | ||
})); | ||
}); | ||
gulp.task('tdd', function tdd() { | ||
gulp.watch(paths.scripts, ['lint', 'tdd-test']); | ||
}); | ||
gulp.task('default', ['lint']); | ||
gulp.task('default', ['lint', 'test']); |
22
index.js
'use strict'; | ||
/* eslint-disable no-console */ | ||
/* eslint-disable no-sync */ | ||
const app = require('./app'); | ||
const Server = require('./server'); | ||
const config = require('./config'); | ||
app.config = require('./config'); | ||
const httpServer = app.listen(app.config.port, function listen() { | ||
const host = this.address().address; | ||
const port = this.address().port; | ||
console.log(`Mock Yeah listening at http://${host}:${port}`); | ||
}); | ||
module.exports = app.RouteManager; | ||
module.exports.config = app.config; | ||
module.exports.close = httpServer.close.bind(httpServer, function callback() { | ||
console.log('Mock Yeah server closed.'); | ||
}); | ||
module.exports = new Server(config); |
{ | ||
"name": "mockyeah", | ||
"version": "0.12.1", | ||
"version": "0.13.0", | ||
"description": "An invaluable service mocking platform built on Express.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha test/**/*-test.js" | ||
"test": "mocha test/**/*Test.js" | ||
}, | ||
@@ -34,3 +34,5 @@ "repository": { | ||
"devDependencies": { | ||
"async": "^1.5.2", | ||
"chai": "^3.4.1", | ||
"dateformat": "^1.0.12", | ||
"eslint": "^1.10.3", | ||
@@ -41,2 +43,3 @@ "eslint-config-airbnb": "^2.1.1", | ||
"gulp-mocha": "^2.2.0", | ||
"rimraf": "^2.5.2", | ||
"supertest": "^1.1.0" | ||
@@ -46,4 +49,7 @@ }, | ||
"express": "^4.13.3", | ||
"lodash": "^3.10.1" | ||
"lodash": "^3.10.1", | ||
"mkdirp": "^0.5.1", | ||
"request": "^2.69.0", | ||
"tildify": "^1.1.2" | ||
} | ||
} |
255
README.md
@@ -1,2 +0,2 @@ | ||
# Mock Yeah [![Build Status](https://travis-ci.org/ryanricard/mockyeah.svg)](https://travis-ci.org/ryanricard/mockyeah) | ||
# mockyeah [![Build Status](https://travis-ci.org/ryanricard/mockyeah.svg)](https://travis-ci.org/ryanricard/mockyeah) | ||
@@ -16,2 +16,122 @@ __"An invaluable service mocking platform built on Express."__ | ||
- [Introductory tutorial](#Introductory-tutorial) | ||
- [Testing example](#Testing-example) | ||
## API | ||
### Mock service creation API | ||
__mockyeah.get(path, options)__<br/> | ||
__mockyeah.put(path, options)__<br/> | ||
__mockyeah.post(path, options)__<br/> | ||
__mockyeah.delete(path, options)__<br/> | ||
__mockyeah.all(path, options)__<br/> | ||
Each of the methods creates a mock service with a HTTP verb matching its respective method name. | ||
#### Parameters | ||
##### Path `String` | ||
Path to which to mount service. Fully supports all Express path matching options. | ||
##### Options `Object` | ||
Response options informing mockyeah how to respond to matching requests. Supported options: | ||
__One of the following options may be used per service:__ | ||
- `filePath` (`String`; optional) - File with contents to include in response body. Assumes response Content-Type of file type. | ||
- `fixture` (`String`; optional) - Fixture file with contents to include in response body. Assumes response Content-Type of file type. Default fixture file location is `./mockyeah/fixtures` in your project. | ||
- `html` (`String`; optional) - HTML to include in response body. Assumes response Content-Type of `text/html`. | ||
- `json` (`Object`; optional) - JSON to include in response body. Assumes response Content-Type of `application/json`. | ||
- `raw` (`String`; optional) - Text to include in response body. Content-Type is the default Express type if not specified in header. | ||
- `text` (`String`; optional) - Text to include in response body. Assumes response Content-Type of `text/plain`. | ||
__Additional options:__ | ||
- `headers` (`Object`; optional) - Header key value pairs to include in response. | ||
- `latency` (`Number` in Milliseconds; optional) - Used to control the response timing of a response. | ||
- `type` (`String`; optional) - Content-Type HTTP header to return with response. Proxies option to Express response method `res.type(type)`; more info here: http://expressjs.com/en/4x/api.html#res.type | ||
- `status` (`String`; optional; default: `200`) - HTTP response status code. | ||
### Fixture recording and playback | ||
__mockyeah.record(name)__ | ||
`name` (`String`; required) Directory name to save service responses recordings | ||
(i.e. `./mockyeah/fixtures/[recording name]`). | ||
Configures mockyeah to proxy and record service requests. Recorded responses | ||
are written to `./mockyeah/fixtures`. To use this feature, you must update | ||
the service addresses in your application to proxy through mockyeah. Here is an | ||
example of an address configured for recording: | ||
``` | ||
http://localhost:[mockyeah port]/http://example.com/your/service/url | ||
``` | ||
__mockyeah.play(name)__ | ||
`name` (`String`; required) Directory name from which to mount contained | ||
service responses recordings (i.e. `./mockyeah/fixtures/[recording name]`). | ||
Mounts each service response captured during a recording. Each service response | ||
will be mounted with exact same payload, headers, status, and latency as | ||
experienced during recording. This behavior may be changed by altering the values | ||
in the captured service response file. | ||
Here is an example of a service response file: | ||
```json | ||
{ | ||
"method": "GET", | ||
"url": "http://example.com/some/service", | ||
"path": "/some/service", | ||
"options": { | ||
"headers": { | ||
"x-powered-by": "Express", | ||
"content-type": "text/plain; charset=utf-8", | ||
"content-length": "12", | ||
"etag": "W/\"5-iwTV43ddKY54RV78XKQE1Q\"", | ||
"date": "Sun, 21 Feb 2016 06:17:49 GMT", | ||
"connection": "close" | ||
}, | ||
"status": 200, | ||
"raw": "Hello world!", | ||
"latency": 57 | ||
} | ||
} | ||
``` | ||
Pseudo recordings may be created manually to ease repetitive setup of multiple | ||
services. Here are the steps to creating a pseudo recording: | ||
1. Create a recording directory (e.g. `./mockyeah/fixtures/pseudo-example`) | ||
2. Add one or more JSON files containing the following properties, at minimum: | ||
```json | ||
{ | ||
"method": "GET", | ||
"path": "/some/service", | ||
"options": { | ||
"text": "Hello world!" | ||
} | ||
} | ||
``` | ||
See [Mock service creation API](#Mock-service-creation-API) for details on supported `options`. | ||
3. Play your pseudo recording. | ||
```js | ||
require('mockyeah').play('pseudo-example'); | ||
``` | ||
4. That's it! | ||
### Mock service and server management | ||
__mockyeah.reset()__ | ||
Removes all mounted mock services. Useful during after test teardown. | ||
__mockyeah.close()__ | ||
Stops mockyeah Express server. Useful when running mockyeah with a file watcher. | ||
mockyeah will attempt to start a new instance of Express with each iteration of | ||
test execution. After all tests run, `mockyeah.close()` should be called to | ||
shutdown mockyeah's Express server. Failing to do so will likely result in | ||
`EADDRINUSE` exceptions. This is due to mockyeah attempting to start a server on | ||
an occupied port. | ||
### Introductory tutorial | ||
@@ -57,2 +177,5 @@ 1. Create an example project and initialized with NPM | ||
// stop mockyeah server | ||
after(() => mockyeah.close()); | ||
it('should create a mock service that returns an internal error', (done) => { | ||
@@ -80,128 +203,18 @@ // create failing service mock | ||
## Package Dependencies | ||
- Mock Yeah was built and tested with Node v4.2.3 | ||
## Package dependencies | ||
- mockyeah was built and tested with Node v4.2.3 | ||
- [Mocha](https://mochajs.org/) | ||
## API | ||
## Release notes | ||
__0.13.0__ | ||
- Removed `.loadSet()` from API, easy multiple service setup is now possible | ||
with `.play()`. | ||
- Add fixture `.play()` functionality. See documentation. | ||
- Add fixture `.record()` functionality. See documentation. | ||
- Implements necessary changes to facilitate [mockyeah-cli](https://github.com/ryanricard/mockyeah-cli) | ||
- Added greater test coverage. | ||
### Mock Service Creation API | ||
__mockyeah.get(path, options)__<br/> | ||
__mockyeah.put(path, options)__<br/> | ||
__mockyeah.post(path, options)__<br/> | ||
__mockyeah.delete(path, options__<br/> | ||
Each of the methods above create a mock service with a HTTP verb matching its | ||
respective method name. | ||
#### Arguments | ||
##### Path | ||
Path to which to respond. Fully supports all Express path matching | ||
options. | ||
##### Options | ||
Response options informing Mock Yeah how to respond to matching requests. Supported options: | ||
- filePath (String; optional) - File with contents to include in response body. Assumes response Content-Type of file type. | ||
- fixture (String; optional) - Fixture file with contents to include in response body. Assumes response Content-Type of file type. Default fixture file location is `./mockyeah/fixtures` in your project. | ||
- headers (Object; optional) - Header key value pairs to include in response. | ||
- html (String; optional) - HTML to include in response body. Assumes response Content-Type of `text/html`. | ||
- json (Object; optional) - JSON to include in response body. Assumes response Content-Type of `application/json`. | ||
- latency (Number/Milliseconds; optional) - Used to control the response timing of a response. | ||
- text (String; optional) - Text to include in response body. Assumes response Content-Type of `text/plain`. | ||
- type (String; optional) - Content-Type HTTP header to return with response. Proxies option to Express response method `res.type(type)`; more info here: http://expressjs.com/en/4x/api.html#res.type | ||
- status (String; optional; default: 200) - HTTP response status code. | ||
Note, only one of the following is permitted per service: filePath, fixture, html, json, or text. | ||
### Mock Service Sets API | ||
__mockyeah.loadSet(setName)__<br/> | ||
Creates a set of mock services. Sets support the full Mock Service Creation API documented above. | ||
A set is a Node module that returns an array of service parameter objects, each being the definition for | ||
a single mock service. The default set file location is `./mockyeah/sets`. | ||
Example of usage: | ||
Invoking: | ||
```js | ||
mockyeah.loadSet('example') | ||
``` | ||
Creates mock services from definitions defined in `./mockyeah/sets/example.js` | ||
```js | ||
module.exports = [ | ||
{ | ||
method: 'get', | ||
path: '/say-hello', | ||
options: { | ||
text: 'Well, hello there.' | ||
} | ||
}, | ||
{ | ||
method: 'get', | ||
path: '/say-your-lost', | ||
options: { | ||
text: 'I\'m lost.', | ||
status: 404 | ||
} | ||
}, | ||
{ | ||
method: 'get', | ||
path: '/say-oh-noes', | ||
options: { | ||
text: 'Oh noes!', | ||
status: 500 | ||
} | ||
}, | ||
{ | ||
method: 'get', | ||
path: '/respond-with-a-file', | ||
options: { | ||
filePath: './test/fixtures/some-data.json' | ||
} | ||
}, | ||
{ | ||
method: 'get', | ||
path: '/respond-with-a-fixture', | ||
options: { | ||
fixture: 'some-data.json' | ||
} | ||
}, | ||
{ | ||
method: 'get', | ||
path: '/wait-to-respond', | ||
options: { | ||
text: 'Oh, hey there.', | ||
latency: 1000 | ||
} | ||
}, | ||
{ | ||
method: 'get', | ||
path: '/say-anything-you-want', | ||
options: (req, res) => { | ||
res.status(200); | ||
res.send('Inversion of service control enables you to respond with whatever you want.'); | ||
} | ||
} | ||
]; | ||
``` | ||
### Mock Service Management Methods | ||
#### mockyeah.reset() | ||
Resets all existing mock services. Useful on test teardown. | ||
#### mockyeah.close() | ||
Shuts down the Mock Yeah Express server. Useful if running Mock Yeah with a file | ||
watcher. Mock Yeah attempts to start a new instance of Express each test | ||
iteration. After all tests run, `mockyeah.close()` should be called to shutdown | ||
Mock Yeah's Express server. Failing to do so will result in `EADDRINUSE` | ||
exceptions. This is due to Mock Yeah attempting to start a server on a port | ||
occupied by a server it started previously. | ||
## Contributing | ||
### Getting started | ||
Installing project and dependencies | ||
@@ -208,0 +221,0 @@ ```shell |
'use strict'; | ||
global.MOCK_YEAH_ROOT = __dirname; | ||
global.MOCKYEAH_ROOT = __dirname; | ||
const mockyeah = require('mockyeah'); | ||
mockyeah.loadSet('example'); | ||
mockyeah.play('some-custom-fixtures'); |
'use strict'; | ||
const TestHelper = require('../TestHelper'); | ||
const mockyeah = TestHelper.mockyeah; | ||
const request = TestHelper.request; | ||
const mockyeah = TestHelper.mockyeah; | ||
describe('Response', () => { | ||
afterEach(() => mockyeah.reset()); | ||
describe('Response Headers', () => { | ||
it('should support custom headers', (done) => { | ||
mockyeah.get('/some/service/end/point', { text: 'Hello.', headers: { 'Foo-Bar': 'abc' } }); | ||
describe('Headers', () => { | ||
it('should support custom headers', (done) => { | ||
mockyeah.get('/some/service/end/point', { text: 'Hello.', headers: { 'Foo-Bar': 'abc' } }); | ||
request | ||
.get('/some/service/end/point') | ||
.expect('Foo-Bar', 'abc') | ||
.expect(200, /Hello/, done); | ||
}); | ||
request | ||
.get('/some/service/end/point') | ||
.expect('Foo-Bar', 'abc') | ||
.expect(200, /Hello/, done); | ||
}); | ||
it('should send header Content-Type when set and raw', (done) => { | ||
mockyeah.get('/some/service/end/point', { raw: 'Hello.', headers: { 'content-type': 'application/xml' } }); | ||
it('should send header Content-Type when set and raw', (done) => { | ||
mockyeah.get('/some/service/end/point', { raw: 'Hello.', headers: { 'content-type': 'application/xml' } }); | ||
request | ||
.get('/some/service/end/point') | ||
.expect('Content-Type', /application\/xml/) | ||
.expect(200, /Hello/, done); | ||
}); | ||
request | ||
.get('/some/service/end/point') | ||
.expect('Content-Type', /application\/xml/) | ||
.expect(200, /Hello/, done); | ||
}); | ||
}); |
'use strict'; | ||
const TestHelper = require('../TestHelper'); | ||
const mockyeah = TestHelper.mockyeah; | ||
const request = TestHelper.request; | ||
const mockyeah = TestHelper.mockyeah; | ||
const expect = require('chai').expect; | ||
describe('Response', () => { | ||
afterEach(() => mockyeah.reset()); | ||
describe('Response Latency', () => { | ||
it('should respond with latency', (done) => { | ||
const latency = 1000; | ||
const threshold = latency + 200; | ||
describe('Latency', () => { | ||
it('should respond with latency', (done) => { | ||
const latency = 1000; | ||
const threshold = latency + 100; | ||
mockyeah.get('/slow/service', { text: 'Hello', latency }); | ||
mockyeah.get('/slow/service', { text: 'Hello', latency }); | ||
const start = (new Date).getTime(); | ||
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); | ||
}); | ||
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; | ||
it('should respond with no latency', (done) => { | ||
const threshold = 25; | ||
mockyeah.get('/fast/service', { text: 'Hello' }); | ||
mockyeah.get('/fast/service', { text: 'Hello' }); | ||
const start = (new Date).getTime(); | ||
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); | ||
}); | ||
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); | ||
}); | ||
}); |
'use strict'; | ||
const TestHelper = require('../TestHelper'); | ||
const mockyeah = TestHelper.mockyeah; | ||
const request = TestHelper.request; | ||
const mockyeah = TestHelper.mockyeah; | ||
describe('Response', () => { | ||
afterEach(() => mockyeah.reset()); | ||
describe('Response Status', () => { | ||
it('should return 404 for undeclared services', (done) => { | ||
request | ||
.get('/some/non/existent/service/end/point') | ||
.expect(404, done); | ||
}); | ||
describe('Status', () => { | ||
it('should return 404 for undeclared services', (done) => { | ||
request | ||
.get('/some/non/existent/service/end/point') | ||
.expect(404, done); | ||
}); | ||
it('should return a default status code of 200', (done) => { | ||
mockyeah.get('/service/exists'); | ||
it('should return a default status code of 200', (done) => { | ||
mockyeah.get('/service/exists'); | ||
request | ||
.get('/service/exists') | ||
.expect(200, done); | ||
}); | ||
request | ||
.get('/service/exists') | ||
.expect(200, done); | ||
}); | ||
it('should support declarative status code 301', (done) => { | ||
mockyeah.get('/some/service/end/point', { status: 301 }); | ||
it('should support declarative status code 301', (done) => { | ||
mockyeah.get('/some/service/end/point', { status: 301 }); | ||
request | ||
.get('/some/service/end/point') | ||
.expect(301, done); | ||
}); | ||
request | ||
.get('/some/service/end/point') | ||
.expect(301, done); | ||
}); | ||
it('should support declarative status code 500', (done) => { | ||
mockyeah.get('/some/service/end/point', { status: 500 }); | ||
it('should support declarative status code 500', (done) => { | ||
mockyeah.get('/some/service/end/point', { status: 500 }); | ||
request | ||
.get('/some/service/end/point') | ||
.expect(500, done); | ||
}); | ||
request | ||
.get('/some/service/end/point') | ||
.expect(500, done); | ||
}); | ||
}); |
'use strict'; | ||
global.MOCK_YEAH_ROOT = __dirname; | ||
global.MOCKYEAH_ROOT = __dirname; | ||
global.MOCKYEAH_SUPPRESS_OUTPUT = true; | ||
global.MOCKYEAH_VERBOSE_OUTPUT = false; | ||
const mockyeah = require('../index.js'); | ||
const request = require('supertest'); | ||
const mockyeah = require('../index.js'); | ||
@@ -12,3 +14,7 @@ after(() => { | ||
module.exports.mockyeah = mockyeah; | ||
module.exports.request = request('http://localhost:4041'); | ||
afterEach(() => { | ||
mockyeah.reset(); | ||
}); | ||
exports.mockyeah = mockyeah; | ||
exports.request = request(mockyeah.server); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
49
990
233
45810
5
10
4
1
+ Addedmkdirp@^0.5.1
+ Addedrequest@^2.69.0
+ Addedtildify@^1.1.2
+ Addedajv@6.12.6(transitive)
+ Addedasn1@0.2.6(transitive)
+ Addedassert-plus@1.0.0(transitive)
+ Addedasynckit@0.4.0(transitive)
+ Addedaws-sign2@0.7.0(transitive)
+ Addedaws4@1.13.2(transitive)
+ Addedbcrypt-pbkdf@1.0.2(transitive)
+ Addedcaseless@0.12.0(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addedcore-util-is@1.0.2(transitive)
+ Addeddashdash@1.14.1(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedecc-jsbn@0.1.2(transitive)
+ Addedextend@3.0.2(transitive)
+ Addedextsprintf@1.3.0(transitive)
+ Addedfast-deep-equal@3.1.3(transitive)
+ Addedfast-json-stable-stringify@2.1.0(transitive)
+ Addedforever-agent@0.6.1(transitive)
+ Addedform-data@2.3.3(transitive)
+ Addedgetpass@0.1.7(transitive)
+ Addedhar-schema@2.0.0(transitive)
+ Addedhar-validator@5.1.5(transitive)
+ Addedhttp-signature@1.2.0(transitive)
+ Addedis-typedarray@1.0.0(transitive)
+ Addedisstream@0.1.2(transitive)
+ Addedjsbn@0.1.1(transitive)
+ Addedjson-schema@0.4.0(transitive)
+ Addedjson-schema-traverse@0.4.1(transitive)
+ Addedjson-stringify-safe@5.0.1(transitive)
+ Addedjsprim@1.4.2(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedmkdirp@0.5.6(transitive)
+ Addedoauth-sign@0.9.0(transitive)
+ Addedos-homedir@1.0.2(transitive)
+ Addedperformance-now@2.1.0(transitive)
+ Addedpsl@1.13.0(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedqs@6.5.3(transitive)
+ Addedrequest@2.88.2(transitive)
+ Addedsshpk@1.18.0(transitive)
+ Addedtildify@1.2.0(transitive)
+ Addedtough-cookie@2.5.0(transitive)
+ Addedtunnel-agent@0.6.0(transitive)
+ Addedtweetnacl@0.14.5(transitive)
+ Addeduri-js@4.4.1(transitive)
+ Addeduuid@3.4.0(transitive)
+ Addedverror@1.10.0(transitive)