From 537e99b4eac36e06bed2e908fa31858fa78267a0 Mon Sep 17 00:00:00 2001 From: Vivek Ghaisas Date: Fri, 10 Jun 2016 17:18:49 +0200 Subject: [PATCH] fix(http): respect custom Content-Type header in XHRConnection (#9131) Fix a bug due to which setting a custom Content-Type header in the Request led to multiple Content-Types being set on the sent XMLHttpRequest: first the detected content type based on the request body, followed by the custom set content type. Fix #9130. --- modules/@angular/http/src/backends/xhr_backend.ts | 2 +- .../@angular/http/test/backends/xhr_backend_spec.ts | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/modules/@angular/http/src/backends/xhr_backend.ts b/modules/@angular/http/src/backends/xhr_backend.ts index 6a0cb815e6..5478d190e9 100644 --- a/modules/@angular/http/src/backends/xhr_backend.ts +++ b/modules/@angular/http/src/backends/xhr_backend.ts @@ -108,7 +108,7 @@ export class XHRConnection implements Connection { setDetectedContentType(req: any /** TODO #9100 */, _xhr: any /** TODO #9100 */) { // Skip if a custom Content-Type header is provided - if (isPresent(req.headers) && isPresent(req.headers['Content-Type'])) { + if (isPresent(req.headers) && isPresent(req.headers.get('Content-Type'))) { return; } diff --git a/modules/@angular/http/test/backends/xhr_backend_spec.ts b/modules/@angular/http/test/backends/xhr_backend_spec.ts index 77fc078e77..3569ca8e1b 100644 --- a/modules/@angular/http/test/backends/xhr_backend_spec.ts +++ b/modules/@angular/http/test/backends/xhr_backend_spec.ts @@ -212,6 +212,18 @@ export function main() { expect(setRequestHeaderSpy).toHaveBeenCalledWith('X-Multi', 'a,b'); }); + it('should skip content type detection if custom content type header is set', () => { + let headers = new Headers({'Content-Type': 'text/plain'}); + let body = {test: 'val'}; + let base = new BaseRequestOptions(); + let connection = new XHRConnection( + new Request(base.merge(new RequestOptions({body: body, headers: headers}))), + new MockBrowserXHR()); + connection.response.subscribe(); + expect(setRequestHeaderSpy).toHaveBeenCalledWith('Content-Type', 'text/plain'); + expect(setRequestHeaderSpy).not.toHaveBeenCalledWith('Content-Type', 'application/json'); + }); + it('should use object body and detect content type header to the request', () => { var body = {test: 'val'}; var base = new BaseRequestOptions();