Skip to content

Commit 721aeae

Browse files
elishererFlorent Lepretre
authored and
Florent Lepretre
committed
Add support for asynchronously delaying mocked responses (#52)
* add support for asynchronously delaying mocked responses * fix lint issues presented in the delay feature * make response local to current request and not global for all mocks. * fix unit tests - update superagent lib files, tests are made on the latest v3 superagent which have changed on 3.4 * fix typo in README
1 parent c05a413 commit 721aeae

File tree

5 files changed

+64
-8
lines changed

5 files changed

+64
-8
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ module.exports = [
7575
if(params['superhero']) {
7676
return 'Your hero:' + params['superhero'];
7777
} else {
78-
return 'You didnt chose a hero';
78+
return 'You didnt choose a hero';
7979
}
8080
}
8181

@@ -107,6 +107,17 @@ module.exports = [
107107
return null;
108108
}
109109

110+
/**
111+
* Delaying the response with a specific number of milliseconds:
112+
* request.get('https://domain.example/delay_test').end(function(err, res){
113+
* console.log(res.body); // This log will be written after the delay time has passed
114+
* })
115+
*/
116+
117+
if (match[1] === '/delay_test') {
118+
context.delay = 3000; // This will delay the response by 3 seconds
119+
return 'zzZ';
120+
}
110121
},
111122

112123
/**

lib/superagent-mock.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ module.exports = mock;
1313
*/
1414
function mock (superagent, config, logger) {
1515
var Request = superagent.Request;
16-
var response = {};
1716
var currentLog = {};
1817
var logEnabled = !!logger;
1918

@@ -54,6 +53,7 @@ function mock (superagent, config, logger) {
5453
var error = null;
5554
var path;
5655
var isNodeServer = this.hasOwnProperty('cookies');
56+
var response = {};
5757

5858
if (isNodeServer) { // node server
5959
var originalPath = this.path;
@@ -106,8 +106,8 @@ function mock (superagent, config, logger) {
106106

107107
var match = new RegExp(parser.pattern, 'g').exec(path);
108108

109+
var context = {};
109110
try {
110-
var context = {};
111111
var fixtures = parser.fixtures(match, this._data, this.header, context);
112112
if (context.cancel === true) {
113113
return oldEnd.call(this, fn); // mocking was cancelled from within fixtures
@@ -142,7 +142,14 @@ function mock (superagent, config, logger) {
142142
}
143143
}
144144

145-
fn(error, response);
145+
if (typeof context.delay === 'number') {
146+
setTimeout(function () {
147+
fn(error, response);
148+
}, context.delay);
149+
}
150+
else {
151+
fn(error, response);
152+
}
146153
return this;
147154
};
148155

tests/support/component.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
"name": "superagent",
33
"scripts": [
44
"../../node_modules/superagent/lib/client.js",
5-
"../../node_modules/superagent/lib/is-object.js",
65
"../../node_modules/superagent/lib/is-function.js",
6+
"../../node_modules/superagent/lib/is-object.js",
77
"../../node_modules/superagent/lib/request-base.js",
88
"../../node_modules/superagent/lib/response-base.js",
9+
"../../node_modules/superagent/lib/should-retry.js",
910
"../../node_modules/superagent/lib/utils.js"
1011
],
1112
"main": "../../node_modules/superagent/lib/client.js",

tests/support/config.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ module.exports = [
181181
}
182182
},
183183
{
184-
pattern: 'https://match.toomuch.example/([\\w-]+)',
184+
pattern: 'https://context.cancel.example/([\\w-]+)',
185185
fixtures: function (match, data, headers, context) {
186186
if (match && match[1] === 'real-call') {
187187
context.cancel = true;
@@ -197,5 +197,21 @@ module.exports = [
197197
put: function (match, data) {
198198
return {match: match, data: data};
199199
}
200+
},
201+
{
202+
pattern: 'https://context.delay.example/([\\w-]+)',
203+
fixtures: function (match, data, headers, context) {
204+
context.delay = 3000;
205+
return match && match[1];
206+
},
207+
get: function (match, data) {
208+
return {match: match, data: data};
209+
},
210+
post: function (match, data) {
211+
return {match: match, data: data};
212+
},
213+
put: function (match, data) {
214+
return {match: match, data: data};
215+
}
200216
}
201217
];

tests/support/expectations.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ module.exports = function (request, config, isServer) {
1111
};
1212
var superagentPackage = require('superagent/package.json');
1313
var superagentUserAgentHeader = isServer ? {"User-Agent": 'node-superagent/' + superagentPackage.version} : {};
14+
var originalSetTimeout = setTimeout;
15+
var timePassed = 0;
1416

1517
return {
1618

@@ -29,6 +31,14 @@ module.exports = function (request, config, isServer) {
2931
return oldSet.call(this, field, value);
3032
};
3133

34+
// Stub setTimeout
35+
Object.defineProperty(global, 'setTimeout', {
36+
value: function(callbackFunc, timeout) {
37+
timePassed = timeout;
38+
callbackFunc();
39+
}
40+
});
41+
3242
// Init module
3343
superagentMock = require('./../../lib/superagent-mock')(request, config, logger);
3444

@@ -39,6 +49,9 @@ module.exports = function (request, config, isServer) {
3949
superagentMock.unset();
4050
headers = null;
4151
currentLog = null;
52+
// restore setTimeout
53+
Object.defineProperty(global, 'setTimeout', { value: originalSetTimeout });
54+
timePassed = 0;
4255

4356
go();
4457
},
@@ -708,20 +721,28 @@ module.exports = function (request, config, isServer) {
708721
test.done();
709722
},
710723
'not calling real api if not cancelled': function (test) {
711-
request.put('https://match.toomuch.example/mock-call')
724+
request.put('https://context.cancel.example/mock-call')
712725
.end(function (err, result) {
713726
test.ok(!err);
714727
test.notEqual(result, 'Real call done');
715728
test.done();
716729
});
717730
},
718731
'calling real api when cancelled': function (test) {
719-
request.put('https://match.toomuch.example/real-call')
732+
request.put('https://context.cancel.example/real-call')
720733
.end(function (err, result) {
721734
test.ok(!err);
722735
test.equal(result, 'Real call done');
723736
test.done();
724737
});
738+
},
739+
'calling callback function after specified delay': function (test) {
740+
request.put('https://context.delay.example/test')
741+
.end(function (err, result) {
742+
test.equal(result.data, 'test'); // just to see the arguments are passed as usual
743+
test.equal(timePassed, 3000); // setTimeout has been called
744+
test.done();
745+
});
725746
}
726747
},
727748
'Logger': {

0 commit comments

Comments
 (0)