fix(http): set the default Accept header (#12989)

Fixes #6354
This commit is contained in:
Dzmitry Shylovich 2016-12-07 03:20:43 +03:00 committed by Alex Rickabaugh
parent 25c2141991
commit 986abbe0b2
2 changed files with 25 additions and 2 deletions

View File

@ -109,9 +109,13 @@ export class XHRConnection implements Connection {
this.setDetectedContentType(req, _xhr); this.setDetectedContentType(req, _xhr);
if (req.headers != null) { if (req.headers == null) {
req.headers.forEach((values, name) => _xhr.setRequestHeader(name, values.join(','))); req.headers = new Headers();
} }
if (!req.headers.has('Accept')) {
req.headers.append('Accept', 'application/json, text/plain, */*');
}
req.headers.forEach((values, name) => _xhr.setRequestHeader(name, values.join(',')));
// Select the correct buffer type to store the response // Select the correct buffer type to store the response
if (req.responseType != null && _xhr.responseType != null) { if (req.responseType != null && _xhr.responseType != null) {

View File

@ -237,6 +237,25 @@ export function main() {
expect(setRequestHeaderSpy).toHaveBeenCalledWith('X-Multi', 'a,b'); expect(setRequestHeaderSpy).toHaveBeenCalledWith('X-Multi', 'a,b');
}); });
it('should attach default Accept header', () => {
const headers = new Headers();
const base = new BaseRequestOptions();
const connection = new XHRConnection(
new Request(base.merge(new RequestOptions({headers}))), new MockBrowserXHR());
connection.response.subscribe();
expect(setRequestHeaderSpy)
.toHaveBeenCalledWith('Accept', 'application/json, text/plain, */*');
});
it('should not override user provided Accept header', () => {
const headers = new Headers({'Accept': 'text/xml'});
const base = new BaseRequestOptions();
const connection = new XHRConnection(
new Request(base.merge(new RequestOptions({headers}))), new MockBrowserXHR());
connection.response.subscribe();
expect(setRequestHeaderSpy).toHaveBeenCalledWith('Accept', 'text/xml');
});
it('should skip content type detection if custom content type header is set', () => { it('should skip content type detection if custom content type header is set', () => {
const headers = new Headers({'Content-Type': 'text/plain'}); const headers = new Headers({'Content-Type': 'text/plain'});
const body = {test: 'val'}; const body = {test: 'val'};