diff --git a/lib/superagent-mock.js b/lib/superagent-mock.js index 14ab292..c293751 100755 --- a/lib/superagent-mock.js +++ b/lib/superagent-mock.js @@ -67,10 +67,9 @@ function mock (superagent, config, logger) { this.params = data; return this; - } else { - return oldSend.call(this, data); } + return oldSend.call(this, data); }; /** @@ -91,20 +90,20 @@ function mock (superagent, config, logger) { } var parser = testUrlForPatterns(this.url); - if (parser) { - if (isObject(field)) { + if (!parser) { + return oldSet.apply(this, arguments); + } + + if (isObject(field)) { for (var key in field) { this.set(key, field[key]); } - } else { + } else { this.headers = this.headers || {}; this.headers[field] = val; - } - - return this; - } else { - return oldSet.apply(this, arguments); } + + return this; }; /** @@ -142,16 +141,19 @@ function mock (superagent, config, logger) { flushLog(); } - if (parser) { - var match = new RegExp(parser.pattern, 'g').exec(path); + if (!parser) { + return oldEnd.call(this, fn); + } + + var match = new RegExp(parser.pattern, 'g').exec(path); - try { - var fixtures = parser.fixtures(match, this.params, this.headers); - var method = this.method.toLocaleLowerCase(); - var parserMethod = parsers[this.url][method] ? parsers[this.url][method] : parsers[this.url].callback; + try { + var fixtures = parser.fixtures(match, this.params, this.headers); + var method = this.method.toLocaleLowerCase(); + var parserMethod = parsers[this.url][method] ? parsers[this.url][method] : parsers[this.url].callback; - response = parserMethod(match, fixtures); - } catch(err) { + response = parserMethod(match, fixtures); + } catch(err) { error = err; response = new superagent.Response({ res: { @@ -170,15 +172,12 @@ function mock (superagent, config, logger) { getAllResponseHeaders: function () {return 'a header';}, getResponseHeader: function () {return err.responseHeader || 'a header';} } - }); - response.setStatusProperties(err.message); - } - - fn(error, response); - return this; - } else { - oldEnd.call(this, fn); + }); + response.setStatusProperties(err.message); } + + fn(error, response); + return this; }; Request.prototype.abort = function () {