Skip to content

Commit

Permalink
fix(ajax): Only set timeout & responseType if request is asynchronous (
Browse files Browse the repository at this point in the history
…#2486)

* fix(ajax): Only set timeout & responseType if request is asynchronous

* fix(ajax): Add test for asynchronous & synchronous calls
  • Loading branch information
jkrehm authored and benlesh committed Jun 14, 2017
1 parent fc39043 commit 380fbcf
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
68 changes: 67 additions & 1 deletion spec/observables/dom/ajax-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,65 @@ describe('Observable.ajax', () => {
});
});

it('should create an asynchronous request', () => {
const obj: Rx.AjaxRequest = {
url: '/flibbertyJibbet',
responseType: 'text',
timeout: 10
};

Rx.Observable.ajax(obj)
.subscribe((x: any) => {
expect(x.status).to.equal(200);
expect(x.xhr.method).to.equal('GET');
expect(x.xhr.async).to.equal(true);
expect(x.xhr.timeout).to.equal(10);
expect(x.xhr.responseType).to.equal('text');
}, () => {
throw 'should not have been called';
});

const request = MockXMLHttpRequest.mostRecent;

expect(request.url).to.equal('/flibbertyJibbet');

request.respondWith({
'status': 200,
'contentType': 'text/plain',
'responseText': 'Wee! I am text!'
});
});

it('should create a synchronous request', () => {
const obj: Rx.AjaxRequest = {
url: '/flibbertyJibbet',
responseType: 'text',
timeout: 10,
async: false
};

Rx.Observable.ajax(obj)
.subscribe((x: any) => {
expect(x.status).to.equal(200);
expect(x.xhr.method).to.equal('GET');
expect(x.xhr.async).to.equal(false);
expect(x.xhr.timeout).to.be.undefined;
expect(x.xhr.responseType).to.equal('');
}, () => {
throw 'should not have been called';
});

const request = MockXMLHttpRequest.mostRecent;

expect(request.url).to.equal('/flibbertyJibbet');

request.respondWith({
'status': 200,
'contentType': 'text/plain',
'responseText': 'Wee! I am text!'
});
});

describe('ajax request body', () => {
let rFormData: FormData;

Expand Down Expand Up @@ -893,6 +952,8 @@ class MockXMLHttpRequest {
private eventHandlers: Array<any> = [];
private readyState: number = 0;

private async: boolean = true;

private user: any;
private password: any;

Expand Down Expand Up @@ -926,6 +987,7 @@ class MockXMLHttpRequest {
open(method: any, url: any, async: any, user: any, password: any): void {
this.method = method;
this.url = url;
this.async = async;
this.user = user;
this.password = password;
this.readyState = 1;
Expand Down Expand Up @@ -969,7 +1031,11 @@ class MockXMLHttpRequest {
}

protected defaultResponseValue() {
throw new Error('unhandled type "' + this.responseType + '"');
if (this.async === false) {
this.response = this.responseText;
} else {
throw new Error('unhandled type "' + this.responseType + '"');
}
}

respondWith(response: any, progressTimes?: number): void {
Expand Down
6 changes: 4 additions & 2 deletions src/observable/dom/AjaxObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,10 @@ export class AjaxSubscriber<T> extends Subscriber<Event> {
}

// timeout, responseType and withCredentials can be set once the XHR is open
xhr.timeout = request.timeout;
xhr.responseType = request.responseType;
if (async) {
xhr.timeout = request.timeout;
xhr.responseType = request.responseType;
}

if ('withCredentials' in xhr) {
xhr.withCredentials = !!request.withCredentials;
Expand Down

0 comments on commit 380fbcf

Please sign in to comment.