mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 04:41:36 +08:00
Promises rejected properly when loading wrong content
This commit is contained in:

committed by
vakrilov

parent
4c82981430
commit
0ff8038742
@ -50,6 +50,21 @@ export var test_getString_fail = function (done) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export var test_getString_fail_when_result_is_not_string = function (done) {
|
||||||
|
var result;
|
||||||
|
|
||||||
|
http.getJSON({ url: "https://httpbin.org/image/png", method: "GET" }).catch(function (e) {
|
||||||
|
result = e;
|
||||||
|
try {
|
||||||
|
TKUnit.assert(result instanceof Error, "Result from getString().catch() should be Error! Current type is " + typeof result);
|
||||||
|
done(null);
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
done(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
export var test_getJSON_isDefined = function () {
|
export var test_getJSON_isDefined = function () {
|
||||||
TKUnit.assert(typeof (http.getJSON) !== "undefined", "Method http.getJSON() should be defined!");
|
TKUnit.assert(typeof (http.getJSON) !== "undefined", "Method http.getJSON() should be defined!");
|
||||||
};
|
};
|
||||||
@ -91,7 +106,22 @@ export var test_getJSON_fail = function (done) {
|
|||||||
http.getJSON({ url: "hgfttp://httpbin.org/get", method: "GET", timeout: 2000 }).catch(function (e) {
|
http.getJSON({ url: "hgfttp://httpbin.org/get", method: "GET", timeout: 2000 }).catch(function (e) {
|
||||||
result = e;
|
result = e;
|
||||||
try {
|
try {
|
||||||
TKUnit.assert(result instanceof Error, "Result from getJSON().fail() should be Error! Current type is " + typeof result);
|
TKUnit.assert(result instanceof Error, "Result from getJSON().catch() should be Error! Current type is " + typeof result);
|
||||||
|
done(null);
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
done(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export var test_getJSON_fail_when_result_is_not_JSON = function (done) {
|
||||||
|
var result;
|
||||||
|
|
||||||
|
http.getJSON({ url: "https://httpbin.org/html", method: "GET" }).catch(function (e) {
|
||||||
|
result = e;
|
||||||
|
try {
|
||||||
|
TKUnit.assert(result instanceof Error, "Result from getJSON().catch() should be Error! Current type is " + typeof result);
|
||||||
done(null);
|
done(null);
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
@ -138,7 +168,22 @@ export var test_getImage_fail = function (done) {
|
|||||||
http.getImage({ url: "hgfttp://www.google.com/images/errors/logo_sm_2.png", method: "GET", timeout: 2000 }).catch(function (e) {
|
http.getImage({ url: "hgfttp://www.google.com/images/errors/logo_sm_2.png", method: "GET", timeout: 2000 }).catch(function (e) {
|
||||||
result = e;
|
result = e;
|
||||||
try {
|
try {
|
||||||
TKUnit.assert(result instanceof Error, "Result from getImage().fail() should be Error! Current type is " + typeof result);
|
TKUnit.assert(result instanceof Error, "Result from getImage().catch() should be Error! Current type is " + typeof result);
|
||||||
|
done(null);
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
done(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export var test_getImage_fail_when_result_is_not_image = function (done) {
|
||||||
|
var result;
|
||||||
|
|
||||||
|
http.getImage({ url: "https://httpbin.org/html", method: "GET" }).catch(function (e) {
|
||||||
|
result = e;
|
||||||
|
try {
|
||||||
|
TKUnit.assert(result instanceof Error, "Result from getImage().catch() should be Error! Current type is " + typeof result);
|
||||||
done(null);
|
done(null);
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
@ -157,7 +202,7 @@ export var test_request_shouldFailIfOptionsUrlIsNotDefined = function (done) {
|
|||||||
http.request({ url: undefined, method: undefined }).catch(function (e) {
|
http.request({ url: undefined, method: undefined }).catch(function (e) {
|
||||||
result = e;
|
result = e;
|
||||||
try {
|
try {
|
||||||
TKUnit.assert(result instanceof Error, "Result from request().fail() should be Error! Current type is " + typeof result);
|
TKUnit.assert(result instanceof Error, "Result from request().catch() should be Error! Current type is " + typeof result);
|
||||||
done(null);
|
done(null);
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
@ -171,7 +216,7 @@ export var test_request_requestShouldTimeout = function (done) {
|
|||||||
http.request({ url: "http://10.255.255.1", method: "GET", timeout: 500 }).catch(function (e) {
|
http.request({ url: "http://10.255.255.1", method: "GET", timeout: 500 }).catch(function (e) {
|
||||||
result = e;
|
result = e;
|
||||||
try {
|
try {
|
||||||
TKUnit.assert(result instanceof Error, "Result from request().fail() should be Error! Current type is " + typeof result);
|
TKUnit.assert(result instanceof Error, "Result from request().catch() should be Error! Current type is " + typeof result);
|
||||||
done(null);
|
done(null);
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
|
@ -62,8 +62,14 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
|
|||||||
toString: () => { return NSDataToString(data); },
|
toString: () => { return NSDataToString(data); },
|
||||||
toJSON: () => { return JSON.parse(NSDataToString(data)); },
|
toJSON: () => { return JSON.parse(NSDataToString(data)); },
|
||||||
toImage: () => {
|
toImage: () => {
|
||||||
return new Promise<imageSource.ImageSource>((resolveImage, reject) => {
|
return new Promise<imageSource.ImageSource>((resolveImage, rejectImage) => {
|
||||||
resolveImage(imageSource.fromData(data));
|
var img = imageSource.fromData(data);
|
||||||
|
if (img instanceof imageSource.ImageSource) {
|
||||||
|
resolveImage(img);
|
||||||
|
} else {
|
||||||
|
rejectImage(new Error("Response content may not be converted to an Image"));
|
||||||
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
20
http/http.ts
20
http/http.ts
@ -6,14 +6,28 @@ global.moduleMerge(httpRequest, exports);
|
|||||||
export function getString(arg: any): Promise<string> {
|
export function getString(arg: any): Promise<string> {
|
||||||
return new Promise<string>((resolve, reject) => {
|
return new Promise<string>((resolve, reject) => {
|
||||||
httpRequest.request(typeof arg === "string" ? { url: arg, method: "GET" } : arg)
|
httpRequest.request(typeof arg === "string" ? { url: arg, method: "GET" } : arg)
|
||||||
.then(r => resolve(r.content.toString()), e => reject(e));
|
.then(r => {
|
||||||
|
try {
|
||||||
|
var str = r.content.toString();
|
||||||
|
resolve(str);
|
||||||
|
} catch (e) {
|
||||||
|
reject(e);
|
||||||
|
}
|
||||||
|
}, e => reject(e));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getJSON<T>(arg: any): Promise<T> {
|
export function getJSON<T>(arg: any): Promise<T> {
|
||||||
return new Promise<T>((resolve, reject) => {
|
return new Promise<T>((resolve, reject) => {
|
||||||
httpRequest.request(typeof arg === "string" ? { url: arg, method: "GET" } : arg)
|
httpRequest.request(typeof arg === "string" ? { url: arg, method: "GET" } : arg)
|
||||||
.then(r => resolve(r.content.toJSON()), e => reject(e));
|
.then(r => {
|
||||||
|
try {
|
||||||
|
var json = r.content.toJSON();
|
||||||
|
resolve(json);
|
||||||
|
} catch (e) {
|
||||||
|
reject(e);
|
||||||
|
}
|
||||||
|
}, e => reject(e));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -21,7 +35,7 @@ export function getImage(arg: any): Promise<image.ImageSource> {
|
|||||||
return new Promise<image.ImageSource>((resolve, reject) => {
|
return new Promise<image.ImageSource>((resolve, reject) => {
|
||||||
httpRequest.request(typeof arg === "string" ? { url: arg, method: "GET" } : arg)
|
httpRequest.request(typeof arg === "string" ? { url: arg, method: "GET" } : arg)
|
||||||
.then(r => {
|
.then(r => {
|
||||||
r.content.toImage().then(source => resolve(source));
|
r.content.toImage().then(source => resolve(source), e => reject(e));
|
||||||
}, e => reject(e));
|
}, e => reject(e));
|
||||||
});
|
});
|
||||||
}
|
}
|
Reference in New Issue
Block a user