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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FormData multi-part handling #202

Open
wants to merge 1 commit into
base: master
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ language: node_js
node_js:
- "10"
- "8"
- "6"
1 change: 1 addition & 0 deletions mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--file setupTests.js
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "axios-mock-adapter",
"version": "1.16.0",
"engines" : { "node" : ">=8" },
"description": "Axios adapter that allows to easily mock requests",
"main": "src/index.js",
"scripts": {
Expand Down Expand Up @@ -43,6 +44,7 @@
"axios": "^0.18.0",
"chai": "^4.1.0",
"eslint": "^5.2.0",
"formdata-node": "^1.5.2",
"istanbul": "^0.4.5",
"mocha": "^5.2.0",
"rimraf": "^2.6.1",
Expand Down
20 changes: 20 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,26 @@ function isParametersMatching(parameters, required) {
}

function isBodyMatching(body, requiredBody) {
if (requiredBody instanceof FormData) {
Copy link
Collaborator

@marcbachmann marcbachmann Apr 1, 2021

Choose a reason for hiding this comment

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

instanceof checks won't work as only one specific version of the module would be handled.
This would probably need some kind of duck typing.
Also, FormData isn't global in node <18.0.0, so this will break the module for older versions.

if (!(body instanceof FormData)) {
return false;
}
var requiredBodyFormData = {};
// eslint-disable-next-line
for (var pair of requiredBody.entries()) {
requiredBodyFormData[pair[0]] = pair[1];
}
var bodyFormData = {};
for (pair of body.entries()) {
bodyFormData[pair[0]] = pair[1];
}
for(var key in requiredBodyFormData) {
if(!(key in bodyFormData) || requiredBodyFormData[key]!==bodyFormData[key]) {
return false;
}
}
return true;
}
if (requiredBody === undefined) {
return true;
}
Expand Down
46 changes: 45 additions & 1 deletion test/basics.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var axios = require('axios');
var expect = require('chai').expect;

var FormData = require('formdata-node').default;
var MockAdapter = require('../src');

describe('MockAdapter basics', function() {
Expand Down Expand Up @@ -311,6 +311,50 @@ describe('MockAdapter basics', function() {
});
});

describe('with FormData', function() {
it('works when multipart FormData body matches', function() {
var body = new FormData();
body.append('key', 'value');
var matchBody = new FormData;
matchBody.append('key', 'value');
mock.onPost('/formDataMatch', body).replyOnce(200);

return instance
.post('/formDataMatch', matchBody)
.then(function(response) {
expect(response.status).to.equal(200);
});
});

it('does not reply on FormData keys mismatch', function() {
var body = new FormData();
body.append('key', 'value');
var matchBody = new FormData;
matchBody.append('some-other-key', 'value');
mock.onPost('/formDataMatch', body).replyOnce(200);

return instance
.post('/formDataNoMatch', matchBody)
.catch(function(error) {
expect(error.response.status).to.equal(404);
});
});

it('does not reply on FormData data mismatch', function() {
var body = new FormData();
body.append('key', 'value');
var matchBody = new FormData;
matchBody.append('key', 'another value');
mock.onPost('/formDataMatch', body).replyOnce(200);

return instance
.post('/formDataNoMatch', matchBody)
.catch(function(error) {
expect(error.response.status).to.equal(404);
});
});
});

it('works when using baseURL', function() {
instance.defaults.baseURL = 'http://www.example.org';

Expand Down
9 changes: 9 additions & 0 deletions test/setupTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var FormData = require('formdata-node').default;

beforeEach(function() {
global.FormData = FormData;
});

afterEach(function() {
delete global.FormData;
});
Loading