Skip to content

Commit

Permalink
doesn’t match if body cannot be parsed as JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
leggsimon committed Sep 28, 2019
1 parent cab2390 commit ba802d0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/lib/generate-matcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,13 @@ const getBodyMatcher = ({ body: expectedBody }) => {
return true;
}

return body && isEqual(JSON.parse(body), expectedBody);
let sentBody;

try {
sentBody = JSON.parse(body);
} catch (_) {}

return sentBody && isEqual(sentBody, expectedBody);
};
};

Expand Down
11 changes: 11 additions & 0 deletions test/specs/routing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,17 @@ module.exports = fetchMock => {
expect(fm.calls(true).length).to.equal(0);
});

it('should not match if body sent isn’t JSON', async () => {
fm.mock('http://it.at.there/', 200, { body: { foo: 'bar' } }).catch();

await fm.fetchHandler('http://it.at.there/', {
method: 'POST',
body: new ArrayBuffer(8),
headers: { 'Content-Type': 'application/json' }
});
expect(fm.calls(true).length).to.equal(0);
});

it('should ignore the order of the keys in the body', async () => {
fm.mock('http://it.at.there/', 200, {
body: {
Expand Down

0 comments on commit ba802d0

Please sign in to comment.