fix(Http): add support for headers
This commit is contained in:
parent
d381c5fc8a
commit
883b506445
|
@ -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());
|
||||
}
|
||||
|
||||
|
|
|
@ -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<string, Function>;
|
||||
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']);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue