fix(Http): add support for headers

This commit is contained in:
Itay Radotzki 2015-07-02 01:20:09 +03:00 committed by Jeff Cross
parent d381c5fc8a
commit 883b506445
2 changed files with 19 additions and 0 deletions

View File

@ -45,6 +45,11 @@ export class XHRConnection implements Connection {
ObservableWrapper.callNext(this.response, new Response(responseOptions)) ObservableWrapper.callNext(this.response, new Response(responseOptions))
}); });
// TODO(jeffbcross): make this more dynamic based on body type // 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()); this._xhr.send(this.request.text());
} }

View File

@ -16,6 +16,7 @@ import {BrowserXhr} from 'angular2/src/http/backends/browser_xhr';
import {XHRConnection, XHRBackend} from 'angular2/src/http/backends/xhr_backend'; import {XHRConnection, XHRBackend} from 'angular2/src/http/backends/xhr_backend';
import {bind, Injector} from 'angular2/di'; import {bind, Injector} from 'angular2/di';
import {Request} from 'angular2/src/http/static_request'; import {Request} from 'angular2/src/http/static_request';
import {Headers} from 'angular2/src/http/headers';
import {Map} from 'angular2/src/facade/collection'; import {Map} from 'angular2/src/facade/collection';
import {RequestOptions, BaseRequestOptions} from 'angular2/src/http/base_request_options'; import {RequestOptions, BaseRequestOptions} from 'angular2/src/http/base_request_options';
import {BaseResponseOptions, ResponseOptions} from 'angular2/src/http/base_response_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 abortSpy;
var sendSpy; var sendSpy;
var openSpy; var openSpy;
var setRequestHeaderSpy;
var addEventListenerSpy; var addEventListenerSpy;
var existingXHRs = []; var existingXHRs = [];
@ -33,6 +35,7 @@ class MockBrowserXHR extends BrowserXhr {
open: any; open: any;
response: any; response: any;
responseText: string; responseText: string;
setRequestHeader: any;
callbacks: Map<string, Function>; callbacks: Map<string, Function>;
constructor() { constructor() {
super(); super();
@ -40,6 +43,7 @@ class MockBrowserXHR extends BrowserXhr {
this.abort = abortSpy = spy.spy('abort'); this.abort = abortSpy = spy.spy('abort');
this.send = sendSpy = spy.spy('send'); this.send = sendSpy = spy.spy('send');
this.open = openSpy = spy.spy('open'); this.open = openSpy = spy.spy('open');
this.setRequestHeader = setRequestHeaderSpy = spy.spy('setRequestHeader');
this.callbacks = new Map(); this.callbacks = new Map();
} }
@ -109,6 +113,16 @@ export function main() {
new MockBrowserXHR()); new MockBrowserXHR());
expect(sendSpy).toHaveBeenCalledWith(body); 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']);
});
}); });
}); });
} }