diff --git a/lib/match_body.js b/lib/match_body.js index 71f92bbae..e909fb66a 100644 --- a/lib/match_body.js +++ b/lib/match_body.js @@ -43,7 +43,11 @@ function matchBody(spec, body) { } if (spec instanceof RegExp) { - return body.match(spec); + if (typeof body === "string") { + return body.match(spec); + } else { + return qs.stringify(body).match(spec); + } } if (!isMultipart && typeof spec === "string") { diff --git a/tests/test_body_match.js b/tests/test_body_match.js index 34761256c..96a1d6096 100644 --- a/tests/test_body_match.js +++ b/tests/test_body_match.js @@ -5,6 +5,27 @@ var test = require('tap').test; var mikealRequest = require('request'); var assert = require('assert'); +test('match body is regex trying to match string', function (t) { + + nock('http://encodingsareus.com') + .post('/', new RegExp("a.+")) + .reply(200); + + mikealRequest({ + url: 'http://encodingsareus.com/', + method: 'post', + json: { + auth: { + passwd: 'abc' + } + }, + }, function(err, res) { + if (err) throw err; + assert.equal(res.statusCode, 200); + t.end(); + }); + +}); test('match body with regex', function (t) { nock('http://encodingsareus.com')