Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down Expand Up @@ -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));
}
};
};
Expand Down
1 change: 1 addition & 0 deletions src/Store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion src/routingManager.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
9 changes: 7 additions & 2 deletions src/server.es6
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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) {
Expand All @@ -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();
});
Expand Down Expand Up @@ -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));
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/stubForm-dynamic.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,13 @@ class StubForm extends React.Component {
<Input style={{"marignBottom": "5px"}} type="text" placeholder="Enter the condition" value={this.state.conditions[i].eval}
onChange={this.handleConditionChange.bind(this, "eval", i)} onBlur={this.handleConditionChange.bind(this, "eval", i)}
help={<p style={{"fontSize": "11px"}}>use <code>req</code> to form a javascript expression <a href="javascript:;" onClick={()=>this.setState({helpModal: true})}>help on conditions</a></p>}/>
<Input type="select" label="Stub" value={this.state.conditions[i].stub} placeholder="select" onChange={this.handleConditionChange.bind(this, "stub", i)} onBlur={this.handleConditionChange.bind(this, "stub", i)}>
<Input type="select" label="Stub" value={this.state.conditions[i].stub} placeholder="select" onChange={this.handleConditionChange.bind(this, "stub", i)} onBlur={this.handleConditionChange.bind(this, "stub", i)}>
<option value="">select</option>
{options}
</Input>
<Input style={{"marignBottom": "5px"}} label="Status Code" type="text" placeholder="Status Code" value={this.state.conditions[i].statusCode}
onChange={this.handleConditionChange.bind(this, "statusCode", i)} onBlur={this.handleConditionChange.bind(this, "statusCode", i)}
/>
<a href="javascript:;" className="pull-right" style={{"fontSize": "12px"}} onClick={this.removeCondition.bind(this, i)}>Remove Condition</a>
</Col>
</Row>
Expand All @@ -95,6 +98,8 @@ class StubForm extends React.Component {
<option value="">select</option>
{options}
</Input>
<Input type="text" label="Default Status Code" placeholder="Status Code" value={this.state.statusCode}
onChange={this.handleChange.bind(this, "statusCode")} onBlur={this.handleChange.bind(this, "statusCode")}/>
<p className="small">Conditions are javascript expressions that can leverage the <code>req</code> object.</p>
<p className="small">Each condition is run sequentially in a sandbox environment and the stub corrensponding to the first matched condition is choosen as the response.</p>
<p className="small">If none of these conditions evaluate to a javascript <code>true</code>, then the Default Stub is responded.</p>
Expand All @@ -116,7 +121,7 @@ class StubForm extends React.Component {
<p>Use <code>req.path == '/path'</code> to match a url which ends with '/path'</p>
<p>List of all properties available in the <code>req</code> object :</p>
<pre>baseURL<br/>body<br/>cookies<br/>headers<br/>hostname<br/>ip<br/>ips<br/>method<br/>originalUrl<br/>params<br/>path<br/>protocol<br/>query<br/>route<br/>signedCookies<br/>stale<br/>subdomains<br/>xhr<br/></pre>
<p>These properties are being provided by expressjs module</p>
<p>These properties are being provided by express js module</p>
<p>See more about these properties <a target="_blank" href="http://expressjs.com/en/api.html#req.baseUrl">here</a></p>
</Modal.Body>
<Modal.Footer>
Expand Down