Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support exact-matching on FormData bodies #622

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
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
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
@@ -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鈥檛 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 () => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might be able to do something more complicated here, but I was a touch worried about the semantics of having multiple values for the same keys/arrays/etc.

We'd probably want to convert the FormData objects to JS objects and compare those as we are today (with isEqual) instead?

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);
});
});
});