Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

swagmock

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

swagmock - npm Package Compare versions

Comparing version 0.0.4 to 1.0.0-alpha.1

74

lib/generators/format.js
'use strict';
var Moment = require('moment');
var Chance = require('chance').Chance();
var Randexp = require('randexp').randexp;
const Moment = require('moment');
const Chance = require('chance').Chance();
const Randexp = require('randexp').randexp;
const date = () => Moment().format('YYYY-MM-DD');
const dataTime = () => Moment().toISOString();
const url = () => Chance.url();
const email = () => Chance.email();
const phone = () => Chance.phone();
const guid = () => Chance.guid();
const ipv4 = () => Chance.ip();
const ipv6 = () => Chance.ipv6();
const hostname = () => Randexp(/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/gm);
module.exports = {
date: dateMock,
'date-time': dataTimeMock,
uri: urlMock,
url: urlMock,
email: emailMock,
phone: phoneMock,
uuid: guidMock,
guid: guidMock,
ipv4: ipv4,
ipv6: ipv6,
hostname: hostname
date,
'date-time': dataTime,
uri: url,
url,
email,
phone,
uuid: guid,
guid,
ipv4,
ipv6,
hostname
};
function dateMock() {
return Moment().format('YYYY-MM-DD');
}
function dataTimeMock() {
return Moment().toISOString();
}
function urlMock() {
return Chance.url();
}
function emailMock() {
return Chance.email();
}
function phoneMock() {
return Chance.phone();
}
function guidMock() {
return Chance.guid();
}
function ipv4() {
return Chance.ip();
}
function ipv6() {
return Chance.ipv6();
}
function hostname() {
return Randexp(/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/gm);
}
'use strict';
var Chance = require('chance').Chance();
var Format = require('./format');
var Util = require('../util');
var Randexp = require('randexp').randexp;
const Chance = require('chance').Chance();
const Format = require('./format');
const Randexp = require('randexp').randexp;
var Generators = module.exports = {
object: objectMock,
array: arrayMock,
string: stringMock,
integer: integerMock,
number: numberMock,
boolean: booleanMock,
file: fileMock,
mock: mock
};
function mock(schema) {
var mock;
const mock = schema => {
let mock;
if (schema) {
var type = schema.type || findType(schema);
let type = schema.type || findType(schema);
/**
* Get the mock generator from the `type` of the schema
*/
var generator = Generators[type];
const generator = Generators[type];
if (generator) {

@@ -31,10 +19,9 @@ mock = generator.call(null, schema);

return mock;
}
};
function objectMock(schema) {
var mockObj = {};
var props = schema.properties;
if (props) {
Object.keys(props).forEach(function (key) {
mockObj[key] = mock(props[key]);
const objectMock = ({ properties, additionalProperties }) => {
let mockObj = {};
if (properties) {
Object.keys(properties).forEach(function (key) {
mockObj[key] = mock(properties[key]);
});

@@ -48,8 +35,8 @@ /**

*/
} else if (schema.additionalProperties) {
} else if (additionalProperties) {
//Create a random property
mockObj[Chance.word()] = mock(schema.additionalProperties);
mockObj[Chance.word()] = mock(additionalProperties);
}
return mockObj;
}
};
/**

@@ -60,15 +47,14 @@ * Generates a mock `array` data of `items`

*/
function arrayMock(schema) {
var items = schema.items;
var min;
var max;
var numItems;
var arr = [];
const arrayMock = ({ items, minItems, maxItems }) => {
let min;
let max;
let numItems;
let arr = [];
if (items) {
//Use the min as the base
min = schema.minItems || 1;
if (schema.maxItems) {
min = minItems || 1;
if (maxItems) {
//If min is greater than max, use min as max.
max = (schema.maxItems < min) ? min : schema.maxItems;
max = (maxItems < min) ? min : maxItems;
} else {

@@ -83,3 +69,3 @@ //If max is not defined, use min as max.

});
for (var i = 0; i < numItems; i++) {
for (let i = 0; i < numItems; i++) {
arr.push(mock(items));

@@ -89,3 +75,3 @@ }

return arr;
}
};
/**

@@ -96,5 +82,5 @@ * Generates a mock `integer` value

*/
function integerMock(schema) {
var opts = {};
var intmock;
const integerMock = schema => {
let opts = {};
let intmock;

@@ -108,14 +94,16 @@ /**

if (Util.isInteger(schema.minimum)) {
opts.min = (schema.exclusiveMinimum) ? schema.minimum : schema.minimum + 1;
if (Number.isInteger(schema.minimum)) {
opts.min = (schema.exclusiveMinimum) ? schema.minimum + 1 : schema.minimum;
}
if (Util.isInteger(schema.maximum)) {
opts.max = (schema.exclusiveMaximum) ? schema.maximum : schema.maximum - 1;
if (Number.isInteger(schema.maximum)) {
opts.max = (schema.exclusiveMaximum) ? schema.maximum - 1 : schema.maximum;
}
//Generate a number that is multiple of schema.multipleOf
if (Util.isInteger(schema.multipleOf) && schema.multipleOf > 0) {
//Use the muplilier as the min number
opts.min = schema.multipleOf;
if (Number.isInteger(schema.multipleOf) && schema.multipleOf > 0) {
//Use the min/muplilier as the min number
//Use default min as 1 if min is not properly set.
opts.min = (Number.isInteger(opts.min)) ? (Math.ceil(opts.min / schema.multipleOf)) : 1;
//Use the max/muplilier as the new max value
opts.max = (Util.isInteger(opts.max)) ? (Math.floor(opts.max/schema.multipleOf)) : opts.max;
//Use a default - min + 10 - if max value is not properly set.
opts.max = (Number.isInteger(opts.max)) ? (Math.floor(opts.max / schema.multipleOf)) : (opts.min + 10);
intmock = Chance.integer(opts);

@@ -127,3 +115,3 @@ intmock = intmock * schema.multipleOf;

return intmock;
}
};

@@ -135,5 +123,5 @@ /**

*/
function numberMock(schema) {
var opts = {};
var nummock;
const numberMock = schema => {
let opts = {};
let nummock;

@@ -147,15 +135,18 @@ /**

if (schema.minimum) {
opts.min = (schema.exclusiveMinimum) ? schema.minimum : schema.minimum + 0.1;
if (Number.isFinite(schema.minimum)) {
opts.min = (schema.exclusiveMinimum) ? schema.minimum + 0.1 : schema.minimum;
}
if (schema.maximum) {
opts.max = (schema.exclusiveMaximum) ? schema.maximum : schema.maximum - 0.1;
if (Number.isFinite(schema.maximum)) {
opts.max = (schema.exclusiveMaximum) ? schema.maximum - 0.1 : schema.maximum ;
}
//Generate a number that is multiple of schema.multipleOf
if (schema.multipleOf > 0) {
//Use the muplilier as the min number
opts.min = schema.multipleOf;
if (Number.isFinite(schema.multipleOf) && schema.multipleOf > 0) {
//Use the min/muplilier as the min number
//Use default min as 1 if min is not properly set
opts.min = (Number.isFinite(opts.min)) ? (Math.ceil(opts.min / schema.multipleOf)) : 1;
//Use the max/muplilier as the new max value
opts.max = (opts.max) ? opts.max/schema.multipleOf : opts.max;
nummock = Chance.floating(opts);
//Use a default - min + 10 - if max value is not properly set.
opts.max = (Number.isFinite(opts.max)) ? (Math.floor(opts.max / schema.multipleOf)) : (opts.min + 10);
nummock = Chance.integer(opts);
nummock = nummock * schema.multipleOf;

@@ -166,5 +157,5 @@ } else {

return nummock;
}
};
function booleanMock(schema) {
const booleanMock = schema => {
/**

@@ -177,13 +168,13 @@ * If `enum` is defined for the property

return Chance.bool();
}
};
/**
* Geneartes a mock `string` value
* Supports: `minLength`, `maxLength`, `enum`, `date`, and `date-time`
* TODO : `pattern`
*
*/
function stringMock(schema) {
var mockStr;
var opts = {};
var minLength = schema.minLength || 1;
var maxLength = schema.maxLength || minLength + 10;
const stringMock = schema => {
let mockStr;
let opts = {};
let minLength = schema.minLength || 1;
let maxLength = schema.maxLength || minLength + 10;
opts.min = minLength;

@@ -215,7 +206,7 @@ opts.max = maxLength;

return mockStr;
}
};
function enumMock(schema) {
var len = schema.enum.length;
var opts = {
const enumMock = schema => {
let len = schema.enum.length;
let opts = {
min: 0,

@@ -225,12 +216,12 @@ max: len - 1

return schema.enum[Chance.integer(opts)];
}
};
function fileMock() {
const fileMock = () => {
return Chance.file();
}
};
//Find out the type based on schema props
//(This is not a complete list or full proof solution)
function findType(schema) {
var type = 'object';// Use 'object' as the default type
const findType = schema => {
let type = 'object';// Use 'object' as the default type
if (schema.pattern) {

@@ -242,2 +233,13 @@ type = 'string';

return type;
}
};
const Generators = module.exports = {
object: objectMock,
array: arrayMock,
string: stringMock,
integer: integerMock,
number: numberMock,
boolean: booleanMock,
file: fileMock,
mock
};

@@ -1,36 +0,17 @@

'use strict';
var Generators = require('./index');
const Generators = require('./index');
module.exports = {
query: queryMock,
path: queryMock,
formData: queryMock,
header: queryMock,
body: bodyMock
const collectionFormat = {
csv : val => val.join(','),
ssv: val => val.join(' '),
tsv: val => val.join('\t'),
pipes: val => val.join('|'),
multi: val => val
};
var collectionFormat = {
csv : function (val) {
return val.join(',');
},
ssv: function (val) {
return val.join(' ');
},
tsv: function (val) {
return val.join('\t');
},
pipes: function (val) {
return val.join('|');
},
multi: function (val) {
return val;
}
};
/**
* TODO : Handle type `file`
*/
function queryMock(param) {
var mock = {};
var value = Generators.mock(param);
var separator = collectionFormat.csv;
const queryMock = param => {
let mock = {};
let value = Generators.mock(param);
let separator = collectionFormat.csv;
//`collectionFormat` Determines the format of the array if type array is used.

@@ -46,5 +27,5 @@ // Possible values are: csv, ssv, tsv. pipes and multi

return mock;
}
};
function bodyMock(param) {
const bodyMock = param => {
return {

@@ -54,2 +35,10 @@ name: param.name,

};
}
};
module.exports = {
query: queryMock,
path: queryMock,
formData: queryMock,
header: queryMock,
body: bodyMock
};

@@ -1,48 +0,46 @@

'use strict';
var Parser = require('swagger-parser');
var Generators = require('./generators');
var ParamTypes = require('./generators/paramtypes');
var Util = require('./util');
var Querystring = require('querystring');
const Parser = require('swagger-parser');
const Generators = require('./generators');
const ParamTypes = require('./generators/paramtypes');
const Querystring = require('querystring');
const Maybe = require('call-me-maybe');
module.exports = function (apiPath) {
return new SwagMock(apiPath);
const OPERATIONS = ['get', 'put', 'post', 'delete', 'options', 'head', 'patch'];
module.exports = (api, options) => {
return new SwagMock(api, options);
};
function SwagMock(apiPath) {
this.swagger = Parser.validate(apiPath);
function SwagMock(api, { validated } = {}) {
//If the api is an already validated Object, use it as it is.
//If not validated, Parse and validate using 'swagger-parser'
this.swagger = validated ? Promise.resolve(api) : Parser.validate(api);
}
SwagMock.prototype.responses = function(options, callback) {
options = options || {};
SwagMock.prototype.responses = function(options = {}, callback) {
options.mockResponses = true;
this.mock(options, callback);
return Maybe(callback, this.mock(options));
};
SwagMock.prototype.parameters = function(options, callback) {
options = options || {};
SwagMock.prototype.parameters = function(options = {}, callback) {
options.mockParams = true;
this.mock(options, callback);
return Maybe(callback, this.mock(options));
};
SwagMock.prototype.requests = function(options, callback) {
options = options || {};
SwagMock.prototype.requests = function(options = {}, callback) {
options.mockRequest = true;
this.mock(options, callback);
return Maybe(callback, this.mock(options));
};
SwagMock.prototype.mock = function(options, callback) {
options = options || {};
this.swagger.then(function(api) {
callback(null, mockSchema(api, options));
}).catch(function(error) {
callback(error);
SwagMock.prototype.mock = function(options = {}) {
return this.swagger.then(api => {
return mockSchema(api, options);
});
};
function mockSchema(api, options) {
var mock = {};
var paths = api.paths;
const mockSchema = (api, options) => {
let mock = {};
let paths = api.paths;
if (paths) {
var pathObj = paths[options.path];
let pathObj = paths[options.path];
if (pathObj) {

@@ -54,5 +52,5 @@ //Found the requested path

Object.keys(paths).forEach(function(pathStr) {
var pathObj = paths[pathStr];
let pathObj = paths[pathStr];
if (pathObj) {
var pathMock = {};
let pathMock = {};
mockPath(pathStr, pathObj, pathMock, options);

@@ -65,11 +63,11 @@ mock[pathStr] = pathMock;

return mock;
}
};
/**
*
* Generate mock for the path
*/
function mockPath(pathStr, pathObj, mock, options) {
var opsObj = pathObj[options.operation];
const mockPath = (pathStr, pathObj, mock, options) => {
let opsObj = pathObj[options.operation];
//Common parameters - A list of parameters that are applicable for
//all the operations described under this path
var commParams = pathObj.parameters;
let commParams = pathObj.parameters;
if (opsObj) {

@@ -84,5 +82,5 @@ //Found the operation

Object.keys(pathObj).forEach(function(operation) {
if (pathObj[operation] && Util.OPERATIONS.indexOf(operation) !== -1) {
if (pathObj[operation] && OPERATIONS.includes(operation)) {
//Valid operation.
var opsMock = {};
let opsMock = {};
mockOperation({

@@ -97,7 +95,7 @@ path: pathStr,

}
}
};
/**
*
* Generate mock for the operation
*/
function mockOperation(resolved, opsObj, mock, options) {
const mockOperation = (resolved, opsObj, mock, options) => {
//Mock response

@@ -116,3 +114,3 @@ if (options.mockResponses) {

}
}
};
/**

@@ -122,7 +120,7 @@ * Generate a mock responses

*/
function mockResponses(opsObj, options) {
var mockResp;
var responses = opsObj.responses;
const mockResponses = (opsObj, options) => {
let mockResp;
let responses = opsObj.responses;
if (responses) {
var response = responses[options.response];
let response = responses[options.response];
if (response) {

@@ -134,3 +132,3 @@ //Found the response

Object.keys(responses).forEach(function(responseStr) {
var response = responses[responseStr];
let response = responses[responseStr];
if (response) {

@@ -143,9 +141,9 @@ mockResp[responseStr] = mockResponse(response);

return mockResp;
}
};
/**
*
*/
function mockResponse(response) {
var mockResp;
var schema = response.schema;
const mockResponse = response => {
let mockResp;
let schema = response.schema;
if (schema) {

@@ -155,3 +153,3 @@ mockResp = Generators.mock(schema);

return mockResp;
}
};
/**

@@ -161,7 +159,6 @@ * Generate a mock parameter list

*/
function mockParameters(resolved, opsObj) {
var mockParam = {};
var parameters = resolved.commonParams || [];
const mockParameters = (resolved, opsObj) => {
let mockParam = {};
//Combine common parameters
parameters = parameters.concat(opsObj.parameters || []);
let parameters = mergeParams(resolved.commonParams, opsObj.parameters);
if (parameters.length > 0) {

@@ -172,3 +169,3 @@ //Iterate over each parameter

// Possible values are "query", "header", "path", "formData" or "body".
var paramType = param.in;
let paramType = param.in;
if (ParamTypes[paramType]) {

@@ -182,12 +179,12 @@ //Found the Mock generator for the param type (AKA `location`, AKA `in`).

return mockParam;
}
};
/**
* Generates the mock request objects that can be used for tests
*/
function mockRequest(resolved, parameters) {
var mock = {};
var queryObj = {};
var headerObj = {};
var formObj = {};
var pathname = resolved.path;
const mockRequest = (resolved, parameters) => {
let mock = {};
let queryObj = {};
let headerObj = {};
let formObj = {};
let pathname = resolved.path;

@@ -243,2 +240,25 @@ if (parameters) {

return mock;
}
};
/**
* Merge the common parameters at the path level to the operation parameters.
* Common parameters are a list of parameters that are applicable for all the operations
* described under a particular path. These parameters can be overridden at the operation level,
* but cannot be removed there.
*/
const mergeParams = (commonParams, opsParams) => {
let paramMap = new Map();
if (commonParams) {
for (let param of commonParams) {
//A unique parameter is defined by a combination of a name and location
paramMap.set(param.name + param.in, param);
}
}
if (opsParams) {
for (let param of opsParams) {
//A unique parameter is defined by a combination of a name and location
paramMap.set(param.name + param.in, param);
}
}
return Array.from(paramMap.values());
};
{
"name": "swagmock",
"version": "0.0.4",
"version": "1.0.0-alpha.1",
"description": "Mock data generator for swagger api",
"main": "lib/index.js",
"engines": {
"node": ">=6.x"
},
"scripts": {

@@ -30,2 +33,3 @@ "lint": "eslint lib",

"dependencies": {
"call-me-maybe": "^1.0.1",
"chance": "^1.0.3",

@@ -32,0 +36,0 @@ "moment": "^2.13.0",

@@ -175,2 +175,10 @@ {

"/pet/{petId}": {
"parameters": [{
"name": "petId",
"in": "path",
"description": "ID of pet to return",
"required": true,
"type": "integer",
"format": "int64"
}],
"get": {

@@ -188,2 +196,4 @@ "tags": ["pet"],

"type": "integer",
"minimum": 1000,
"maximum": 2000,
"format": "int64"

@@ -198,2 +208,21 @@ },

"pattern": "awesome+ (pet|cat|bird)"
},
{
"name": "petWeight",
"in": "query",
"description": "Weight of pet to return",
"required": false,
"type": "number",
"minimum": 10,
"maximum": 500
},
{
"name": "bmi",
"in": "query",
"description": "bmi of the pet",
"required": false,
"type": "number",
"minimum": 0,
"maximum": 1,
"multipleOf": 0.2
}],

@@ -225,10 +254,3 @@ "responses": {

"produces": ["application/xml", "application/json"],
"parameters": [{
"name": "petId",
"in": "path",
"description": "ID of pet that needs to be updated",
"required": true,
"type": "integer",
"format": "int64"
}, {
"parameters": [ {
"name": "name",

@@ -266,9 +288,2 @@ "in": "formData",

"type": "string"
}, {
"name": "petId",
"in": "path",
"description": "Pet id to delete",
"required": true,
"type": "integer",
"format": "int64"
}],

@@ -302,2 +317,6 @@ "responses": {

"type": "integer",
"exclusiveMinimum": true,
"exclusiveMaximum": true,
"minimum": 1000,
"maximum": 1010,
"format": "int64"

@@ -767,2 +786,5 @@ }, {

"type": "integer",
"multipleOf": 100,
"minimum": 1000,
"exclusiveMinimum": true,
"format": "int32",

@@ -769,0 +791,0 @@ "description": "User Status"

@@ -1,26 +0,23 @@

var Assert = require('assert');
var Swagmock = require('../lib');
var Path = require('path')
//isInteger pollyfil for pre es6
Number.isInteger = Number.isInteger || function(value) {
return typeof value === "number" &&
isFinite(value) &&
Math.floor(value) === value;
};
const Assert = require('assert');
const Swagmock = require('../lib');
const Path = require('path');
const Parser = require('swagger-parser');
describe('Parameter Mock generator', function () {
var apiPath = Path.resolve(__dirname, 'fixture/petstore.json');
var swagmock = Swagmock(apiPath);
it('should generate parameter mock for path /store/order/{orderId}', function(done) {
describe('Parameter Mock generator', () => {
let apiPath = Path.resolve(__dirname, 'fixture/petstore.json');
let apiResolver = Parser.validate(apiPath);
//Test case of valiadted api use case.
let swagmock = Swagmock(apiResolver, { validated: true });
it('should generate parameter mock for path /store/order/{orderId}', done => {
swagmock.parameters({
path: '/store/order/{orderId}',
operation: 'get'
}, function(err, mock) {
}, (err, mock) => {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
var params = mock.parameters;
let params = mock.parameters;
Assert.ok(params, 'Generated parameters');
Assert.ok(params.path, 'Generated path parameter');
Assert.ok(params.path[0].name === 'orderId', 'generated mock parameter for orderId');
Assert.ok(params.path[0].value > 0 && params.path[0].value < 10, 'OK value for orderId');
Assert.ok(params.path[0].value >= 1 && params.path[0].value <= 10, 'OK value for orderId');
done();

@@ -30,10 +27,10 @@ });

it('should generate parameter mock for path /pet/findByStatus', function(done) {
it('should generate parameter mock for path /pet/findByStatus', done => {
swagmock.parameters({
path: '/pet/findByStatus',
operation: 'get'
}, function(err, mock) {
}, (err, mock) => {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
var params = mock.parameters;
let params = mock.parameters;
Assert.ok(params, 'Generated parameters');

@@ -48,10 +45,10 @@ Assert.ok(params.query, 'Generated query parameter');

it('should generate parameter mock for path /pet/{petId}', function(done) {
it('should generate parameter mock for path /pet/{petId}', done => {
swagmock.parameters({
path: '/pet/{petId}',
operation: 'get'
}, function(err, mock) {
}, (err, mock) => {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
var params = mock.parameters;
let params = mock.parameters;
Assert.ok(params, 'Generated parameters');

@@ -61,6 +58,20 @@ Assert.ok(params.path, 'Generated path parameter');

Assert.ok(Number.isInteger(params.path[0].value), 'OK value for petId');
//Test the operation level overrides
Assert.ok(params.path[0].value >= 1000 && params.path[0].value <= 2000, 'OK value for petId');
Assert.ok(params.query, 'Generated query parameter');
Assert.ok(params.query[0].name === 'petName', 'generated mock parameter for petName');
Assert.ok(/awesome+ (pet|cat|bird)/.test(params.query[0].value), 'OK value for petName');
params.query.forEach(param => {
if (param.name === 'petName') {
Assert.ok(/awesome+ (pet|cat|bird)/.test(param.value), 'OK value for petName');
}
if (param.name === 'petWeight') {
Assert.ok(Number.isFinite(param.value), 'OK value for petWeight');
Assert.ok(param.value <= 500 && param.value >= 10, 'OK value for petWeight');
}
if (param.name === 'bmi') {
Assert.ok(Number.isFinite(param.value), 'OK value for bmi');
Assert.ok(param.value <= 1 && param.value >= 0, 'OK value for bmi');
}
});
done();

@@ -70,10 +81,27 @@ });

it('should generate parameter mock for path /pet/{petId}/uploadImage', function(done) {
it('should generate parameter mock for path /pet/{petId} post - common parameter', (done) => {
swagmock.parameters({
path: '/pet/{petId}',
operation: 'post'
}, (err, mock) => {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
let params = mock.parameters;
Assert.ok(params, 'Generated parameters');
Assert.ok(params.path, 'Generated path parameter');
Assert.ok(params.path[0].name === 'petId', 'generated mock parameter for petId');
Assert.ok(Number.isInteger(params.path[0].value), 'OK value for petId');
done();
});
});
it('should generate parameter mock for path /pet/{petId}/uploadImage', (done) => {
swagmock.parameters({
path: '/pet/{petId}/uploadImage',
operation: 'post'
}, function(err, mock) {
}, (err, mock) => {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
var params = mock.parameters;
let params = mock.parameters;
Assert.ok(params, 'Generated parameters');

@@ -83,3 +111,3 @@ Assert.ok(params.path, 'Generated path parameter');

Assert.ok(Number.isInteger(params.path[0].value), 'OK value for petId');
Assert.ok(params.path[0].value > 1000 && params.path[0].value < 1010, 'OK value for petId');
Assert.ok(params.formData, 'Generated formData parameter');

@@ -92,10 +120,10 @@ Assert.ok(params.formData[0].name === 'additionalMetadata', 'generated mock parameter for additionalMetadata');

it('should generate parameter mock for path /store/inventory', function(done) {
it('should generate parameter mock for path /store/inventory', (done) => {
swagmock.parameters({
path: '/store/inventory',
operation: 'get'
}, function(err, mock) {
}, (err, mock) => {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
var params = mock.parameters;
let params = mock.parameters;
Assert.ok(params, 'Generated parameters');

@@ -106,14 +134,14 @@ done();

it('should generate parameter mock for path /store/order', function(done) {
it('should generate parameter mock for path /store/order', (done) => {
swagmock.parameters({
path: '/store/order',
operation: 'post'
}, function(err, mock) {
}, (err, mock) => {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
var params = mock.parameters;
let params = mock.parameters;
Assert.ok(params, 'Generated parameters');
Assert.ok(params.body, 'Generated body parameter');
Assert.ok(params.body[0].name === 'body', 'generated mock parameter for body');
var order = params.body[0].value;
let order = params.body[0].value;
Assert.ok(typeof order === 'object', 'OK value for body');

@@ -130,10 +158,10 @@ Assert.ok(Number.isInteger(order.id), 'order.id is integer');

it('should generate parameter mock for path /user/createWithArray', function(done) {
it('should generate parameter mock for path /user/createWithArray', (done) => {
swagmock.parameters({
path: '/user/createWithArray',
operation: 'post'
}, function(err, mock) {
}, (err, mock) => {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
var params = mock.parameters;
let params = mock.parameters;
Assert.ok(params, 'Generated parameters');

@@ -143,8 +171,10 @@

Assert.ok(params.body[0].name === 'body', 'generated mock parameter for body');
var users = params.body[0].value;
let users = params.body[0].value;
Assert.ok(users.length === 1, 'Created a parameter array of users');
var user = users[0];
let user = users[0];
Assert.ok(typeof user === 'object', 'OK value for user parameter');
Assert.ok(Number.isInteger(user.id), 'user.id is integer');
Assert.ok(Number.isInteger(user.userStatus), 'user.userStatus is integer');
Assert.ok(user.userStatus > 1000, 'user.userStatus is greater than 1000');
Assert.ok(user.userStatus % 100 === 0, 'user.userStatus is multipleOf 100');
Assert.ok(typeof user.username === 'string', 'user.username is string');

@@ -156,10 +186,10 @@

it('should generate parameter mock for path /user/logout', function(done) {
it('should generate parameter mock for path /user/logout', (done) => {
swagmock.parameters({
path: '/user/logout',
operation: 'get'
}, function(err, mock) {
}, (err, mock) => {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
var params = mock.parameters;
let params = mock.parameters;
Assert.ok(params, 'Generated parameters');

@@ -166,0 +196,0 @@ Assert.ok(params.query, 'Generated path parameter');

@@ -1,35 +0,34 @@

var Assert = require('assert');
var Swagmock = require('../lib');
var Path = require('path')
//isInteger pollyfil for pre es6
Number.isInteger = Number.isInteger || function(value) {
return typeof value === "number" &&
isFinite(value) &&
Math.floor(value) === value;
};
const Assert = require('assert');
const Swagmock = require('../lib');
const Path = require('path')
describe('Parameters Mock generator', function () {
var apiPath = Path.resolve(__dirname, 'fixture/petstore.json');
var swagmock = Swagmock(apiPath);
it('should generate parameter mock for path /store/order/{orderId} for all operations', function(done) {
swagmock.parameters({
describe('Parameters Mock generator', () => {
let apiPath = Path.resolve(__dirname, 'fixture/petstore.json');
let swagmock = Swagmock(apiPath);
it('should generate parameter mock for path /store/order/{orderId} for all operations', (done) => {
let mockgen = swagmock.parameters({
path: '/store/order/{orderId}'
}, function(err, mock) {
Assert.ok(!err, 'No error');
});
//Promise test case
mockgen.then(mock => {
Assert.ok(mock, 'Generated mock');
Assert.ok(mock.get, 'Generated mock for get operation');
Assert.ok(mock.delete, 'Generated mock for delete operation');
var params = mock.get.parameters;
let params = mock.get.parameters;
Assert.ok(params, 'Generated parameters');
Assert.ok(params.path, 'Generated path parameter');
Assert.ok(params.path[0].name === 'orderId', 'generated mock parameter for orderId');
Assert.ok(params.path[0].value > 0 && params.path[0].value < 10, 'OK value for orderId');
Assert.ok(params.path[0].value >= 1 && params.path[0].value <= 10, 'OK value for orderId');
done();
}).catch(err => {
Assert.ok(!err, 'No error');
done();
});
});
it('should generate parameter mock for all the path', function(done) {
swagmock.parameters({}, function(err, mock) {
var testMock;
Assert.ok(!err, 'No error');
it('should generate parameter mock for all the path', (done) => {
let mockgen = swagmock.parameters({});
//Promise test case
mockgen.then(mock => {
let testMock;
Assert.ok(mock, 'Generated mock');

@@ -57,4 +56,7 @@ Assert.ok(mock['/pet'], 'Generated mock for path /pet');

done();
}).catch(err => {
Assert.ok(!err, 'No error');
done();
});
});
});

@@ -1,22 +0,16 @@

var Assert = require('assert');
var Swagmock = require('../lib');
var Path = require('path')
//isInteger pollyfil for pre es6
Number.isInteger = Number.isInteger || function(value) {
return typeof value === "number" &&
isFinite(value) &&
Math.floor(value) === value;
};
const Assert = require('assert');
const Swagmock = require('../lib');
const Path = require('path')
describe('Request Mock generator', function () {
var apiPath = Path.resolve(__dirname, 'fixture/petstore.json');
var swagmock = Swagmock(apiPath);
it('should generate request mock for path /store/order/{orderId}', function(done) {
describe('Request Mock generator', () => {
let apiPath = Path.resolve(__dirname, 'fixture/petstore.json');
let swagmock = Swagmock(apiPath);
it('should generate request mock for path /store/order/{orderId}', (done) => {
swagmock.requests({
path: '/store/order/{orderId}',
operation: 'get'
}, function(err, mock) {
}, (err, mock) => {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
var request = mock.request;
let request = mock.request;
Assert.ok(request, 'Generated request');

@@ -29,10 +23,10 @@ Assert.ok(request.pathname, 'Generated pathname request');

it('should generate request mock for path /pet/findByStatus', function(done) {
it('should generate request mock for path /pet/findByStatus', (done) => {
swagmock.requests({
path: '/pet/findByStatus',
operation: 'get'
}, function(err, mock) {
}, (err, mock) => {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
var request = mock.request;
let request = mock.request;
Assert.ok(request, 'Generated request');

@@ -44,10 +38,10 @@ Assert.ok(request.query, 'Generated query request');

it('should generate request mock for path /pet/{petId}', function(done) {
it('should generate request mock for path /pet/{petId}', (done) => {
swagmock.requests({
path: '/pet/{petId}',
operation: 'get'
}, function(err, mock) {
}, (err, mock) => {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
var request = mock.request;
let request = mock.request;
Assert.ok(request, 'Generated parameters');

@@ -59,10 +53,10 @@ Assert.ok(request.pathname, 'Generated path parameter');

it('should generate request mock for path /pet/{petId}/uploadImage', function(done) {
it('should generate request mock for path /pet/{petId}/uploadImage', (done) => {
swagmock.requests({
path: '/pet/{petId}/uploadImage',
operation: 'post'
}, function(err, mock) {
}, (err, mock) => {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
var request = mock.request;
let request = mock.request;
Assert.ok(request, 'Generated request');

@@ -75,10 +69,10 @@ Assert.ok(request.pathname, 'Generated path request');

it('should generate request mock for path /store/inventory', function(done) {
it('should generate request mock for path /store/inventory', (done) => {
swagmock.requests({
path: '/store/inventory',
operation: 'get'
}, function(err, mock) {
}, (err, mock) => {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
var request = mock.request;
let request = mock.request;
Assert.ok(request, 'Generated request');

@@ -89,13 +83,13 @@ done();

it('should generate request mock for path /store/order', function(done) {
it('should generate request mock for path /store/order', (done) => {
swagmock.requests({
path: '/store/order',
operation: 'post'
}, function(err, mock) {
}, (err, mock) => {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
var request = mock.request;
let request = mock.request;
Assert.ok(request, 'Generated request');
Assert.ok(request.body, 'Generated body request');
var order = request.body;
let order = request.body;
Assert.ok(typeof order === 'object', 'OK value for body');

@@ -112,15 +106,15 @@ Assert.ok(Number.isInteger(order.id), 'order.id is integer');

it('should generate request mock for path /user/createWithArray', function(done) {
it('should generate request mock for path /user/createWithArray', (done) => {
swagmock.requests({
path: '/user/createWithArray',
operation: 'post'
}, function(err, mock) {
}, (err, mock) => {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
var request = mock.request;
let request = mock.request;
Assert.ok(request, 'Generated request');
Assert.ok(request.body, 'Generated body request');
var users = request.body;
let users = request.body;
Assert.ok(users.length === 1, 'Created a request array of users');
var user = users[0];
let user = users[0];
Assert.ok(typeof user === 'object', 'OK value for user request');

@@ -127,0 +121,0 @@ Assert.ok(Number.isInteger(user.id), 'user.id is integer');

@@ -1,16 +0,10 @@

var Assert = require('assert');
var Swagmock = require('../lib');
var Path = require('path')
//isInteger pollyfil for pre es6
Number.isInteger = Number.isInteger || function(value) {
return typeof value === "number" &&
isFinite(value) &&
Math.floor(value) === value;
};
const Assert = require('assert');
const Swagmock = require('../lib');
const Path = require('path')
describe('Response Mock generator', function () {
var apiPath = Path.resolve(__dirname, 'fixture/petstore.json');
var swagmock = Swagmock(apiPath);
describe('Response Mock generator', () => {
let apiPath = Path.resolve(__dirname, 'fixture/petstore.json');
let swagmock = Swagmock(apiPath);
it('should generate response mock for path /store/order/{orderId}', function(done) {
it('should generate response mock for path /store/order/{orderId}', (done) => {
swagmock.responses({

@@ -20,6 +14,6 @@ path: '/store/order/{orderId}',

response: '200'
}, function(err, mock) {
}, (err, mock) => {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
var resp = mock.responses;
let resp = mock.responses;
Assert.ok(resp, 'Generated response');

@@ -36,3 +30,3 @@ Assert.ok(Number.isInteger(resp.id), 'id is integer');

it('should generate response mock for path /pet/findByStatus', function(done) {
it('should generate response mock for path /pet/findByStatus', (done) => {
swagmock.responses({

@@ -42,10 +36,10 @@ path: '/pet/findByStatus',

response: '200'
}, function(err, mock) {
}, (err, mock) => {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
var resp = mock.responses;
let resp = mock.responses;
Assert.ok(resp, 'Generated response');
Assert.ok(Array.isArray(resp), 'response is Pet array');
var pet = resp[0];
let pet = resp[0];
Assert.ok(pet, 'Ok Pet response');

@@ -58,3 +52,3 @@ Assert.ok(Number.isInteger(pet.id), 'id is integer');

it('should generate response mock for path /pet/{petId}', function(done) {
it('should generate response mock for path /pet/{petId}', (done) => {
swagmock.responses({

@@ -64,7 +58,7 @@ path: '/pet/{petId}',

response: '200'
}, function(err, mock) {
}, (err, mock) => {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
var resp = mock.responses;
let resp = mock.responses;
Assert.ok(resp, 'Generated response');

@@ -76,3 +70,3 @@ //TODO add asserts for pending props

it('should generate response mock for path /pet/{petId}/uploadImage', function(done) {
it('should generate response mock for path /pet/{petId}/uploadImage', (done) => {
swagmock.responses({

@@ -82,7 +76,7 @@ path: '/pet/{petId}/uploadImage',

response: '200'
}, function(err, mock) {
}, (err, mock) => {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
var resp = mock.responses;
let resp = mock.responses;
Assert.ok(resp, 'Generated response');

@@ -94,3 +88,3 @@ //TODO add asserts for pending props

it('should generate response mock for path /store/inventory', function(done) {
it('should generate response mock for path /store/inventory', (done) => {
swagmock.responses({

@@ -100,7 +94,7 @@ path: '/store/inventory',

response: '200'
}, function(err, mock) {
}, (err, mock) => {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
var resp = mock.responses;
let resp = mock.responses;
Assert.ok(resp, 'Generated response');

@@ -112,3 +106,3 @@ //TODO add asserts for pending props

it('should generate response mock for path /store/order', function(done) {
it('should generate response mock for path /store/order', (done) => {
swagmock.responses({

@@ -118,7 +112,7 @@ path: '/store/order',

response: '200'
}, function(err, mock) {
}, (err, mock) => {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
var resp = mock.responses;
let resp = mock.responses;
Assert.ok(resp, 'Generated response');

@@ -130,3 +124,3 @@ //TODO add asserts for pending props

it('should generate response mock for path /user/login', function(done) {
it('should generate response mock for path /user/login', (done) => {
swagmock.responses({

@@ -136,7 +130,7 @@ path: '/user/login',

response: '200'
}, function(err, mock) {
}, (err, mock) => {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
var resp = mock.responses;
let resp = mock.responses;
Assert.ok(resp, 'Generated response');

@@ -148,3 +142,3 @@ //TODO add asserts for pending props

it('should generate response mock for path /pet', function(done) {
it('should generate response mock for path /pet', (done) => {
swagmock.responses({

@@ -154,7 +148,7 @@ path: '/pet',

response: '405'
}, function(err, mock) {
}, (err, mock) => {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
var resp = mock.responses;
let resp = mock.responses;
Assert.ok(!resp, 'No response');

@@ -161,0 +155,0 @@ //TODO add asserts for pending props

@@ -1,23 +0,19 @@

var Assert = require('assert');
var Swagmock = require('../lib');
var Path = require('path')
//isInteger pollyfil for pre es6
Number.isInteger = Number.isInteger || function(value) {
return typeof value === "number" &&
isFinite(value) &&
Math.floor(value) === value;
};
const Assert = require('assert');
const Swagmock = require('../lib');
const Path = require('path')
const Parser = require('swagger-parser');
describe('Responses Mock generator', function () {
var apiPath = Path.resolve(__dirname, 'fixture/petstore.json');
var swagmock = Swagmock(apiPath);
describe('Responses Mock generator', () => {
let apiPath = Path.resolve(__dirname, 'fixture/petstore.json');
let apiResolver = Parser.validate(apiPath);
let swagmock = Swagmock(apiResolver, { validated: true });
it('should generate response mock for path /store/order/{orderId}, get operation', function(done) {
swagmock.responses({
it('should generate response mock for path /store/order/{orderId}, get operation', (done) => {
let mockgen = swagmock.responses({
path: '/store/order/{orderId}',
operation: 'get'
}, function(err, mock) {
Assert.ok(!err, 'No error');
});
mockgen.then(mock => {
Assert.ok(mock, 'Generated mock');
var resp = mock.responses;
let resp = mock.responses;
Assert.ok(resp, 'Generated response');

@@ -27,3 +23,3 @@ Assert.ok(resp.hasOwnProperty('200'), 'Generated 200 response');

Assert.ok(resp.hasOwnProperty('404'), 'Generated 404 response');
var successResp = resp['200'];
let successResp = resp['200'];
Assert.ok(Number.isInteger(successResp.id), 'id is integer');

@@ -36,33 +32,35 @@ Assert.ok(Number.isInteger(successResp.petId), 'petId is integer');

done();
}).catch(err => {
Assert.ok(!err, 'No error');
done();
});
});
it('should generate response mock for path /pet/findByStatus, for all operations', function(done) {
swagmock.responses({
it('should generate response mock for path /pet/findByStatus, for all operations', (done) => {
let mockgen = swagmock.responses({
path: '/pet/findByStatus'
}, function(err, mock) {
Assert.ok(!err, 'No error');
});
mockgen.then(mock => {
Assert.ok(mock, 'Generated mock');
Assert.ok(mock.get, 'Generated mock for `get` operation');
var responses = mock.get.responses;
let responses = mock.get.responses;
Assert.ok(responses, 'Generated responses');
Assert.ok(responses.hasOwnProperty('200'), 'Generated 200 response');
Assert.ok(responses.hasOwnProperty('400'), 'Generated 400 response');
var resp = responses['200'];
let resp = responses['200'];
Assert.ok(Array.isArray(resp), 'response is Pet array');
var pet = resp[0];
let pet = resp[0];
Assert.ok(pet, 'Ok Pet response');
Assert.ok(Number.isInteger(pet.id), 'id is integer');
done();
}).catch(err => {
Assert.ok(!err, 'No error');
done();
});
});
it('should generate response mock for path /pet/{petId} for all operations', function(done) {
it('should generate response mock for path /pet/{petId} for all operations', (done) => {
swagmock.responses({
path: '/pet/{petId}'
}, function(err, mock) {
}, (err, mock) => {
Assert.ok(!err, 'No error');

@@ -74,3 +72,3 @@

Assert.ok(mock.delete, 'Generated mock for delete operation');
var responses = mock.delete.responses;
let responses = mock.delete.responses;
Assert.ok(responses, 'Generated response for delete operation');

@@ -83,5 +81,5 @@ Assert.ok(responses.hasOwnProperty('404'), 'Generated 404 response');

it('should generate response mock for all paths', function(done) {
swagmock.responses({}, function(err, mock) {
var testMock;
it('should generate response mock for all paths', (done) => {
swagmock.responses({}, (err, mock) => {
let testMock;
Assert.ok(!err, 'No error');

@@ -103,3 +101,2 @@ Assert.ok(mock, 'Generated mock');

Assert.ok(mock['/user/createWithList'], 'Generated mock for path /user/createWithList');
done();

@@ -106,0 +103,0 @@ });

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc