Skip to content

Commit

Permalink
Fixed multipart/form-data requests content type boundary
Browse files Browse the repository at this point in the history
  • Loading branch information
Amareis committed Jan 13, 2018
1 parent 808c9ef commit b514172
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 9 deletions.
5 changes: 4 additions & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "another-rest-client",
"version": "0.4.1",
"version": "0.4.2",
"description": "Simple REST API client that makes your code lesser and more beautiful than without it.",
"main": "rest-client.js",
"scripts": {
Expand Down Expand Up @@ -30,5 +30,8 @@
"mocha": "^2.5.1",
"sinon": "^1.17.4",
"webpack": "^1.13.1"
},
"dependencies": {
"form-data": "^2.3.1"
}
}
2 changes: 1 addition & 1 deletion rest-client.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rest-client.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion rest-client.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rest-client.min.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/rest-client.js
Expand Up @@ -62,7 +62,8 @@ class RestClient {
let mime = this._opts[contentType];
if (mime && mime.encode)
data = safe(mime.encode, data);
xhr.setRequestHeader('Content-Type', contentType);
if (!(contentType === 'multipart/form-data' && data.constructor.name === 'FormData'))
xhr.setRequestHeader('Content-Type', contentType);
}

let p = new Promise((resolve, reject) =>
Expand Down
16 changes: 13 additions & 3 deletions test/test.js
@@ -1,11 +1,13 @@
var should = require('chai').should();
var sinon = require('sinon');
var FormData = require('form-data');

var RestClient = require('../rest-client');

var host = 'http://example.com';

xhr = global.XMLHttpRequest = sinon.useFakeXMLHttpRequest();
global.FormData = FormData;

describe('RestClient', () => {
describe('#_request()', () => {
Expand Down Expand Up @@ -36,8 +38,8 @@ describe('RestClient', () => {
var req, bool;
xhr.onCreate = r => req = r;

var p = new RestClient(host, {trailing: '/'}).on('request', xhr => bool = true).res('cookies').get({fresh: true});
req.url.should.be.equal(host + '/cookies/?fresh=true');
var p = api.on('request', xhr => bool = true).cookies.get({fresh: true});
req.url.should.be.equal(host + '/cookies?fresh=true');

setTimeout(() => req.respond(200, [], '{a:1}'), 0);

Expand All @@ -47,6 +49,14 @@ describe('RestClient', () => {
}).catch(done);
});

it('should correct handle form data', () => {
var req;
xhr.onCreate = r => req = r;

var p = api.cookies.post(new FormData(), 'multipart/form-data');
req.url.should.be.equal(host + '/cookies');
(typeof req.requestHeaders['Content-Type']).should.be.equal('undefined');
});
});
});

Expand Down Expand Up @@ -248,7 +258,7 @@ describe('resource', () => {

var p = api.cookies.get({fresh: true}).on('success', xhr => respText = xhr.responseText);

req.respond(200, [], '{a:1}');
setTimeout(() => req.respond(200, [], '{a:1}'), 0);

req.url.should.be.equal(host + '/cookies?fresh=true');
p.then(r => {
Expand Down

0 comments on commit b514172

Please sign in to comment.