From acfb51bba260822700090cdac4fb6fc60e7d08bb Mon Sep 17 00:00:00 2001 From: Hristo Deshev Date: Fri, 12 Jun 2015 18:58:12 +0300 Subject: [PATCH 1/3] TKUnit: add exception guard and fail the test instead of crashing the app. --- apps/tests/TKUnit.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/apps/tests/TKUnit.ts b/apps/tests/TKUnit.ts index ef4e4eee2..3c607b265 100644 --- a/apps/tests/TKUnit.ts +++ b/apps/tests/TKUnit.ts @@ -120,12 +120,15 @@ function runAsync(testInfo: TestInfoEntry, recursiveIndex: number, testTimeout?: } } - if (testInfo.instance) { - testInfo.testFunc.apply(testInfo.instance, [doneCallback]); - } - else { - var func: any = testInfo.testFunc; - func(doneCallback); + try { + if (testInfo.instance) { + testInfo.testFunc.apply(testInfo.instance, [doneCallback]); + } else { + var func: any = testInfo.testFunc; + func(doneCallback); + } + } catch (e) { + doneCallback(e); } setTimeout(checkFinished, 0); @@ -342,4 +345,4 @@ var doModalAndroid = function (quitLoop: () => boolean, timeoutSec: number) { quit = true; } } -}; \ No newline at end of file +}; From f109cbe80bded17e749c930c64a90ed4250f8ee9 Mon Sep 17 00:00:00 2001 From: Hristo Deshev Date: Mon, 15 Jun 2015 14:28:16 +0300 Subject: [PATCH 2/3] Support onload/onerror events on XMLHttpRequest. --- apps/tests/http-tests.ts | 18 ++++++++++++++++++ declarations.d.ts | 3 +++ http/http.ts | 19 ++++++++++++++++--- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/apps/tests/http-tests.ts b/apps/tests/http-tests.ts index fc1d144e3..1f3797e0f 100644 --- a/apps/tests/http-tests.ts +++ b/apps/tests/http-tests.ts @@ -598,3 +598,21 @@ export var test_XMLHttpRequest_requestShouldBePossibleAfterAbort = function (don xhr.send(JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" })); }; + +export function test_raises_onload_Event(done) { + let xhr = new XMLHttpRequest(); + xhr.onload = () => { + done(null); + } + xhr.open("GET", "https://httpbin.org/get"); + xhr.send(); +} + +export function test_raises_onerror_Event(done) { + let xhr = new XMLHttpRequest(); + xhr.onerror = () => { + done(null); + } + xhr.open("GET", "https://no-such-domain-httpbin.org"); + xhr.send(); +} diff --git a/declarations.d.ts b/declarations.d.ts index 88412183b..bcb43448a 100644 --- a/declarations.d.ts +++ b/declarations.d.ts @@ -38,6 +38,9 @@ declare class XMLHttpRequest { readyState: number; responseText: string; status: number; + + onload: () => void; + onerror: () => void; } /** diff --git a/http/http.ts b/http/http.ts index 0bdc7bac6..da8b385d7 100644 --- a/http/http.ts +++ b/http/http.ts @@ -39,6 +39,9 @@ export class XMLHttpRequest { public LOADING = 3; public DONE = 4; + public onload: () => void; + public onerror: () => void; + private _options: definition.HttpRequestOptions; private _readyState: number; private _status: number; @@ -112,8 +115,9 @@ export class XMLHttpRequest { } }).catch(e => { - this._errorFlag = true; - }); + this._errorFlag = true; + this._setReadyState(this.DONE); + }); } } @@ -167,6 +171,15 @@ export class XMLHttpRequest { this.onreadystatechange(); } } + + if (this._readyState === this.DONE) { + if (this._errorFlag && types.isFunction(this.onerror)) { + this.onerror(); + } + if (!this._errorFlag && types.isFunction(this.onload)) { + this.onload(); + } + } } get responseText(): string { @@ -230,4 +243,4 @@ var statuses = { 503: "Service Unavailable", 504: "Gateway Timeout", 505: "HTTP Version Not Supported" -}; \ No newline at end of file +}; From 51a56a2aea39ec70413ae0fd852dbbd93a2e8cf3 Mon Sep 17 00:00:00 2001 From: Hristo Deshev Date: Mon, 15 Jun 2015 15:25:43 +0300 Subject: [PATCH 3/3] Support responseType property on XMLHttpRequest. Only default, "" and "text" supported for now. Raising an error otherwise. --- apps/tests/http-tests.ts | 13 +++++++++++++ declarations.d.ts | 1 + http/http.ts | 13 +++++++++++++ 3 files changed, 27 insertions(+) diff --git a/apps/tests/http-tests.ts b/apps/tests/http-tests.ts index 1f3797e0f..58abade52 100644 --- a/apps/tests/http-tests.ts +++ b/apps/tests/http-tests.ts @@ -616,3 +616,16 @@ export function test_raises_onerror_Event(done) { xhr.open("GET", "https://no-such-domain-httpbin.org"); xhr.send(); } + +export function test_responseType(done) { + let xhr = new XMLHttpRequest(); + xhr.responseType = ""; + xhr.responseType = "text"; + + TKUnit.assertThrows( + () => xhr.responseType = "json", + "Didn't raise on unsupported type.", + "Response type of 'json' not supported." + ); + done(null); +} diff --git a/declarations.d.ts b/declarations.d.ts index bcb43448a..d5154095d 100644 --- a/declarations.d.ts +++ b/declarations.d.ts @@ -37,6 +37,7 @@ declare class XMLHttpRequest { overrideMimeType(mime: string): void; readyState: number; responseText: string; + responseType: string; status: number; onload: () => void; diff --git a/http/http.ts b/http/http.ts index da8b385d7..0e9a22ef2 100644 --- a/http/http.ts +++ b/http/http.ts @@ -49,6 +49,7 @@ export class XMLHttpRequest { private _responseText: string = ""; private _headers: any; private _errorFlag: boolean; + private _responseType: string; public onreadystatechange: Function; @@ -163,6 +164,18 @@ export class XMLHttpRequest { return this._readyState; } + public get responseType(): string { + return this._responseType; + } + + public set responseType(value: string) { + if (value === "" || value === "text") { + this._responseType = value; + } else { + throw new Error(`Response type of '${value}' not supported.`); + } + } + private _setReadyState(value: number) { if (this._readyState !== value) { this._readyState = value;