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/routingManager.jsx b/src/routingManager.jsx
index c8f7879..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));
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
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.
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