Skip to content

Commit 745bd95

Browse files
elishererFlorent Dubost
authored and
Florent Dubost
committed
Add support for cancelling mocking after pattern match (#49)
* add support for cancelling mocking after pattern match through fixtures function * add superagent as a dev dependency since it is used in the testing (peer dependencies are no longer installed automatically) * add documentation for the new cancelling option
1 parent 59d648c commit 745bd95

File tree

5 files changed

+55
-4
lines changed

5 files changed

+55
-4
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ module.exports = [
5050
* @param match array Result of the resolution of the regular expression
5151
* @param params object sent by 'send' function
5252
* @param headers object set by 'set' function
53+
* @param context object the context of running the fixtures function
5354
*/
54-
fixtures: function (match, params, headers) {
55+
fixtures: function (match, params, headers, context) {
5556
/**
5657
* Returning error codes example:
5758
* request.get('https://domain.example/404').end(function(err, res){
@@ -94,6 +95,18 @@ module.exports = [
9495
}
9596
}
9697

98+
/**
99+
* Cancelling the mocking for a specific matched route example:
100+
* request.get('https://domain.example/server_test').end(function(err, res){
101+
* console.log(res.body); // (whatever the actual server would have returned)
102+
* })
103+
*/
104+
105+
if (match[1] === '/server_test') {
106+
context.cancel = true; // This will cancel the mock process and continue as usual (unmocked)
107+
return null;
108+
}
109+
97110
},
98111

99112
/**

lib/superagent-mock.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,13 @@ function mock (superagent, config, logger) {
107107
var match = new RegExp(parser.pattern, 'g').exec(path);
108108

109109
try {
110-
var fixtures = parser.fixtures(match, this._data, this.header);
110+
var context = {};
111+
var fixtures = parser.fixtures(match, this._data, this.header, context);
112+
if (context.cancel === true) {
113+
return oldEnd.call(this, fn); // mocking was cancelled from within fixtures
114+
}
111115
var method = this.method.toLocaleLowerCase();
112116
var parserMethod = parser[method] || parser.callback;
113-
114117
response = parserMethod(match, fixtures);
115118
} catch(err) {
116119
error = err;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"devDependencies": {
2121
"component-as-module": "0.3.0",
2222
"eslint": "0.12.0",
23-
"nodeunit": "0.10.2"
23+
"nodeunit": "0.10.2",
24+
"superagent": "^3.3.1"
2425
},
2526
"scripts": {
2627
"test": "nodeunit ./tests",

tests/support/config.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,5 +179,23 @@ module.exports = [
179179
callback: function (match, data) {
180180
return {match: match, data: data};
181181
}
182+
},
183+
{
184+
pattern: 'https://match.toomuch.example/([\\w-]+)',
185+
fixtures: function (match, data, headers, context) {
186+
if (match && match[1] === 'real-call') {
187+
context.cancel = true;
188+
}
189+
return match && match[1];
190+
},
191+
get: function (match, data) {
192+
return {match: match, data: data};
193+
},
194+
post: function (match, data) {
195+
return {match: match, data: data};
196+
},
197+
put: function (match, data) {
198+
return {match: match, data: data};
199+
}
182200
}
183201
];

tests/support/expectations.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,22 @@ module.exports = function (request, config, isServer) {
706706
}, Error, 'Should throw internal exception');
707707
test.equal(calls, 1);
708708
test.done();
709+
},
710+
'not calling real api if not cancelled': function (test) {
711+
request.put('https://match.toomuch.example/mock-call')
712+
.end(function (err, result) {
713+
test.ok(!err);
714+
test.notEqual(result, 'Real call done');
715+
test.done();
716+
});
717+
},
718+
'calling real api when cancelled': function (test) {
719+
request.put('https://match.toomuch.example/real-call')
720+
.end(function (err, result) {
721+
test.ok(!err);
722+
test.equal(result, 'Real call done');
723+
test.done();
724+
});
709725
}
710726
},
711727
'Logger': {

0 commit comments

Comments
 (0)