Skip to content

Commit

Permalink
FormData multi-part handling
Browse files Browse the repository at this point in the history
  • Loading branch information
burgalon committed Apr 28, 2019
1 parent 13c43f9 commit 45b11be
Show file tree
Hide file tree
Showing 6 changed files with 425 additions and 8 deletions.
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) {
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 @@ -323,6 +323,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

0 comments on commit 45b11be

Please sign in to comment.