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

fix(ajax): Fix case-insensitive headers in HTTP request #4453

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions spec/observables/dom/ajax-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,26 @@ describe('ajax', () => {
expect(MockXMLHttpRequest.mostRecent.data).to.equal('{"🌟":"🚀"}');
});

it('should send json body not mattered on case-sensitivity of HTTP headers', () => {
const body = {
hello: 'world'
};

const requestObj = {
url: '/flibbertyJibbet',
method: '',
body: body,
headers: {
'cOnTeNt-TyPe': 'application/json; charset=UTF-8'
}
};

ajax(requestObj).subscribe();

expect(MockXMLHttpRequest.mostRecent.url).to.equal('/flibbertyJibbet');
expect(MockXMLHttpRequest.mostRecent.data).to.equal('{"hello":"world"}');
});

it('should error if send request throws', (done: MochaDone) => {
const expected = new Error('xhr send failure');

Expand Down
17 changes: 14 additions & 3 deletions src/internal/observable/dom/AjaxObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,17 +202,18 @@ export class AjaxSubscriber<T> extends Subscriber<Event> {
const headers = request.headers = request.headers || {};

// force CORS if requested
if (!request.crossDomain && !headers['X-Requested-With']) {
if (!request.crossDomain && !this.getHeader(headers, 'X-Requested-With')) {
headers['X-Requested-With'] = 'XMLHttpRequest';
}

// ensure content type is set
if (!('Content-Type' in headers) && !(root.FormData && request.body instanceof root.FormData) && typeof request.body !== 'undefined') {
let contentTypeHeader = this.getHeader(headers, 'Content-Type');
if (!contentTypeHeader && !(root.FormData && request.body instanceof root.FormData) && typeof request.body !== 'undefined') {
headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
}

// properly serialize body
request.body = this.serializeBody(request.body, request.headers['Content-Type']);
request.body = this.serializeBody(request.body, this.getHeader(request.headers, 'Content-Type'));

this.send();
}
Expand Down Expand Up @@ -315,6 +316,16 @@ export class AjaxSubscriber<T> extends Subscriber<Event> {
}
}

private getHeader(headers: {}, headerName: string): any {
for (let key in headers) {
if (key.toLowerCase() === headerName.toLowerCase()) {
return headers[key];
}
}

return undefined;
}

private setupEvents(xhr: XMLHttpRequest, request: AjaxRequest) {
const progressSubscriber = request.progressSubscriber;

Expand Down