Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Dec 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions modules/@angular/http/src/backends/xhr_backend.ts
Expand Up @@ -109,9 +109,13 @@ export class XHRConnection implements Connection {

this.setDetectedContentType(req, _xhr);

if (req.headers != null) {
req.headers.forEach((values, name) => _xhr.setRequestHeader(name, values.join(',')));
if (req.headers == null) {
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
if (req.responseType != null && _xhr.responseType != null) {
Expand Down
19 changes: 19 additions & 0 deletions modules/@angular/http/test/backends/xhr_backend_spec.ts
Expand Up @@ -237,6 +237,25 @@ export function main() {
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', () => {
const headers = new Headers({'Content-Type': 'text/plain'});
const body = {test: 'val'};
Expand Down