From 883b5064454c4b0d80994e3a6945017fd0eb192b Mon Sep 17 00:00:00 2001 From: Itay Radotzki Date: Thu, 2 Jul 2015 01:20:09 +0300 Subject: [PATCH] fix(Http): add support for headers --- modules/angular2/src/http/backends/xhr_backend.ts | 5 +++++ .../test/http/backends/xhr_backend_spec.ts | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/modules/angular2/src/http/backends/xhr_backend.ts b/modules/angular2/src/http/backends/xhr_backend.ts index b7cd90bbc3..a92e283ed7 100644 --- a/modules/angular2/src/http/backends/xhr_backend.ts +++ b/modules/angular2/src/http/backends/xhr_backend.ts @@ -45,6 +45,11 @@ export class XHRConnection implements Connection { ObservableWrapper.callNext(this.response, new Response(responseOptions)) }); // TODO(jeffbcross): make this more dynamic based on body type + + if (isPresent(req.headers)) { + req.headers.forEach((value, name) => { this._xhr.setRequestHeader(name, value); }); + } + this._xhr.send(this.request.text()); } diff --git a/modules/angular2/test/http/backends/xhr_backend_spec.ts b/modules/angular2/test/http/backends/xhr_backend_spec.ts index 43fce548f0..f6982c7d44 100644 --- a/modules/angular2/test/http/backends/xhr_backend_spec.ts +++ b/modules/angular2/test/http/backends/xhr_backend_spec.ts @@ -16,6 +16,7 @@ import {BrowserXhr} from 'angular2/src/http/backends/browser_xhr'; import {XHRConnection, XHRBackend} from 'angular2/src/http/backends/xhr_backend'; import {bind, Injector} from 'angular2/di'; import {Request} from 'angular2/src/http/static_request'; +import {Headers} from 'angular2/src/http/headers'; import {Map} from 'angular2/src/facade/collection'; import {RequestOptions, BaseRequestOptions} from 'angular2/src/http/base_request_options'; import {BaseResponseOptions, ResponseOptions} from 'angular2/src/http/base_response_options'; @@ -24,6 +25,7 @@ import {ResponseTypes} from 'angular2/src/http/enums'; var abortSpy; var sendSpy; var openSpy; +var setRequestHeaderSpy; var addEventListenerSpy; var existingXHRs = []; @@ -33,6 +35,7 @@ class MockBrowserXHR extends BrowserXhr { open: any; response: any; responseText: string; + setRequestHeader: any; callbacks: Map; constructor() { super(); @@ -40,6 +43,7 @@ class MockBrowserXHR extends BrowserXhr { this.abort = abortSpy = spy.spy('abort'); this.send = sendSpy = spy.spy('send'); this.open = openSpy = spy.spy('open'); + this.setRequestHeader = setRequestHeaderSpy = spy.spy('setRequestHeader'); this.callbacks = new Map(); } @@ -109,6 +113,16 @@ export function main() { new MockBrowserXHR()); expect(sendSpy).toHaveBeenCalledWith(body); }); + + it('should attach headers to the request', () => { + var headers = new Headers({'Content-Type': 'text/xml', 'Breaking-Bad': '<3'}); + + var base = new BaseRequestOptions(); + new XHRConnection(new Request(base.merge(new RequestOptions({headers: headers}))), + new MockBrowserXHR()); + expect(setRequestHeaderSpy).toHaveBeenCalledWith('Content-Type', ['text/xml']); + expect(setRequestHeaderSpy).toHaveBeenCalledWith('Breaking-Bad', ['<3']); + }); }); }); }