Skip to content

Commit

Permalink
fix: filtering by regex (#1089)
Browse files Browse the repository at this point in the history
  • Loading branch information
hisabimbola authored and gr2m committed Nov 26, 2018
1 parent 6cd119b commit 88fbdc2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
15 changes: 7 additions & 8 deletions lib/match_body.js
Expand Up @@ -10,12 +10,19 @@ function matchBody(spec, body) {
if (typeof spec === 'undefined') {
return true;
}

var options = this || {};

if (Buffer.isBuffer(body)) {
body = body.toString();
}

if (spec instanceof RegExp) {
if (typeof body === "string") {
return body.match(spec);
}
}

if (Buffer.isBuffer(spec)) {
if (common.isBinaryBuffer(spec)) {
spec = spec.toString('hex');
Expand Down Expand Up @@ -54,14 +61,6 @@ function matchBody(spec, body) {
body = body.replace(/\r?\n|\r/g, '');
}

if (spec instanceof RegExp) {
if (typeof body === "string") {
return body.match(spec);
} else {
return qs.stringify(body).match(spec);
}
}

if (!isMultipart && typeof spec === "string") {
spec = spec.replace(/\r?\n|\r/g, '');
}
Expand Down
23 changes: 23 additions & 0 deletions tests/test_body_match.js
Expand Up @@ -26,6 +26,7 @@ test('match body is regex trying to match string', function (t) {
});

});

test('match body with regex', function (t) {

nock('http://encodingsareus.com')
Expand All @@ -48,6 +49,28 @@ test('match body with regex', function (t) {

});

test('match body (with space character) with regex', function (t) {

nock('http://encodingsareus.com')
.post('/', /a bc/)
.reply(200);

mikealRequest({
url: 'http://encodingsareus.com/',
method: 'post',
json: {
auth: {
passwd: 'a bc'
}
},
}, function(err, res) {
if (err) throw err;
assert.equal(res.statusCode, 200);
t.end();
});

});

test('match body with regex inside array', function (t) {

nock('http://encodingsareus.com')
Expand Down

0 comments on commit 88fbdc2

Please sign in to comment.