New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.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 to 0.0.2-alpha.1

tests/request_mockgen.js

2

docs/EXAMPLES.md

@@ -151,3 +151,3 @@ # Swagmock examples

"name": "status",
"value": "status=available&status=pending",
"value": [ "available", "pending" ],
"separator": "multi"

@@ -154,0 +154,0 @@ }]

@@ -25,6 +25,4 @@ 'use strict';

},
multi: function (val, name) {
return val.map(function (elem) {
return name + '=' + elem;
}).join('&');
multi: function (val) {
return val;
}

@@ -36,3 +34,3 @@ };

function queryMock(param) {
var mock;
var mock = {};
var value = Generators.mock(param);

@@ -45,11 +43,6 @@ var separator = collectionFormat.csv;

value = separator(value, param.name);
mock.separator = param.collectionFormat;
}
mock = {
name: param.name,
value: value
};
//Add a special separator field to identify the multi separator
if (param.collectionFormat === 'multi') {
mock.separator = 'multi';
}
mock.name = param.name;
mock.value = value;
return mock;

@@ -56,0 +49,0 @@ }

@@ -6,2 +6,3 @@ 'use strict';

var Util = require('./util');
var Querystring = require('querystring');

@@ -28,2 +29,8 @@ module.exports = function (apiPath) {

SwagMock.prototype.requests = function(options, callback) {
options = options || {};
options.mockRequest = true;
this.mock(options, callback);
};
SwagMock.prototype.mock = function(options, callback) {

@@ -45,3 +52,3 @@ options = options || {};

//Found the requested path
mockPath(pathObj, mock, options);
mockPath(options.path, pathObj, mock, options);
} else {

@@ -53,3 +60,3 @@ //Generate Mocks for all the paths

var pathMock = {};
mockPath(pathObj, pathMock, options);
mockPath(pathStr, pathObj, pathMock, options);
mock[pathStr] = pathMock;

@@ -65,3 +72,3 @@ }

*/
function mockPath(pathObj, mock, options) {
function mockPath(pathStr, pathObj, mock, options) {
var opsObj = pathObj[options.operation];

@@ -73,3 +80,6 @@ //Common parameters - A list of parameters that are applicable for

//Found the operation
mockOperation(opsObj, mock, options);
mockOperation({
path: pathStr,
operation: options.operation
}, opsObj, mock, options);
} else {

@@ -80,3 +90,6 @@ Object.keys(pathObj).forEach(function(operation) {

var opsMock = {};
mockOperation(pathObj[operation], opsMock, options);
mockOperation({
path: pathStr,
operation: operation
}, pathObj[operation], opsMock, options);
mock[operation] = opsMock;

@@ -90,3 +103,3 @@ }

*/
function mockOperation(opsObj, mock, options) {
function mockOperation(resolved, opsObj, mock, options) {
//Mock response

@@ -100,2 +113,6 @@ if (options.mockResponses) {

}
//Mock Requests
if (options.mockRequest) {
mock.request = mockRequest(resolved, mock.parameters || mockParameters(opsObj, options));
}
}

@@ -159,1 +176,54 @@ /**

}
/**
* 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;
if (parameters) {
//path
if (parameters.path && parameters.path.length > 0) {
parameters.path.forEach(function (pathParam) {
if (pathParam && pathParam.name) {
pathname = pathname.replace(new RegExp('{' + pathParam.name + '}', 'g'), pathParam.value);
}
});
mock.pathname = pathname;
}
//query
if (parameters.query && parameters.query.length > 0) {
queryObj = parameters.query.reduce(function (aggr, queryParam) {
aggr[queryParam.name] = queryParam.value;
return aggr;
}, queryObj);
mock.query = Querystring.stringify(queryObj);
}
// Body - The payload that's appended to the HTTP request. Since there can only be one payload, there can only be one body parameter. The name of the body parameter has no effect on the parameter itself and is used for documentation purposes only. Since Form parameters are also in the payload, body and form parameters cannot exist together for the same operation.
if (parameters.body && parameters.body.length > 0) {
mock.body = parameters.body[0].value;
}
//headers
if (parameters.headers && parameters.headers.length > 0) {
//Assuming only one Body for a request.
headerObj = parameters.headers.reduce(function (aggr, headersParam) {
aggr[headersParam.name] = headersParam.value;
return aggr;
}, headerObj);
mock.headers = headerObj;
}
//form-data
if (parameters.formData && parameters.formData.length > 0) {
formObj = parameters.formData.reduce(function (aggr, formParam) {
aggr[formParam.name] = formParam.value;
return aggr;
}, formObj);
mock.formData = Querystring.stringify(formObj);
}
}
return mock;
}
{
"name": "swagmock",
"version": "0.0.1",
"version": "0.0.2-alpha.1",
"description": "Mock data generator for swagger api",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

@@ -57,3 +57,3 @@ # swagmock

// "name": "status",
// "value": "status=available&status=pending",
// "value": [ 'available', 'pending' ],
// "separator": "multi"

@@ -108,2 +108,38 @@ // }]

## 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*) - (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.
### data
`request` Object will have following possible properties `query`, `header`, `path`, `formData` or `body`.
```javascript
mockgen.requests({
path: '/pet/findByStatus',
operation: 'get'
}, function (error, mock) {
console.log(mock);
//This would print:
// {
// "request": {
// "query": "status=available&status=pending"
// }
// }
});
```
## [Examples](docs/EXAMPLES.md)

@@ -127,2 +127,3 @@ var Assert = require('assert');

var params = mock.parameters;
console.log("===>", params);
Assert.ok(params, 'Generated parameters');

@@ -129,0 +130,0 @@

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