Skip to content

Commit

Permalink
Support exact-matching on FormData bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekthompson committed Nov 8, 2021
1 parent 1bdac54 commit c9c4b6f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"eslint-config-origami-component": "1.0.0",
"eslint-config-prettier": "^2.9.0",
"eslint-plugin-prettier": "^2.6.1",
"form-data": "^2.3.3",
"karma": "^3.1.4",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^2.2.0",
Expand Down
2 changes: 1 addition & 1 deletion src/Route/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ const getBodyMatcher = (route, fetchMock) => {
return true;
}

let sentBody;
let sentBody = body;

try {
debug(' Parsing request body as JSON');
Expand Down
68 changes: 67 additions & 1 deletion test/specs/routing/body-matching.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const FormData = require('form-data');
const chai = require('chai');
const expect = chai.expect;

Expand Down Expand Up @@ -64,7 +65,7 @@ describe('body matching', () => {
expect(fm.calls(true).length).to.equal(0);
});

it('should not match if body sent isn’t JSON', async () => {
it('should not match if the bodies are of different types', async () => {
fm.mock({ body: { foo: 'bar' } }, 200).catch();

await fm.fetchHandler('http://a.com/', {
Expand Down Expand Up @@ -112,6 +113,54 @@ describe('body matching', () => {
expect(fm.calls(true).length).to.equal(1);
});

it('should match exact FormData bodies', async () => {
const formData = new FormData();
formData.append('foo', 'bar');

fm.mock({ body: formData }, 200).catch();

await fm.fetchHandler('http://a.com/', {
method: 'POST',
body: formData,
headers: {},
});
expect(fm.calls(true).length).to.equal(1);
});

it('should not match FormData bodies that are not the same', async () => {
const expectedFormData = new FormData();
expectedFormData.append('foo', 'bar');

fm.mock({ body: expectedFormData }, 200).catch();

const actualFormData = new FormData();
actualFormData.append('fizz', 'buzz');
await fm.fetchHandler('http://a.com/', {
method: 'POST',
body: actualFormData,
headers: {},
});
expect(fm.calls(true).length).to.equal(0);
});

it('should not ignore the order of the form fields', async () => {
const expectedFormData = new FormData();
expectedFormData.append('foo', 'bar');
expectedFormData.append('fizz', 'buzz');

fm.mock({ body: expectedFormData }, 200).catch();

const actualFormData = new FormData();
actualFormData.append('fizz', 'buzz');
actualFormData.append('foo', 'bar');
await fm.fetchHandler('http://a.com/', {
method: 'POST',
body: actualFormData,
headers: {},
});
expect(fm.calls(true).length).to.equal(0);
});

describe('partial body matching', () => {
it('match when missing properties', async () => {
fm.mock({ body: { ham: 'sandwich' }, matchPartialBody: true }, 200).catch(
Expand Down Expand Up @@ -170,5 +219,22 @@ describe('body matching', () => {
});
expect(res.status).to.equal(404);
});

it('should not support partial matches of FormData bodies', async () => {
const expectedFormData = new FormData();
expectedFormData.append('foo', 'bar');

fm.mock({ body: expectedFormData, matchPartialBody: true }, 200).catch();

const actualFormData = new FormData();
actualFormData.append('fizz', 'buzz');
actualFormData.append('foo', 'bar');
await fm.fetchHandler('http://a.com/', {
method: 'POST',
body: actualFormData,
headers: {},
});
expect(fm.calls(true).length).to.equal(0);
});
});
});

0 comments on commit c9c4b6f

Please sign in to comment.