Skip to content

Commit 45b11be

Browse files
committed
FormData multi-part handling
1 parent 13c43f9 commit 45b11be

File tree

6 files changed

+425
-8
lines changed

6 files changed

+425
-8
lines changed

mocha.opts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--file setupTests.js

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "axios-mock-adapter",
33
"version": "1.16.0",
4+
"engines" : { "node" : ">=8" },
45
"description": "Axios adapter that allows to easily mock requests",
56
"main": "src/index.js",
67
"scripts": {
@@ -43,6 +44,7 @@
4344
"axios": "^0.18.0",
4445
"chai": "^4.1.0",
4546
"eslint": "^5.2.0",
47+
"formdata-node": "^1.5.2",
4648
"istanbul": "^0.4.5",
4749
"mocha": "^5.2.0",
4850
"rimraf": "^2.6.1",

src/utils.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,26 @@ function isParametersMatching(parameters, required) {
6464
}
6565

6666
function isBodyMatching(body, requiredBody) {
67+
if (requiredBody instanceof FormData) {
68+
if (!(body instanceof FormData)) {
69+
return false;
70+
}
71+
var requiredBodyFormData = {};
72+
// eslint-disable-next-line
73+
for (var pair of requiredBody.entries()) {
74+
requiredBodyFormData[pair[0]] = pair[1];
75+
}
76+
var bodyFormData = {};
77+
for (pair of body.entries()) {
78+
bodyFormData[pair[0]] = pair[1];
79+
}
80+
for(var key in requiredBodyFormData) {
81+
if(!(key in bodyFormData) || requiredBodyFormData[key]!==bodyFormData[key]) {
82+
return false;
83+
}
84+
}
85+
return true;
86+
}
6787
if (requiredBody === undefined) {
6888
return true;
6989
}

test/basics.spec.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var axios = require('axios');
22
var expect = require('chai').expect;
3-
3+
var FormData = require('formdata-node').default;
44
var MockAdapter = require('../src');
55

66
describe('MockAdapter basics', function() {
@@ -323,6 +323,50 @@ describe('MockAdapter basics', function() {
323323
});
324324
});
325325

326+
describe('with FormData', function() {
327+
it('works when multipart FormData body matches', function() {
328+
var body = new FormData();
329+
body.append('key', 'value');
330+
var matchBody = new FormData;
331+
matchBody.append('key', 'value');
332+
mock.onPost('/formDataMatch', body).replyOnce(200);
333+
334+
return instance
335+
.post('/formDataMatch', matchBody)
336+
.then(function(response) {
337+
expect(response.status).to.equal(200);
338+
});
339+
});
340+
341+
it('does not reply on FormData keys mismatch', function() {
342+
var body = new FormData();
343+
body.append('key', 'value');
344+
var matchBody = new FormData;
345+
matchBody.append('some-other-key', 'value');
346+
mock.onPost('/formDataMatch', body).replyOnce(200);
347+
348+
return instance
349+
.post('/formDataNoMatch', matchBody)
350+
.catch(function(error) {
351+
expect(error.response.status).to.equal(404);
352+
});
353+
});
354+
355+
it('does not reply on FormData data mismatch', function() {
356+
var body = new FormData();
357+
body.append('key', 'value');
358+
var matchBody = new FormData;
359+
matchBody.append('key', 'another value');
360+
mock.onPost('/formDataMatch', body).replyOnce(200);
361+
362+
return instance
363+
.post('/formDataNoMatch', matchBody)
364+
.catch(function(error) {
365+
expect(error.response.status).to.equal(404);
366+
});
367+
});
368+
});
369+
326370
it('works when using baseURL', function() {
327371
instance.defaults.baseURL = 'http://www.example.org';
328372

test/setupTests.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var FormData = require('formdata-node').default;
2+
3+
beforeEach(function() {
4+
global.FormData = FormData;
5+
});
6+
7+
afterEach(function() {
8+
delete global.FormData;
9+
});

0 commit comments

Comments
 (0)