Skip to content

Commit

Permalink
fix: stripping of new lines when using a matcher function for the body (
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsHassler authored and gr2m committed Oct 20, 2017
1 parent f13add9 commit bd38736
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
28 changes: 14 additions & 14 deletions lib/match_body.js
Expand Up @@ -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') {
Expand All @@ -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);
};

Expand Down
11 changes: 11 additions & 0 deletions tests/test_common.js
Expand Up @@ -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: {
Expand Down

0 comments on commit bd38736

Please sign in to comment.