From 380fbcffb34879659b8adabdc81c999bd54111f4 Mon Sep 17 00:00:00 2001 From: Jonathan Rehm Date: Wed, 14 Jun 2017 11:00:01 -0700 Subject: [PATCH] fix(ajax): Only set timeout & responseType if request is asynchronous (#2486) * fix(ajax): Only set timeout & responseType if request is asynchronous * fix(ajax): Add test for asynchronous & synchronous calls --- spec/observables/dom/ajax-spec.ts | 68 +++++++++++++++++++++++++++- src/observable/dom/AjaxObservable.ts | 6 ++- 2 files changed, 71 insertions(+), 3 deletions(-) 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 { 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;