Merge pull request #326 from hdeshev/xhr-compat

XHR compatibility updates
This commit is contained in:
Hristo Deshev
2015-06-16 09:25:18 +03:00
4 changed files with 74 additions and 10 deletions

View File

@@ -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;
}
}
};
};

View File

@@ -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);
}

4
declarations.d.ts vendored
View File

@@ -37,7 +37,11 @@ declare class XMLHttpRequest {
overrideMimeType(mime: string): void;
readyState: number;
responseText: string;
responseType: string;
status: number;
onload: () => void;
onerror: () => void;
}
/**

View File

@@ -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"
};
};