simpler-mocks
Advanced tools
Comparing version 1.5.1 to 1.6.0
{ | ||
"name": "simpler-mocks", | ||
"version": "1.5.1", | ||
"version": "1.6.0", | ||
"description": "REST API mock server made simple. Runs on Node with YAML and JSON mock definitions.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -94,3 +94,7 @@ const fs = require('fs') | ||
if (mock[SCHEMA_KEYS.status]) ctx.status = mock[SCHEMA_KEYS.status] | ||
if (mock[SCHEMA_KEYS.status]) { | ||
const mockStatus = mock[SCHEMA_KEYS.status] | ||
ctx.status = mockStatus instanceof util.MockRegExp ? Number(mockStatus.data) : mockStatus | ||
} | ||
if (mock[SCHEMA_KEYS.headers]) ctx.set(mock[SCHEMA_KEYS.headers]) | ||
@@ -97,0 +101,0 @@ if (mock[SCHEMA_KEYS.response]) ctx.body = mock[SCHEMA_KEYS.response] |
@@ -18,6 +18,4 @@ const fs = require('fs') | ||
kind: 'scalar', | ||
resolve: function(data) { | ||
return data !== null | ||
}, | ||
construct: function(name) { | ||
resolve: (data) => data !== null, | ||
construct: (name) => { | ||
let fileContent, output | ||
@@ -57,8 +55,4 @@ const fixture = cache.fixtures[name] | ||
kind: 'scalar', | ||
resolve: function(data) { | ||
return data !== null | ||
}, | ||
construct: function(data) { | ||
return _get(cache.request, data, {}) | ||
} | ||
resolve: (data) => data !== null, | ||
construct: (data) => _get(cache.request, data, {}) | ||
}) | ||
@@ -69,6 +63,4 @@ | ||
kind: 'scalar', | ||
resolve: function(data) { | ||
return data !== null | ||
}, | ||
construct: function(data) { | ||
resolve: (data) => data !== null, | ||
construct: (data) => { | ||
let type, options | ||
@@ -88,11 +80,25 @@ const indexOfSpace = data.indexOf(' ') | ||
// match any type of data | ||
const AnyType = new yaml.Type('!any', { | ||
kind: 'scalar', | ||
construct: function(data) { | ||
return new util.Any(data) | ||
}, | ||
construct: (data) => new util.Any(data), | ||
instanceOf: util.Any | ||
}) | ||
MOCK_TAGS = yaml.Schema.create([AnyType, ChanceType, IncludeType, RequestType]) | ||
// custom !regexp tag, based on the original !!js/regexp tag definition | ||
const jsRegExp = yaml.DEFAULT_FULL_SCHEMA.compiledTypeMap.scalar['tag:yaml.org,2002:js/regexp'] | ||
const mockRegExpOptions = { | ||
...jsRegExp, | ||
instanceOf: util.MockRegExp, | ||
construct: (data) => { | ||
const regexp = jsRegExp.construct(data) | ||
return new util.MockRegExp(regexp) | ||
} | ||
} | ||
delete mockRegExpOptions.tag | ||
delete mockRegExpOptions.predicate | ||
const MockRegExpType = new yaml.Type('!regexp', mockRegExpOptions) | ||
MOCK_TAGS = yaml.Schema.create([AnyType, ChanceType, IncludeType, RequestType, MockRegExpType]) | ||
module.exports = MOCK_TAGS |
@@ -85,2 +85,10 @@ const cache = require('./cache') | ||
return tester.test(val) | ||
} else if (tester instanceof MockRegExp) { | ||
const matches = tester.pattern.exec(val) | ||
if (matches && matches.length > 1) { | ||
tester.data = matches.length === 2 ? matches[1] : matches.slice(1) | ||
} | ||
return !!matches | ||
} else if (typeof tester === 'function') { | ||
@@ -91,7 +99,17 @@ return tester(val) | ||
// the Any class to be used for the custom !any tag | ||
// type = boolean|string|number|array | ||
function Any(type) { | ||
if (type) { | ||
this.type = type.toLowerCase() | ||
/** | ||
* @class | ||
* the Any class to be used to identify the custom !any tag | ||
* | ||
* @property {string} type | ||
*/ | ||
class Any { | ||
/** | ||
* constructors new Any | ||
* @param {string} type the type of data this tag will match, could be boolean|string|number|array | ||
*/ | ||
constructor(type) { | ||
if (type) { | ||
this.type = type.toLowerCase() | ||
} | ||
} | ||
@@ -101,2 +119,24 @@ } | ||
/** | ||
* @class | ||
* Custom regexp Class that outputs the content of data to when dumping JSON | ||
* | ||
* @property {RegExp} pattern | ||
* @property {*} data the value of this object to be outputed using toJSON | ||
*/ | ||
class MockRegExp { | ||
/** | ||
* constructs new MockRegExp | ||
* @param {*} pattern the regexp pattern for this object | ||
*/ | ||
constructor(pattern) { | ||
this.pattern = pattern | ||
this.data = pattern | ||
} | ||
toJSON() { | ||
return this.data | ||
} | ||
} | ||
/** | ||
* Verbose logging utility function | ||
@@ -116,3 +156,4 @@ */ | ||
log, | ||
parseOptions | ||
parseOptions, | ||
MockRegExp | ||
} |
29136
644