Promises rejected properly when loading wrong content

This commit is contained in:
Vladimir Enchev
2015-09-02 11:40:50 +03:00
committed by vakrilov
parent 4c82981430
commit 0ff8038742
3 changed files with 74 additions and 9 deletions

View File

@ -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 () {
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) {
result = e;
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);
}
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) {
result = e;
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);
}
catch (err) {
@ -157,7 +202,7 @@ export var test_request_shouldFailIfOptionsUrlIsNotDefined = function (done) {
http.request({ url: undefined, method: undefined }).catch(function (e) {
result = e;
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);
}
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) {
result = e;
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);
}
catch (err) {

View File

@ -62,8 +62,14 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
toString: () => { return NSDataToString(data); },
toJSON: () => { return JSON.parse(NSDataToString(data)); },
toImage: () => {
return new Promise<imageSource.ImageSource>((resolveImage, reject) => {
resolveImage(imageSource.fromData(data));
return new Promise<imageSource.ImageSource>((resolveImage, rejectImage) => {
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"));
}
});
}
},

View File

@ -6,14 +6,28 @@ global.moduleMerge(httpRequest, exports);
export function getString(arg: any): Promise<string> {
return new Promise<string>((resolve, reject) => {
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> {
return new Promise<T>((resolve, reject) => {
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) => {
httpRequest.request(typeof arg === "string" ? { url: arg, method: "GET" } : arg)
.then(r => {
r.content.toImage().then(source => resolve(source));
r.content.toImage().then(source => resolve(source), e => reject(e));
}, e => reject(e));
});
}