From 9566e7580eee941e1a9d039b170afc939a00379b Mon Sep 17 00:00:00 2001 From: Kirill Glazunov Date: Wed, 16 May 2018 15:14:48 +0300 Subject: [PATCH 1/3] Put data from query string of a get request into the `data` variable. --- package.json | 4 ++++ src/superagent-mock.js | 24 +++++++++++++++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index cf2e14a..cfc01a0 100644 --- a/package.json +++ b/package.json @@ -35,5 +35,9 @@ "lint": "eslint src tests", "build": "cross-env babel src --out-dir lib", "prepublishOnly": "yarn ci" + }, + "dependencies": { + "lodash": "^4.17.10", + "qs": "^6.5.2" } } diff --git a/src/superagent-mock.js b/src/superagent-mock.js index 31569d2..5560bfb 100755 --- a/src/superagent-mock.js +++ b/src/superagent-mock.js @@ -1,3 +1,6 @@ +import qs from 'qs'; +import {isEmpty, isNil} from 'lodash'; + /** * Installs the `mock` extension to superagent. * @param superagent Superagent instance @@ -77,6 +80,16 @@ module.exports = function (superagent, config, logger) { }, progress.delay || 0); }; + var getData = function(methodName, data, url) { + if (methodName.toUpperCase() !== 'GET' || !isNil(data)) { + return data; + } + + const parts = url.split('?'); + + return parts.length > 1 ? qs.parse(parts[1]) : undefined; + }; + /** * Override end function */ @@ -131,12 +144,14 @@ module.exports = function (superagent, config, logger) { }); } + const match = parser && new RegExp(parser.pattern, 'g').exec(path); + if (logEnabled) { currentLog.matcher = parser && parser.pattern; currentLog.mocked = !!parser; currentLog.url = path; currentLog.method = this.method; - currentLog.data = this._data; + currentLog.data = parser && getData(this.method, this._data, path); currentLog.headers = this.header; currentLog.timestamp = new Date().getTime(); } @@ -148,13 +163,12 @@ module.exports = function (superagent, config, logger) { return oldEnd.call(this, fn); } - const match = new RegExp(parser.pattern, 'g').exec(path); - const context = {}; try { const method = this.method.toLocaleLowerCase(); + const data = getData(this.method, this._data, path); context.method = method; - const fixtures = parser.fixtures(match, this._data, this.header, context); + const fixtures = parser.fixtures(match, data, this.header, context); if (context.cancel === true) { if (logEnabled) { currentLog.mocked = false; @@ -213,7 +227,7 @@ module.exports = function (superagent, config, logger) { if (logEnabled) { currentLog.error = error; } - + // Check if a callback for progress events was specified as part of the request var progressEventsUsed = isNodeServer ? !!this._formData : this.hasListeners && this.hasListeners('progress'); From cde2162c54c72f20c20a43d77b4e0053a3ad3f05 Mon Sep 17 00:00:00 2001 From: Kirill Glazunov Date: Wed, 16 May 2018 15:15:00 +0300 Subject: [PATCH 2/3] Tests. --- tests/support/config.js | 15 +++++++++++++++ tests/support/expectations.js | 16 ++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/tests/support/config.js b/tests/support/config.js index 88c2783..1767d91 100644 --- a/tests/support/config.js +++ b/tests/support/config.js @@ -48,6 +48,21 @@ module.exports = [ return {match: match, data: data}; } }, + { + pattern: 'https://decode.query.params', + fixtures: function (match, params) { + return params; + }, + get: function (match, data) { + return {match: match, data: data}; + }, + post: function (match, data) { + return {match: match, data: data}; + }, + put: function (match, data) { + return {match: match, data: data}; + } + }, { pattern: 'https://forget.query.params$', fixtures: function () { diff --git a/tests/support/expectations.js b/tests/support/expectations.js index e50a29b..3bbbd22 100755 --- a/tests/support/expectations.js +++ b/tests/support/expectations.js @@ -212,6 +212,22 @@ module.exports = function (request, config, isServer) { }); }); + it('should pass params as `data`', function(done) { + var params = { + foo: 'bar', + baz: '123', + oof: ['foo', '42'], + }; + + request.get('https://decode.query.params') + .query(params) + .end(function(err, result) { + expect(result.data).toEqual(params); + + done(); + }); + }); + it('attempt to match without query params', function (done) { var url = 'https://forget.query.params'; request.get(url) From 31e93ab232a259b39409980e2ae2d6aa6ee663c3 Mon Sep 17 00:00:00 2001 From: Kirill Glazunov Date: Thu, 17 May 2018 13:48:37 +0300 Subject: [PATCH 3/3] Remove lodash. --- package.json | 1 - src/superagent-mock.js | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/package.json b/package.json index cfc01a0..c52b577 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,6 @@ "prepublishOnly": "yarn ci" }, "dependencies": { - "lodash": "^4.17.10", "qs": "^6.5.2" } } diff --git a/src/superagent-mock.js b/src/superagent-mock.js index 5560bfb..b72af33 100755 --- a/src/superagent-mock.js +++ b/src/superagent-mock.js @@ -1,5 +1,4 @@ import qs from 'qs'; -import {isEmpty, isNil} from 'lodash'; /** * Installs the `mock` extension to superagent. @@ -81,7 +80,7 @@ module.exports = function (superagent, config, logger) { }; var getData = function(methodName, data, url) { - if (methodName.toUpperCase() !== 'GET' || !isNil(data)) { + if (methodName.toUpperCase() !== 'GET' || data !== undefined) { return data; }