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.1-alpha.1 to 0.0.1

docs/EXAMPLES.md

154

lib/index.js
'use strict';
var Parser = require('swagger-parser');
var Generators = require('./generators');
var ParamTypes = require('./generators/paramtypes');
var Util = require('./util');
module.exports = function mockGen(apiPath, options, callback) {
module.exports = function (apiPath) {
return new SwagMock(apiPath);
};
function SwagMock(apiPath) {
this.swagger = Parser.validate(apiPath);
}
SwagMock.prototype.responses = function(options, callback) {
options = options || {};
options.mockResponses = true;
this.mock(options, callback);
};
Parser.validate(apiPath, function (error, api) {
if (error) {
callback(error);
return;
}
SwagMock.prototype.parameters = function(options, callback) {
options = options || {};
options.mockParams = true;
this.mock(options, callback);
};
SwagMock.prototype.mock = function(options, callback) {
options = options || {};
this.swagger.then(function(api) {
callback(null, mockSchema(api, options));
}).catch(function(error) {
callback(error);
});

@@ -24,14 +43,13 @@ };

//Found the requested path
var opsObj = pathObj[options.operation];
if (opsObj) {
//Found the operation
//Mock response
if (options.mockResponse) {
mock.response = mockResponse(opsObj, options);
mockPath(pathObj, mock, options);
} else {
//Generate Mocks for all the paths
Object.keys(paths).forEach(function(pathStr) {
var pathObj = paths[pathStr];
if (pathObj) {
var pathMock = {};
mockPath(pathObj, pathMock, options);
mock[pathStr] = pathMock;
}
//Mock Parameters
if (options.mockParams) {
mock.params = mockParameters(opsObj, options);
}
}
});
}

@@ -41,11 +59,57 @@ }

}
function mockResponse(opsObj, options) {
/**
*
*/
function mockPath(pathObj, mock, options) {
var 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;
if (opsObj) {
//Found the operation
mockOperation(opsObj, mock, options);
} else {
Object.keys(pathObj).forEach(function(operation) {
if (pathObj[operation] && Util.OPERATIONS.indexOf(operation) !== -1) {
//Valid operation.
var opsMock = {};
mockOperation(pathObj[operation], opsMock, options);
mock[operation] = opsMock;
}
});
}
}
/**
*
*/
function mockOperation(opsObj, mock, options) {
//Mock response
if (options.mockResponses) {
mock.responses = mockResponses(opsObj, options);
}
//Mock Parameters
if (options.mockParams) {
mock.parameters = mockParameters(opsObj, options);
}
}
/**
* Generate a mock responses
*
*/
function mockResponses(opsObj, options) {
var mockResp;
var response = opsObj.responses[options.response];
if (response) {
//Found the response
var schema = response.schema;
if (schema) {
mockResp = Generators.mock(schema);
var responses = opsObj.responses;
if (responses) {
var response = responses[options.response];
if (response) {
//Found the response
mockResp = mockResponse(response);
} else {
mockResp = mockResp || {};
Object.keys(responses).forEach(function(responseStr) {
var response = responses[responseStr];
if (response) {
mockResp[responseStr] = mockResponse(response);
}
});
}

@@ -55,14 +119,34 @@ }

}
function mockParameters(opsObj, options) {
var mockParam;
var parameter = opsObj.parameters[options.parameter];
if (parameter) {
//Found the parameter
var schema = parameter.schema;
if (schema) {
mockParam = Generators.mock(schema);
}
/**
*
*/
function mockResponse(response) {
var mockResp;
var schema = response.schema;
if (schema) {
mockResp = Generators.mock(schema);
}
return mockResp;
}
/**
* Generate a mock parameter list
*
*/
function mockParameters(opsObj) {
var mockParam = {};
var parameters = opsObj.parameters;
if (parameters && parameters.length > 0) {
//Iterate over each parameter
parameters.forEach(function(param) {
// `in` - The location of the parameter.
// Possible values are "query", "header", "path", "formData" or "body".
var paramType = param.in;
if (ParamTypes[paramType]) {
//Found the Mock generator for the param type (AKA `location`, AKA `in`).
mockParam[paramType] = mockParam[paramType] || [];
mockParam[paramType].push(ParamTypes[paramType].call(null, param));
}
});
}
return mockParam;
}
{
"name": "swagmock",
"version": "0.0.1-alpha.1",
"version": "0.0.1",
"description": "Mock data generator for swagger api",

@@ -8,3 +8,3 @@ "main": "lib/index.js",

"lint": "eslint lib",
"test": "mocha tests"
"test": "mocha tests/*.js"
},

@@ -11,0 +11,0 @@ "repository": {

# swagmock
Mock data generator for swagger api
~THIS MODULE IS UNDER ACTIVE DEVELOPMENT~
## Install
```
npm install swagmock
```
## Usage
```javascript
var apiPath = 'http://petstore.swagger.io/v2/swagger.json';
var Swagmock = require('swagmock');
var mockgen = Swagmock(apiPath);
mockgen.responses({
path: '/pet/findByStatus',
operation: 'get',
response: 200
}, function (error, mock) {
console.log(mock);
//This would print:
// {
// "responses": [{
// "id": 2530624032210944,
// "category": {
// "id": 8200505595527168,
// "name": "r($vA&"
// },
// "name": "doggie",
// "photoUrls": ["p0x1", "6O)3*kO"],
// "tags": [{
// "id": 4590764340281344,
// "name": "WCTA6f!"
// }, {
// "id": -4614156653166592,
// "name": "e"
// }],
// "status": "pending"
// }]
// }
});
mockgen.parameters({
path: '/pet/findByStatus',
operation: 'get'
}, function (error, mock) {
console.log(mock);
//This would print:
// {
// "parameters": {
// "query": [{
// "name": "status",
// "value": "status=available&status=pending",
// "separator": "multi"
// }]
// }
// }
});
```
Check [Examples](docs/EXAMPLES.md) for more details on mock generators.
## API
`Swagmock(apiPath)`
* `apiPath` - (*String*) - (required) - The url or local path of the Swagger api.
## responses
`mockgen.responses(options, callback)`
This generates the mock response objects based on the `options`
* `options` - (*Object*) - (required) - Options to control the mock generation.
* `callback` - (*Function*) - (required) - `function (error, mock)`.
### options
* `path` - (*String*) - (optional) - The path for which the response mock need to be generated. For example `/pet/findByStatus`, `/pet` etc. If a `path` is not specified, mock response will be generated for all the paths defined by the swagger api.
* `operation` - (*String*) - (optional) - The operation for which the response mock need to be generated. For example `get`, `post` etc. If `operation` is not specified, mock response will be generated for all the operations defined by the swagger api.
* `response` - (*String*) - (optional) - The response for which the response mock need to be generated. For example `200`, `400`, `default` etc. If `response` is not specified, mock response will be generated for all the responses defined by the swagger api.
## parameters
`mockgen.parameters(options, callback)`
This generates the mock parameters objects based on the `options`
* `options` - (*Object*) - (required) - Options to control the mock generation.
* `callback` - (*Function*) - (required) - `function (error, mock)`.
### options
* `path` - (*String*) - (optional) - The path for which the parameters mock need to be generated. For example `/pet/findByStatus`, `/pet` etc. If a `path` is not specified, mock parameters will be generated for all the paths defined by the swagger api.
* `operation` - (*String*) - (optional) - The operation for which the parameters mock need to be generated. For example `get`, `post` etc. If `operation` is not specified, mock parameters will be generated for all the operations defined by the swagger api.
## [Examples](docs/EXAMPLES.md)

@@ -11,8 +11,9 @@ var Assert = require('assert');

describe('Mock generator', function () {
describe('Response Mock generator', function () {
var apiPath = Path.resolve(__dirname, 'fixture/petstore.json');
var swagmock = Swagmock(apiPath);
it('should generate response mock for path /store/order/{orderId}', function(done) {
Swagmock(apiPath,{
swagmock.responses({
path: '/store/order/{orderId}',
mockResponse: true,
operation: 'get',

@@ -23,3 +24,3 @@ response: '200'

Assert.ok(mock, 'Generated mock');
var resp = mock.response;
var resp = mock.responses;
Assert.ok(resp, 'Generated response');

@@ -37,5 +38,4 @@ Assert.ok(Number.isInteger(resp.id), 'id is integer');

it('should generate response mock for path /pet/findByStatus', function(done) {
Swagmock(apiPath,{
swagmock.responses({
path: '/pet/findByStatus',
mockResponse: true,
operation: 'get',

@@ -47,3 +47,3 @@ response: '200'

Assert.ok(mock, 'Generated mock');
var resp = mock.response;
var resp = mock.responses;
Assert.ok(resp, 'Generated response');

@@ -60,5 +60,4 @@ Assert.ok(Array.isArray(resp), 'response is Pet array');

it('should generate response mock for path /pet/{petId}', function(done) {
Swagmock(apiPath,{
swagmock.responses({
path: '/pet/{petId}',
mockResponse: true,
operation: 'get',

@@ -70,3 +69,3 @@ response: '200'

Assert.ok(mock, 'Generated mock');
var resp = mock.response;
var resp = mock.responses;
Assert.ok(resp, 'Generated response');

@@ -79,5 +78,4 @@ //TODO add asserts for pending props

it('should generate response mock for path /pet/{petId}/uploadImage', function(done) {
Swagmock(apiPath,{
swagmock.responses({
path: '/pet/{petId}/uploadImage',
mockResponse: true,
operation: 'post',

@@ -89,3 +87,3 @@ response: '200'

Assert.ok(mock, 'Generated mock');
var resp = mock.response;
var resp = mock.responses;
Assert.ok(resp, 'Generated response');

@@ -98,5 +96,4 @@ //TODO add asserts for pending props

it('should generate response mock for path /store/inventory', function(done) {
Swagmock(apiPath,{
swagmock.responses({
path: '/store/inventory',
mockResponse: true,
operation: 'get',

@@ -108,3 +105,3 @@ response: '200'

Assert.ok(mock, 'Generated mock');
var resp = mock.response;
var resp = mock.responses;
Assert.ok(resp, 'Generated response');

@@ -117,5 +114,4 @@ //TODO add asserts for pending props

it('should generate response mock for path /store/order', function(done) {
Swagmock(apiPath,{
swagmock.responses({
path: '/store/order',
mockResponse: true,
operation: 'post',

@@ -127,3 +123,3 @@ response: '200'

Assert.ok(mock, 'Generated mock');
var resp = mock.response;
var resp = mock.responses;
Assert.ok(resp, 'Generated response');

@@ -136,5 +132,4 @@ //TODO add asserts for pending props

it('should generate response mock for path /user/login', function(done) {
Swagmock(apiPath,{
swagmock.responses({
path: '/user/login',
mockResponse: true,
operation: 'get',

@@ -146,3 +141,3 @@ response: '200'

Assert.ok(mock, 'Generated mock');
var resp = mock.response;
var resp = mock.responses;
Assert.ok(resp, 'Generated response');

@@ -155,5 +150,4 @@ //TODO add asserts for pending props

it('should generate response mock for path /pet', function(done) {
Swagmock(apiPath,{
swagmock.responses({
path: '/pet',
mockResponse: true,
operation: 'post',

@@ -165,3 +159,3 @@ response: '405'

Assert.ok(mock, 'Generated mock');
var resp = mock.response;
var resp = mock.responses;
Assert.ok(!resp, 'No response');

@@ -172,2 +166,2 @@ //TODO add asserts for pending props

});
})
});
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