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 +}; diff --git a/apps/tests/http-tests.ts b/apps/tests/http-tests.ts index fc1d144e3..58abade52 100644 --- a/apps/tests/http-tests.ts +++ b/apps/tests/http-tests.ts @@ -598,3 +598,34 @@ 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(); +} + +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 88412183b..d5154095d 100644 --- a/declarations.d.ts +++ b/declarations.d.ts @@ -37,7 +37,11 @@ declare class XMLHttpRequest { overrideMimeType(mime: string): void; readyState: number; responseText: string; + responseType: string; status: number; + + onload: () => void; + onerror: () => void; } /** diff --git a/http/http.ts b/http/http.ts index 0bdc7bac6..0e9a22ef2 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; @@ -46,6 +49,7 @@ export class XMLHttpRequest { private _responseText: string = ""; private _headers: any; private _errorFlag: boolean; + private _responseType: string; public onreadystatechange: Function; @@ -112,8 +116,9 @@ export class XMLHttpRequest { } }).catch(e => { - this._errorFlag = true; - }); + this._errorFlag = true; + this._setReadyState(this.DONE); + }); } } @@ -159,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; @@ -167,6 +184,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 +256,4 @@ var statuses = { 503: "Service Unavailable", 504: "Gateway Timeout", 505: "HTTP Version Not Supported" -}; \ No newline at end of file +};