mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Merge pull request #326 from hdeshev/xhr-compat
XHR compatibility updates
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
@@ -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
4
declarations.d.ts
vendored
@@ -37,7 +37,11 @@ declare class XMLHttpRequest {
|
||||
overrideMimeType(mime: string): void;
|
||||
readyState: number;
|
||||
responseText: string;
|
||||
responseType: string;
|
||||
status: number;
|
||||
|
||||
onload: () => void;
|
||||
onerror: () => void;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
32
http/http.ts
32
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"
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user