From 58189195de50be8d19603a16923395016c2be284 Mon Sep 17 00:00:00 2001 From: Ben Date: Wed, 6 Sep 2017 15:06:11 -0700 Subject: [PATCH 1/7] Adapt to change in superagent lib --- src/superagent-mock.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/superagent-mock.js b/src/superagent-mock.js index 13add7c..54210c7 100755 --- a/src/superagent-mock.js +++ b/src/superagent-mock.js @@ -89,14 +89,14 @@ module.exports = function (superagent, config, logger) { if (isNodeServer) { // node server const originalPath = this.path; this.path = this.url; - this._appendQueryString(this); // use superagent implementation of adding the query + this._finalizeQueryString(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 + this.path = originalPath; // reverse the addition of query to path by _finalizeQueryString } else { // client const originalUrl = this.url; - this._appendQueryString(this); // use superagent implementation of adding the query + this._finalizeQueryString(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 + this.url = originalUrl; // reverse the addition of query to url by _finalizeQueryString } // Attempt to match path against the patterns in fixtures From 84173912a94ca5ab98ad387cd1d321074dace77c Mon Sep 17 00:00:00 2001 From: Ben Date: Thu, 7 Sep 2017 09:17:09 -0700 Subject: [PATCH 2/7] Query string compilation. But this may be just a bug in the superagent refactor --- src/superagent-mock.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/superagent-mock.js b/src/superagent-mock.js index 54210c7..aaaf994 100755 --- a/src/superagent-mock.js +++ b/src/superagent-mock.js @@ -1,3 +1,5 @@ +var qs = require('qs'); + /** * Installs the `mock` extension to superagent. * @param superagent Superagent instance @@ -89,6 +91,13 @@ module.exports = function (superagent, config, logger) { if (isNodeServer) { // node server const originalPath = this.path; this.path = this.url; + + if (this.qs) { + var query = qs.stringify(this.qs, {indices: false, strictNullHandling: true}); + query += ((query.length && this.qsRaw.length) ? '&' : '') + this.qsRaw.join('&'); + this.path += query.length ? (~this.path.indexOf('?') ? '&' : '?') + query : ''; + } + this._finalizeQueryString(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 _finalizeQueryString From 5d37f4918307d2e52952d12479ed7ac055d58bd5 Mon Sep 17 00:00:00 2001 From: Ben Date: Thu, 7 Sep 2017 10:23:39 -0700 Subject: [PATCH 3/7] Backwards compat --- src/superagent-mock.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/superagent-mock.js b/src/superagent-mock.js index aaaf994..ae415c6 100755 --- a/src/superagent-mock.js +++ b/src/superagent-mock.js @@ -92,18 +92,29 @@ module.exports = function (superagent, config, logger) { const originalPath = this.path; this.path = this.url; - if (this.qs) { - var query = qs.stringify(this.qs, {indices: false, strictNullHandling: true}); - query += ((query.length && this.qsRaw.length) ? '&' : '') + this.qsRaw.join('&'); - this.path += query.length ? (~this.path.indexOf('?') ? '&' : '?') + query : ''; + if (this._finalizeQueryString) { + if (this.qs) { + var query = qs.stringify(this.qs, {indices: false, strictNullHandling: true}); + query += ((query.length && this.qsRaw.length) ? '&' : '') + this.qsRaw.join('&'); + this.path += query.length ? (~this.path.indexOf('?') ? '&' : '?') + query : ''; + } + + this._finalizeQueryString(this); // use superagent implementation of adding the query + } else { + this._appendQueryString(this); } - this._finalizeQueryString(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 _finalizeQueryString } else { // client const originalUrl = this.url; - this._finalizeQueryString(this); // use superagent implementation of adding the query + + if (this._finalizeQueryString) { + this._finalizeQueryString(this); // use superagent implementation of adding the query + } else { + this._appendQueryString(this); + } + path = this.url; // save the url together with the query this.url = originalUrl; // reverse the addition of query to url by _finalizeQueryString } From f982bfddf870461a7a8d6984421df60bf48a333a Mon Sep 17 00:00:00 2001 From: Ben Date: Mon, 11 Sep 2017 16:06:29 -0700 Subject: [PATCH 4/7] Better utilize superagent code --- package.json | 2 +- src/superagent-mock.js | 45 ++++++++++++++++++------------------------ 2 files changed, 20 insertions(+), 27 deletions(-) diff --git a/package.json b/package.json index ebe0f2b..005a2c7 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "eslint": "^3.19.0", "jest": "20.0.3", "rimraf": "^2.6.1", - "superagent": "^3.5.2" + "superagent": "3.5.2" }, "scripts": { "clean": "rimraf lib es", diff --git a/src/superagent-mock.js b/src/superagent-mock.js index ae415c6..604eb71 100755 --- a/src/superagent-mock.js +++ b/src/superagent-mock.js @@ -88,35 +88,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; - - if (this._finalizeQueryString) { - if (this.qs) { - var query = qs.stringify(this.qs, {indices: false, strictNullHandling: true}); - query += ((query.length && this.qsRaw.length) ? '&' : '') + this.qsRaw.join('&'); - this.path += query.length ? (~this.path.indexOf('?') ? '&' : '?') + query : ''; - } - - this._finalizeQueryString(this); // use superagent implementation of adding the query - } else { - this._appendQueryString(this); - } + if (this._finalizeQueryString) { + // superagent 3.6+ - path = this.path; // save the url together with the query - this.path = originalPath; // reverse the addition of query to path by _finalizeQueryString - } else { // client const originalUrl = this.url; - - if (this._finalizeQueryString) { - this._finalizeQueryString(this); // use superagent implementation of adding the query - } else { - this._appendQueryString(this); + 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 } - - path = this.url; // save the url together with the query - this.url = originalUrl; // reverse the addition of query to url by _finalizeQueryString } // Attempt to match path against the patterns in fixtures From ff173d3d4497c811af86928916da8fc0d1d349f3 Mon Sep 17 00:00:00 2001 From: Ben Date: Mon, 11 Sep 2017 16:06:47 -0700 Subject: [PATCH 5/7] Forgot to remove qs require --- src/superagent-mock.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/superagent-mock.js b/src/superagent-mock.js index 604eb71..9fcf892 100755 --- a/src/superagent-mock.js +++ b/src/superagent-mock.js @@ -1,5 +1,3 @@ -var qs = require('qs'); - /** * Installs the `mock` extension to superagent. * @param superagent Superagent instance From 6041921db4dac1c40186259d8baea00cf21a006c Mon Sep 17 00:00:00 2001 From: Ben Date: Mon, 11 Sep 2017 16:07:17 -0700 Subject: [PATCH 6/7] Revert changed package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 005a2c7..ebe0f2b 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "eslint": "^3.19.0", "jest": "20.0.3", "rimraf": "^2.6.1", - "superagent": "3.5.2" + "superagent": "^3.5.2" }, "scripts": { "clean": "rimraf lib es", From 503853d59744ec2e950274f132f42c6be6898563 Mon Sep 17 00:00:00 2001 From: Ben Date: Tue, 12 Sep 2017 10:36:30 -0700 Subject: [PATCH 7/7] Update deps --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 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",