Skip to content

Commit f61e74e

Browse files
committed
FormData multi-part handling
1 parent d742b23 commit f61e74e

File tree

7 files changed

+425
-9
lines changed

7 files changed

+425
-9
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ language: node_js
22
node_js:
33
- "10"
44
- "8"
5-
- "6"

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() {
@@ -311,6 +311,50 @@ describe('MockAdapter basics', function() {
311311
});
312312
});
313313

314+
describe('with FormData', function() {
315+
it('works when multipart FormData body matches', function() {
316+
var body = new FormData();
317+
body.append('key', 'value');
318+
var matchBody = new FormData;
319+
matchBody.append('key', 'value');
320+
mock.onPost('/formDataMatch', body).replyOnce(200);
321+
322+
return instance
323+
.post('/formDataMatch', matchBody)
324+
.then(function(response) {
325+
expect(response.status).to.equal(200);
326+
});
327+
});
328+
329+
it('does not reply on FormData keys mismatch', function() {
330+
var body = new FormData();
331+
body.append('key', 'value');
332+
var matchBody = new FormData;
333+
matchBody.append('some-other-key', 'value');
334+
mock.onPost('/formDataMatch', body).replyOnce(200);
335+
336+
return instance
337+
.post('/formDataNoMatch', matchBody)
338+
.catch(function(error) {
339+
expect(error.response.status).to.equal(404);
340+
});
341+
});
342+
343+
it('does not reply on FormData data mismatch', function() {
344+
var body = new FormData();
345+
body.append('key', 'value');
346+
var matchBody = new FormData;
347+
matchBody.append('key', 'another value');
348+
mock.onPost('/formDataMatch', body).replyOnce(200);
349+
350+
return instance
351+
.post('/formDataNoMatch', matchBody)
352+
.catch(function(error) {
353+
expect(error.response.status).to.equal(404);
354+
});
355+
});
356+
});
357+
314358
it('works when using baseURL', function() {
315359
instance.defaults.baseURL = 'http://www.example.org';
316360

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)