From bd387365ff8e6df731cf5a35cc92560346826ec5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Ha=C3=9Fler?= Date: Sat, 21 Oct 2017 01:39:15 +0200 Subject: [PATCH] fix: stripping of new lines when using a matcher function for the body (#932) --- lib/match_body.js | 28 ++++++++++++++-------------- tests/test_common.js | 11 +++++++++++ 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/lib/match_body.js b/lib/match_body.js index 4c1a44f45..71f92bbae 100644 --- a/lib/match_body.js +++ b/lib/match_body.js @@ -19,20 +19,6 @@ function matchBody(spec, body) { var isMultipart = contentType && contentType.toString().match(/multipart/); - //strip line endings from both so that we get a match no matter what OS we are running on - //if Content-Type does not contains 'multipart' - if (!isMultipart && typeof body === "string") { - body = body.replace(/\r?\n|\r/g, ''); - } - - if (spec instanceof RegExp) { - return body.match(spec); - } - - if (!isMultipart && typeof spec === "string") { - spec = spec.replace(/\r?\n|\r/g, ''); - } - // try to transform body to json var json; if (typeof spec === 'object' || typeof spec === 'function') { @@ -50,6 +36,20 @@ function matchBody(spec, body) { return spec.call(this, body); } + //strip line endings from both so that we get a match no matter what OS we are running on + //if Content-Type does not contains 'multipart' + if (!isMultipart && typeof body === "string") { + body = body.replace(/\r?\n|\r/g, ''); + } + + if (spec instanceof RegExp) { + return body.match(spec); + } + + if (!isMultipart && typeof spec === "string") { + spec = spec.replace(/\r?\n|\r/g, ''); + } + return deepEqualExtended(spec, body); }; diff --git a/tests/test_common.js b/tests/test_common.js index 3007d4f31..c5b2662f4 100644 --- a/tests/test_common.js +++ b/tests/test_common.js @@ -12,6 +12,17 @@ tap.test('matchBody ignores new line characters from strings', function(t) { t.end() }); +tap.test('matchBody keeps new line characters if specs is a function', function(t) { + var body = "something //here is something more \n"; + var bodyAsSpecParameter = null + var spec = function(bodyToTest) { + bodyAsSpecParameter = bodyToTest + } + matchBody(spec, body); + t.equal(bodyAsSpecParameter, body); + t.end() +}); + tap.test('matchBody should not throw, when headers come node-fetch style as array', function(t) { var testThis = { headers: {