From 85c9febe17cb2fa589f2b899f4d754721b740664 Mon Sep 17 00:00:00 2001 From: Ben Date: Thu, 14 Sep 2017 07:19:51 -0700 Subject: [PATCH] Adapt to change in superagent lib (#68) --- package.json | 4 ++-- src/superagent-mock.js | 31 +++++++++++++++++++++---------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index ebe0f2b..b5e5976 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "url": "https://github.com/M6Web/superagent-mock" }, "peerDependencies": { - "superagent": "^3.5.2" + "superagent": "^3.6.0" }, "devDependencies": { "babel-cli": "^6.24.1", @@ -25,7 +25,7 @@ "eslint": "^3.19.0", "jest": "20.0.3", "rimraf": "^2.6.1", - "superagent": "^3.5.2" + "superagent": "^3.6.0" }, "scripts": { "clean": "rimraf lib es", diff --git a/src/superagent-mock.js b/src/superagent-mock.js index 13add7c..9fcf892 100755 --- a/src/superagent-mock.js +++ b/src/superagent-mock.js @@ -86,17 +86,28 @@ module.exports = function (superagent, config, logger) { const isNodeServer = this.hasOwnProperty('cookies'); let response = {}; - if (isNodeServer) { // node server - const originalPath = this.path; - this.path = this.url; - this._appendQueryString(this); // use superagent implementation of adding the query - path = this.path; // save the url together with the query - this.path = originalPath; // reverse the addition of query to path by _appendQueryString - } else { // client + if (this._finalizeQueryString) { + // superagent 3.6+ + const originalUrl = this.url; - this._appendQueryString(this); // use superagent implementation of adding the query - path = this.url; // save the url together with the query - this.url = originalUrl; // reverse the addition of query to url by _appendQueryString + isNodeServer ? this.request() : this._finalizeQueryString(this); + path = this.url; + this.url = originalUrl; + } else { + // superagent < 3.6 + + if (isNodeServer) { // node server + const originalPath = this.path; + this.path = this.url; + this._appendQueryString(this); // use superagent implementation of adding the query + path = this.path; // save the url together with the query + this.path = originalPath; // reverse the addition of query to path by _appendQueryString + } else { // client + const originalUrl = this.url; + this._appendQueryString(this); // use superagent implementation of adding the query + path = this.url; // save the url together with the query + this.url = originalUrl; // reverse the addition of query to url by _appendQueryString + } } // Attempt to match path against the patterns in fixtures