From 7d5e752e5c2ce1c410f3b9086d04e0d5d0dd2277 Mon Sep 17 00:00:00 2001 From: Jonathan Rehm Date: Thu, 23 Mar 2017 10:48:19 -0700 Subject: [PATCH 1/2] fix(ajax): Only set timeout & responseType if request is asynchronous --- src/observable/dom/AjaxObservable.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/observable/dom/AjaxObservable.ts b/src/observable/dom/AjaxObservable.ts index 6ceb35c801..ec6b60bb3b 100644 --- a/src/observable/dom/AjaxObservable.ts +++ b/src/observable/dom/AjaxObservable.ts @@ -245,8 +245,10 @@ export class AjaxSubscriber extends Subscriber { } // 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; From 1e7375521f76231c8d97300ac39d34e6840b7aef Mon Sep 17 00:00:00 2001 From: Jonathan Rehm Date: Fri, 24 Mar 2017 09:13:30 -0700 Subject: [PATCH 2/2] fix(ajax): Add test for asynchronous & synchronous calls --- spec/observables/dom/ajax-spec.ts | 68 ++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/spec/observables/dom/ajax-spec.ts b/spec/observables/dom/ajax-spec.ts index 854380f2bc..d80dc79ec0 100644 --- a/spec/observables/dom/ajax-spec.ts +++ b/spec/observables/dom/ajax-spec.ts @@ -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; @@ -893,6 +952,8 @@ class MockXMLHttpRequest { private eventHandlers: Array = []; private readyState: number = 0; + private async: boolean = true; + private user: any; private password: any; @@ -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; @@ -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 {