Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,8 @@
"lint": "eslint src tests",
"build": "cross-env babel src --out-dir lib",
"prepublishOnly": "yarn ci"
},
"dependencies": {
"qs": "^6.5.2"
}
}
23 changes: 18 additions & 5 deletions src/superagent-mock.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import qs from 'qs';

/**
* Installs the `mock` extension to superagent.
* @param superagent Superagent instance
Expand Down Expand Up @@ -77,6 +79,16 @@ module.exports = function (superagent, config, logger) {
}, progress.delay || 0);
};

var getData = function(methodName, data, url) {
if (methodName.toUpperCase() !== 'GET' || data !== undefined) {
return data;
}

const parts = url.split('?');

return parts.length > 1 ? qs.parse(parts[1]) : undefined;
};

/**
* Override end function
*/
Expand Down Expand Up @@ -131,12 +143,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();
}
Expand All @@ -148,13 +162,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;
Expand Down Expand Up @@ -213,7 +226,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');

Expand Down
15 changes: 15 additions & 0 deletions tests/support/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
16 changes: 16 additions & 0 deletions tests/support/expectations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down