Skip to content

Commit

Permalink
fix(ajax): ensure that headers are set properly
Browse files Browse the repository at this point in the history
  • Loading branch information
benlesh committed Jan 13, 2016
1 parent 887d51c commit 1100bdd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
22 changes: 22 additions & 0 deletions spec/observables/dom/ajax-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@ describe('Observable.ajax', function () {
jasmine.Ajax.uninstall();
});

it('should set headers', function () {
Rx.Observable.ajax({
url: '/talk-to-me-goose',
headers: {
'Content-Type': 'kenny/loggins',
'Fly-Into-The': 'Dangah Zone!',
'Take-A-Ride-Into-The': 'Danger ZoooOoone!'
}
})
.subscribe();

var request = jasmine.Ajax.requests.mostRecent();

expect(request.url).toBe('/talk-to-me-goose');
expect(request.requestHeaders).toEqual({
'Content-Type': 'kenny/loggins',
'Fly-Into-The': 'Dangah Zone!',
'Take-A-Ride-Into-The': 'Danger ZoooOoone!',
'X-Requested-With': 'XMLHttpRequest'
});
});

it('should have an optional resultSelector', function () {
var expected = 'avast ye swabs!';
var result;
Expand Down
13 changes: 12 additions & 1 deletion src/observable/dom/ajax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export class AjaxSubscriber<T> extends Subscriber<Event> {
private send(): XMLHttpRequest {
const {
request,
request: { user, method, url, async, password }
request: { user, method, url, async, password, headers }
} = this;
const createXHR = request.createXHR;
const xhr = tryCatch(createXHR).call(request);
Expand All @@ -209,6 +209,9 @@ export class AjaxSubscriber<T> extends Subscriber<Event> {
xhr.timeout = request.timeout;
xhr.responseType = request.responseType;

// set headers
this.setupHeaders(xhr, headers);

// now set up the events
this.setupEvents(xhr, request);

Expand All @@ -217,6 +220,14 @@ export class AjaxSubscriber<T> extends Subscriber<Event> {
}
}

private setupHeaders(xhr: XMLHttpRequest, headers: Object) {
for (let key in headers) {
if (headers.hasOwnProperty(key)) {
xhr.setRequestHeader(key, headers[key]);
}
}
}

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

Expand Down

0 comments on commit 1100bdd

Please sign in to comment.