Skip to content

Commit

Permalink
Merge pull request #32 from pornel/master
Browse files Browse the repository at this point in the history
Handle more than 1 request
  • Loading branch information
A authored Jun 30, 2016
2 parents 4c49a5e + 5bf8b24 commit 72b96a6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
38 changes: 18 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,11 @@ function mock(superagent) {
if (superagent._patchedBySuperagentMocker) return mock;
superagent._patchedBySuperagentMocker = true;

// The room for matched route
var state = {
current: null,
reqest: {
body: {},
headers: {}
}
};

// patch api methods (http)
for (var method in methodsMapping) {
if (methodsMapping.hasOwnProperty(method)) {
var httpMethod = methodsMapping[method];
patch(superagent, method, httpMethod, state);
patch(superagent, method, httpMethod);
}
}

Expand All @@ -91,8 +82,9 @@ function mock(superagent) {
// Patch Request.end()
var oldEnd = originalMethods.end = superagent.Request.prototype.end;
reqProto.end = function(cb) {
var current = state.current;
if (current) {
var state = this._superagentMockerState;
if (state && state.current) {
var current = state.current;
setTimeout(function(request) {
try {
var response = current(request);
Expand All @@ -113,7 +105,8 @@ function mock(superagent) {
// Patch Request.set()
var oldSet = originalMethods.set = reqProto.set;
reqProto.set = function(key, val) {
if (!state.current) {
var state = this._superagentMockerState;
if (!state || !state.current) {
return oldSet.call(this, key, val);
}
// Recursively set keys if passed an object
Expand All @@ -133,7 +126,8 @@ function mock(superagent) {
// Patch Request.send()
var oldSend = originalMethods.send = reqProto.send;
reqProto.send = function(data) {
if (!state.current) {
var state = this._superagentMockerState;
if (!state || !state.current) {
return oldSend.call(this, data);
}
state.request.body = mergeObjects(state.current.body, data);
Expand Down Expand Up @@ -184,15 +178,19 @@ function defineRoute(method, url, handler) {
/**
* Patch superagent method
*/
function patch(superagent, prop, method, state) {
function patch(superagent, prop, method) {
var old = originalMethods[prop] = superagent[prop];
superagent[prop] = function (url, data, fn) {
state.current = match(method, url, data);
state.request = {
headers: {},
body: {}
var current = match(method, url, data);
var orig = old.call(this, url, data, fn);
orig._superagentMockerState = {
current: current,
request: {
headers: {},
body: {}
},
};
return old.call(this, url, data, fn);
return orig;
};
}

Expand Down
18 changes: 18 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ describe('superagent mock', function() {
});
});

it('should mock multiple requests', function(done) {
mock.get('/thread/:id', function(req) {
return { id: req.params.id };
});
var finished = 0;
var r1 = request.get('/thread/1');
var r2 = request.get('/thread/2');

r1.end(function(_, data) {
data.should.have.property('id', '1');
if (++finished == 2) done();
});
r2.end(function(_, data) {
data.should.have.property('id', '2');
if (++finished == 2) done();
});
});

it('should mock for post', function(done) {
mock.post('/topics/:id', function(req) {
return {
Expand Down

0 comments on commit 72b96a6

Please sign in to comment.