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

apimocker

Package Overview
Dependencies
Maintainers
1
Versions
54
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

apimocker - npm Package Compare versions

Comparing version 0.3.2 to 0.3.3

samplemocks/sorry.json

11

config.json

@@ -39,4 +39,15 @@ {

"switch": ["id", "color"]
},
"login": {
"verbs": ["post"],
"switch": ["userId", "password"],
"responses": {
"post": {"httpStatus": 401, "mockFile": "sorry.json"}
},
"switchResponses": {
"userIduser1passwordgood": {"httpStatus": 200, "mockFile": "king.json"},
"userIdadminpasswordgood": {"httpStatus": 200}
}
}
}
}

34

lib/apimocker.js

@@ -169,7 +169,8 @@ var express = require('express'),

originalOptions = _.clone(options);
apiMocker.setMockFile(options, req);
apiMocker.setSwitchOptions(options, req);
mockPath = path.join(apiMocker.options.mockDirectory, options.mockFile);
if (!fs.existsSync(mockPath)) {
apiMocker.log("No file found: " + options.mockFile + " attempting base file: " + originalOptions.mockFile);
options = originalOptions;
// options = originalOptions;
options.mockFile = originalOptions.mockFile;
}

@@ -196,4 +197,4 @@ }

// only used when there is a switch configured
apiMocker.setMockFile = function(options, req) {
var switchValue = "",
apiMocker.setSwitchOptions = function(options, req) {
var switchFilePrefix = "", switchParamValue,
mockFileParts, mockFilePrefix = "", mockFileBaseName;

@@ -206,14 +207,25 @@

switches.forEach(function(elem){
if (req.body[elem]) { // json post request
switchValue = switchValue + elem + encodeURIComponent(req.body[elem]);
} else if (req.param(elem)) { // query param in get request
switchValue = switchValue + elem + encodeURIComponent(req.param(elem));
switches.forEach(function(s){
switchParamValue = null;
if (req.body[s]) { // json post request
switchParamValue = encodeURIComponent( req.body[s] );
} else if (req.param(s)) { // query param in get request
switchParamValue = encodeURIComponent( req.param(s));
}
if (switchParamValue) {
switchFilePrefix = switchFilePrefix + s + switchParamValue;
}
});
if(!switchValue){
if(!switchFilePrefix){
return;
}
if (options.switchResponses && options.switchResponses[switchFilePrefix]) {
_.extend(options, options.switchResponses[switchFilePrefix]);
if (options.switchResponses[switchFilePrefix].mockFile) {
return;
}
}
mockFileParts = options.mockFile.split("/");

@@ -224,3 +236,3 @@ mockFileBaseName = mockFileParts.pop();

}
options.mockFile = mockFilePrefix + switchValue + "." + mockFileBaseName;
options.mockFile = mockFilePrefix + switchFilePrefix + "." + mockFileBaseName;
};

@@ -227,0 +239,0 @@

{
"name": "apimocker",
"description": "Simple HTTP server that returns mock service API responses to your front end.",
"version": "0.3.2",
"version": "0.3.3",
"engines": {

@@ -6,0 +6,0 @@ "node": ">=0.10.0"

@@ -85,3 +85,3 @@ # apimocker

},
"nested/ace": {
"nested/ace": {
"mockFile": "ace.json",

@@ -91,3 +91,3 @@ "verbs": ["post", "get"],

},
"nested/ace2": {
"nested/ace2": {
"mockFile": "ace.json",

@@ -101,2 +101,13 @@ "verbs": ["post", "get"],

"switch": "id"
},
"login": {
"verbs": ["post"],
"switch": ["userId", "password"],
"responses": {
"post": {"httpStatus": 401, "mockFile": "sorry.json"}
},
"switchResponses": {
"userIduser1passwordgood": {"httpStatus": 200, "mockFile": "king.json"},
"userIdadminpasswordgood": {"httpStatus": 200}
}
}

@@ -124,2 +135,3 @@ }

#### Multiple switches
You can now also define an array of values to switch on. Given the configuration in "ace2", a request to "nested/ace2" containing:

@@ -135,2 +147,13 @@ ```js

#### Switch HTTP Status
To specify a different HTTP status, depending on a request parameter, you'll need to set up the "switchResponses" as shown above for the "login" service. You can also set a specific mock file using the "switchRespones" configuration. The switchReponses config section is an object, where the key is a composite of the switch keys specified in the "switch" setting for the service, and the values for each key, passed in as request parameters. For instance, a post request to "/login" containing:
```js
{
"userId": "user1",
"password": "good"
}
```
will return data from the mock file called "king.json", with HTTP status 200.
Any other password will return "sorry.json" with HTTP status 401.
## Runtime configuration

@@ -160,2 +183,4 @@ After starting apimocker, mocks can be configured using a simple http api.

## Versions
### 0.3.3
Added support for switching response HTTP status based on a request parameter. (see issue #12)
### 0.3.2

@@ -162,0 +187,0 @@ Added support for multiple switch parameters on a single URL. Thanks @skjegg and @snyoz !

@@ -31,4 +31,15 @@ {

"verbs": ["all"]
},
"login": {
"mockFile": "king.json",
"verbs": ["post"],
"switch": ["userId", "password"],
"responses": {
"post": {"httpStatus": 401}
},
"switchResponses": {
"userIduser1passwordgood": {"httpStatus": 200}
}
}
}
}

@@ -206,2 +206,13 @@ /* global describe, it, before */

it('returns correct httpStatus when switches match', function(done) {
var postData = '{"userId": "user1", "password": "good"}',
postOptions = httpPostOptions("/login", postData);
verifyResponseStatus(postOptions, postData, 200, done);
});
it('returns correct httpStatus when switch does not match', function(done) {
var postOptions = httpPostOptions("/login", "{}");
verifyResponseStatus(postOptions, "{}", 401, done);
});
it("returns httpStatus of 200 if not set", function(done) {

@@ -208,0 +219,0 @@ verifyResponseStatus(httpReqOptions("/first"), null, 200, done);

@@ -164,3 +164,3 @@ /* global describe, it, beforeEach, afterEach, after */

describe("setMockFile: ", function() {
describe("setSwitchOptions: ", function() {
var mocker, svcOptions, reqStub;

@@ -178,3 +178,3 @@

it("does not set mock file path if switch is not found in request", function() {
mocker.setMockFile(svcOptions, reqStub);
mocker.setSwitchOptions(svcOptions, reqStub);
expect(svcOptions.mockFile).to.equal("base");

@@ -185,3 +185,3 @@ });

reqStub.param = function() {return "123";};
mocker.setMockFile(svcOptions, reqStub);
mocker.setSwitchOptions(svcOptions, reqStub);
expect(svcOptions.mockFile).to.equal("productId123.base");

@@ -192,3 +192,3 @@ });

reqStub.body.productId = "678";
mocker.setMockFile(svcOptions, reqStub);
mocker.setSwitchOptions(svcOptions, reqStub);
expect(svcOptions.mockFile).to.equal("productId678.base");

@@ -200,3 +200,3 @@ });

svcOptions.mockFile = "path/to/base";
mocker.setMockFile(svcOptions, reqStub);
mocker.setSwitchOptions(svcOptions, reqStub);
expect(svcOptions.mockFile).to.equal("path/to/productId678.base");

@@ -207,3 +207,3 @@ });

reqStub.body.productId="abc/123";
mocker.setMockFile(svcOptions, reqStub);
mocker.setSwitchOptions(svcOptions, reqStub);
expect(svcOptions.mockFile).to.equal("productIdabc%2F123.base");

@@ -216,5 +216,48 @@ });

reqStub.body.color = "red";
mocker.setMockFile(svcOptions, reqStub);
mocker.setSwitchOptions(svcOptions, reqStub);
expect(svcOptions.mockFile).to.equal("productId345colorred.base");
});
it("sets correct http status based on matching switch value", function() {
svcOptions.switch = "password";
svcOptions.switchResponses = {
passwordgood: {httpStatus: 200}
};
reqStub.body.password = "good";
mocker.setSwitchOptions(svcOptions, reqStub);
expect(svcOptions.httpStatus).to.equal(200);
});
it("sets correct mock file path when switch matches and switchResponse contains a mockFile", function() {
reqStub.body.productId = "678";
svcOptions.switchResponses = {
"productId678": {mockFile: "specialFileName"}
};
mocker.setSwitchOptions(svcOptions, reqStub);
expect(svcOptions.mockFile).to.equal("specialFileName");
});
it("sets correct http status when switch value does not match", function() {
svcOptions.switch = "password";
svcOptions.httpStatus = 401;
svcOptions.switchResponses = {
passwordgood: {httpStatus: 200}
};
reqStub.body.password = "bad";
mocker.setSwitchOptions(svcOptions, reqStub);
expect(svcOptions.httpStatus).to.equal(401);
});
it("sets correct http status when two switches match", function() {
svcOptions.switch = ["userId", "password"];
svcOptions.httpStatus = 401;
svcOptions.switchResponses = {
userId1234passwordgood: {httpStatus: 200}
};
reqStub.body.password = "good";
reqStub.body.userId = "1234";
mocker.setSwitchOptions(svcOptions, reqStub);
expect(svcOptions.httpStatus).to.equal(200);
});
});

@@ -221,0 +264,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