Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(match_body): urlencoded body matches not only string values #1110

Merged
Merged
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
41 changes: 34 additions & 7 deletions lib/match_body.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var deepEqual = require('deep-equal');
var qs = require('qs');
var _ = require('lodash')

module.exports =
function matchBody(spec, body) {
Expand All @@ -14,21 +15,23 @@ function matchBody(spec, body) {
body = body.toString();
}

var contentType = options.headers && (options.headers['Content-Type'] ||
options.headers['content-type']);
var contentType = (
options.headers &&
(options.headers['Content-Type'] || options.headers['content-type']) ||
''
).toString();

var isMultipart = contentType && contentType.toString().match(/multipart/);
var isMultipart = contentType.indexOf('multipart') >= 0;
var isUrlencoded = contentType.indexOf('application/x-www-form-urlencoded') >= 0;

// try to transform body to json
var json;
if (typeof spec === 'object' || typeof spec === 'function') {
try { json = JSON.parse(body);} catch(err) {}
if (json !== undefined) {
body = json;
} else {
if (contentType && contentType.toString().match(/application\/x-www-form-urlencoded/)) {
body = qs.parse(body, { allowDots: true });
}
} else if (isUrlencoded) {
body = qs.parse(body, { allowDots: true });
}
}

Expand All @@ -54,9 +57,33 @@ function matchBody(spec, body) {
spec = spec.replace(/\r?\n|\r/g, '');
}

if (isUrlencoded) {
spec = mapValuesDeep(spec, (val) => {
if (_.isRegExp(val)) {
return val
}
return val + ''
})
}

return deepEqualExtended(spec, body);
};


/**
* Based on lodash issue discussion
* https://github.com/lodash/lodash/issues/1244
*/
function mapValuesDeep(obj, cb) {
if (_.isArray(obj)) {
return obj.map((v) => mapValuesDeep(v, cb))
}
if (_.isPlainObject(obj)) {
return _.mapValues(obj, (v) => mapValuesDeep(v, cb))
}
return cb(obj)
}

function deepEqualExtended(spec, body) {
if (spec && spec.constructor === RegExp) {
return spec.test(body);
Expand Down
46 changes: 46 additions & 0 deletions tests/test_body_match.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,49 @@ test('array like urlencoded form posts are correctly parsed', function(t) {
t.end();
});
});

test('urlencoded form posts are matched with non-string values', function(t) {

nock('http://encodingsareus.com')
.post('/', {
boolean: true,
number: 1,
values: [false, -1, 'test']
})
.reply(200);

mikealRequest({
url: 'http://encodingsareus.com/',
method: 'post',
form: {
boolean: true,
number: 1,
values: [false, -1, 'test']
}
}, function(err, res) {
if (err) throw err;
assert.equal(res.statusCode, 200);
t.end();
});
});

test('urlencoded form posts are matched with regexp', function(t) {

nock('http://encodingsareus.com')
.post('/', {
regexp: /^xyz$/,
})
.reply(200);

mikealRequest({
url: 'http://encodingsareus.com/',
method: 'post',
form: {
regexp: 'xyz',
}
}, function(err, res) {
if (err) throw err;
assert.equal(res.statusCode, 200);
t.end();
});
});