swagmock
Mock data generator for swagger api
Install
npm install swagmock
Usage
let Swagmock = require('swagmock');
let Mockgen = Swagmock(api, options);
Promise response:
let responseMock = Mockgen.responses({});
responseMock.then(mock => {
}).catch(error => {
Assert.ifError(error);
});
Callback style:
Mockgen.responses({ path: '/somepath'}, (error, mock) => {
Assert.ifError(error);
});
Check the API for more details.
Example
Initialize the mock generator
const apiPath = 'http://petstore.swagger.io/v2/swagger.json';
let Assert = require('assert');
let Swagmock = require('swagmock');
let Mockgen = Swagmock(apiPath);
Response mock generation:
mockgen.responses({
path: '/pet/findByStatus',
operation: 'get',
response: 200
}).then(mock => {
console.log(mock);
}).catch(error => {
Assert.ifError(error);
});
Parameters mock generation:
mockgen.parameters({
path: '/pet/findByStatus',
operation: 'get'
}).then(mock => {
console.log(mock);
}).catch(error => {
Assert.ifError(error);
})
Check Examples for more details on mock generators.
API
Swagmock(api, [options])
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) - (optional) - function (error, mock)
. If a callback is not provided a Promise
will be returned.
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) - (optional) - function (error, mock)
. If a callback is not provided a Promise
will be returned.
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.
requests
mockgen.requests(options, [callback])
This generates the mock request object based on the options
. requests
API resolves the parameters
mock data to generate the request
mock object useful for unit tests.
-
options
- (Object) - (required) - Options to control the mock generation.
-
callback
- (Function) - (optional) - function (error, mock)
. If a callback is not provided a Promise
will be returned.
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.
data
request
Object will have following possible properties query
, header
, pathname
, path
, formData
or body
based on the parameters
defined for the path and operation.
Mock request Path templates are resolved using path parameters.
mockgen.requests({
path: '/pet/findByStatus',
operation: 'get'
}, function (error, mock) {
assert.ifError(error);
console.log(mock);
});
Examples
API
Usage
Unit test request mocks
github api express app
slack api hapi app
Mock response data providers
spotify api hapi app
glugbot api express app