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): Only set timeout & responseType if request is asynchronous #2486

Merged
merged 2 commits into from
Jun 14, 2017
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
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