From 36d86793def59ff3deecba89e13c3a17d6c2e70c Mon Sep 17 00:00:00 2001
From: amangupta27
Date: Mon, 28 May 2018 14:24:57 +0530
Subject: [PATCH 1/3] feat: Added support for status code in dynamic stub -
Added field for providing status code while creating dynamic stub both in
default and condition
---
package.json | 2 +-
server.js | 14 +++++++++-----
src/Store.js | 1 +
src/server.es6 | 9 +++++++--
src/stubForm-dynamic.jsx | 9 +++++++--
5 files changed, 25 insertions(+), 10 deletions(-)
diff --git a/package.json b/package.json
index d09fee7..d6ff022 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "mocknode",
- "version": "1.2.3",
+ "version": "1.2.4",
"description": "A configurable mock server with an intuitive configuration management interface and a http api",
"author": "Anunay Inuganti",
"license": "MIT",
diff --git a/server.js b/server.js
index 7e9bde0..41e1e6e 100644
--- a/server.js
+++ b/server.js
@@ -149,20 +149,22 @@ if (argv.location) console.log('mocknode installation directory: ', __dirname);e
return function (req, res) {
var returnedStub = _stub.defaultStub,
continueLoop = true,
+ statusCode = _stub.statusCode || 200,
count = 0;
if (_stub.conditions.length) {
async.whilst(function () {
return count < _stub.conditions.length && continueLoop;
}, function (callback) {
- var script = sandcastle.createScript('exports.main = function() {\n try {\n if (' + _stub.conditions[count].eval + ')\n exit(\'' + _stub.conditions[count].stub + '\')\n else\n exit(false)\n } catch(e) {\n exit(false)\n }\n }');
+ var script = sandcastle.createScript('exports.main = function() {\n try {\n if (' + _stub.conditions[count].eval + ')\n exit(\'' + count + '\')\n else\n exit(false)\n } catch(e) {\n exit(false)\n }\n }');
count++;
script.on('exit', function (err, output) {
if (output) {
- continueLoop = false;
- returnedStub = output;
+ continueLoop = false;
+ returnedStub = _stub.conditions[output].stub;
+ statusCode = _stub.conditions[output].statusCode || statusCode;
}
callback();
});
@@ -190,10 +192,12 @@ if (argv.location) console.log('mocknode installation directory: ', __dirname);e
})
});
}, function (err) {
- res.sendFile(path.join(__dirname, 'stubs', encodeRoutePath(_route), returnedStub));
+ res.status(statusCode);
+ res.sendFile(path.join(__dirname, 'stubs', encodeRoutePath(_route), returnedStub));
});
} else {
- res.sendFile(path.join(__dirname, 'stubs', encodeRoutePath(_route), returnedStub));
+ res.status(statusCode);
+ res.sendFile(path.join(__dirname, 'stubs', encodeRoutePath(_route), returnedStub));
}
};
};
diff --git a/src/Store.js b/src/Store.js
index 6d3b549..5942a51 100644
--- a/src/Store.js
+++ b/src/Store.js
@@ -78,6 +78,7 @@ let Store = assign({}, EventEmitter.prototype, {
name: state.name,
description: state.description,
defaultStub: state.defaultStub,
+ statusCode:state.statusCode,
conditions: state.conditions
})
}).then((json) => {
diff --git a/src/server.es6 b/src/server.es6
index 15a5146..249afe9 100644
--- a/src/server.es6
+++ b/src/server.es6
@@ -140,7 +140,9 @@ let dynamicStubRequestHandler = (_route, _stub) => {
return (req, res) => {
let returnedStub = _stub.defaultStub,
continueLoop = true,
+ statusCode=_stub.statusCode||200,
count = 0;
+
if (_stub.conditions.length) {
async.whilst(
@@ -149,7 +151,7 @@ let dynamicStubRequestHandler = (_route, _stub) => {
let script = sandcastle.createScript(`exports.main = function() {
try {
if (${_stub.conditions[count].eval})
- exit('${_stub.conditions[count].stub}')
+ exit('${count}')
else
exit(false)
} catch(e) {
@@ -162,7 +164,8 @@ let dynamicStubRequestHandler = (_route, _stub) => {
script.on('exit', function(err, output) {
if (output) {
continueLoop = false
- returnedStub = output
+ returnedStub = _stub.conditions[output].stub
+ statusCode= _stub.conditions[output].statusCode||statusCode
}
callback();
});
@@ -191,11 +194,13 @@ let dynamicStubRequestHandler = (_route, _stub) => {
});
},
(err) => {
+ res.status(statusCode);
res.sendFile(path.join(__dirname, 'stubs', encodeRoutePath(_route), returnedStub));
}
);
}
else {
+ res.status(statusCode);
res.sendFile(path.join(__dirname, 'stubs', encodeRoutePath(_route), returnedStub));
}
}
diff --git a/src/stubForm-dynamic.jsx b/src/stubForm-dynamic.jsx
index 1f3a705..1f7cf98 100644
--- a/src/stubForm-dynamic.jsx
+++ b/src/stubForm-dynamic.jsx
@@ -74,10 +74,13 @@ class StubForm extends React.Component {
use req to form a javascript expression this.setState({helpModal: true})}>help on conditions
}/>
-
+
{options}
+
Remove Condition
@@ -95,6 +98,8 @@ class StubForm extends React.Component {
{options}
+
Conditions are javascript expressions that can leverage the req object.
Each condition is run sequentially in a sandbox environment and the stub corrensponding to the first matched condition is choosen as the response.
If none of these conditions evaluate to a javascript true, then the Default Stub is responded.
@@ -116,7 +121,7 @@ class StubForm extends React.Component {
Use req.path == '/path' to match a url which ends with '/path'
List of all properties available in the req object :
baseURL
body
cookies
headers
hostname
ip
ips
method
originalUrl
params
path
protocol
query
route
signedCookies
stale
subdomains
xhr
- These properties are being provided by expressjs module
+ These properties are being provided by express js module
See more about these properties here
From 03cf578a7b365d9f7f85eec365d7535c0c099a72 Mon Sep 17 00:00:00 2001
From: amangupta27
Date: Mon, 28 May 2018 14:26:36 +0530
Subject: [PATCH 2/3] fix: Allowed % character in URL - Fixed the regex that
was preventing % from URL
---
src/routingManager.jsx | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/routingManager.jsx b/src/routingManager.jsx
index c8f7879..838a88d 100644
--- a/src/routingManager.jsx
+++ b/src/routingManager.jsx
@@ -23,7 +23,7 @@ class RoutingManager extends React.Component {
}
validate(state){
let validity = state.route && state.route.charAt(0) == '/' && state.route.charAt(state.route.length - 1) == '/'
- && state.route.length > 2 && !/[^a-zA-Z0-9\_\-\/]/.test( state.route ) &&
+ && state.route.length > 2 && !/[^a-zA-Z0-9\_\-\/\%]/.test( state.route ) &&
((state.handle == "proxy" && state.proxy && isWebUri(state.proxy) && state.proxy.charAt(state.proxy.length - 1) != '/')
|| (state.handle == "stub" && state.stub)
|| (state.handle == "dynamicStub" && state.dynamicStub));
From 738518d633a0520c50b5b08f63718084ab886d7b Mon Sep 17 00:00:00 2001
From: "aman.gupta"
Date: Thu, 3 Oct 2019 15:27:52 +0530
Subject: [PATCH 3/3] m
---
package.json | 2 +-
src/routingManager.jsx | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/package.json b/package.json
index d6ff022..d09fee7 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "mocknode",
- "version": "1.2.4",
+ "version": "1.2.3",
"description": "A configurable mock server with an intuitive configuration management interface and a http api",
"author": "Anunay Inuganti",
"license": "MIT",
diff --git a/src/routingManager.jsx b/src/routingManager.jsx
index 838a88d..c782011 100644
--- a/src/routingManager.jsx
+++ b/src/routingManager.jsx
@@ -23,7 +23,7 @@ class RoutingManager extends React.Component {
}
validate(state){
let validity = state.route && state.route.charAt(0) == '/' && state.route.charAt(state.route.length - 1) == '/'
- && state.route.length > 2 && !/[^a-zA-Z0-9\_\-\/\%]/.test( state.route ) &&
+ && state.route.length > 2 && !/[^a-z0-9\_\-\/\%]/.test( state.route ) &&
((state.handle == "proxy" && state.proxy && isWebUri(state.proxy) && state.proxy.charAt(state.proxy.length - 1) != '/')
|| (state.handle == "stub" && state.stub)
|| (state.handle == "dynamicStub" && state.dynamicStub));