Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(http-client): ensure default content-type is respected
Request's content type can be automatically set based on a body. This caused us to avoid applying a default content type. Default content types are now preferred over automatically-added content types.

fixes #32
  • Loading branch information
bryanrsmith committed Jan 26, 2016
1 parent 314bbc5 commit f001eba
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/http-client.js
Expand Up @@ -168,7 +168,11 @@ function buildRequest(input, init = {}) {
let bodyObj = body ? { body } : null;
let parsedDefaultHeaders = parseHeaderValues(defaults.headers);
let requestInit = Object.assign({}, defaults, { headers: {} }, source, bodyObj);
let requestContentType = new Headers(requestInit.headers).get('Content-Type');
let request = new Request((this.baseUrl || '') + url, requestInit);
if (!requestContentType && new Headers(parsedDefaultHeaders).has('content-type')) {
request.headers.set('Content-Type', new Headers(parsedDefaultHeaders).get('content-type'));
}
setDefaultHeaders(request.headers, parsedDefaultHeaders);

if (body && Blob.prototype.isPrototypeOf(body) && body.type) {
Expand Down
14 changes: 14 additions & 0 deletions test/http-client.spec.js
Expand Up @@ -425,6 +425,20 @@ describe('HttpClient', () => {
done();
});
});

it('uses default content-type header', (done) => {
fetch.and.returnValue(emptyResponse(200));
let contentType = 'application/json;charset=UTF-8';
client.defaults = { method: 'post', body: '{}', headers: { 'content-type': contentType } };

client.fetch('path')
.then(() => {
let [request] = fetch.calls.first().args;
expect(request.headers.has('content-type')).toBe(true);
expect(request.headers.get('content-type')).toBe(contentType);
done();
});
});
});
});

Expand Down

0 comments on commit f001eba

Please sign in to comment.