diff --git a/apps/tests/http-tests.ts b/apps/tests/http-tests.ts index a3c7b49a7..30bd859b0 100644 --- a/apps/tests/http-tests.ts +++ b/apps/tests/http-tests.ts @@ -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) { diff --git a/apps/tests/ui/list-view/list-view-tests.ts b/apps/tests/ui/list-view/list-view-tests.ts index cc1c113d3..6a744d647 100644 --- a/apps/tests/ui/list-view/list-view-tests.ts +++ b/apps/tests/ui/list-view/list-view-tests.ts @@ -473,7 +473,7 @@ export function test_loadMoreItems_is_raised_when_scroll_to_last_item() { listView.scrollToIndex(MANY_ITEMS.length - 1); TKUnit.wait(ASYNC); - TKUnit.assertEqual(loadMoreItemsCount, 1, "loadMoreItemsCount"); + TKUnit.assert(loadMoreItemsCount > 0, "loadMoreItemsCount"); }; helper.buildUIAndRunTest(listView, testAction); diff --git a/http/http-request.ios.ts b/http/http-request.ios.ts index e22e73eb9..da8db20ea 100644 --- a/http/http-request.ios.ts +++ b/http/http-request.ios.ts @@ -62,8 +62,14 @@ export function request(options: http.HttpRequestOptions): Promise { return NSDataToString(data); }, toJSON: () => { return JSON.parse(NSDataToString(data)); }, toImage: () => { - return new Promise((resolveImage, reject) => { - resolveImage(imageSource.fromData(data)); + return new Promise((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")); + } + }); } }, diff --git a/http/http.ts b/http/http.ts index d846f241d..e34de3aab 100644 --- a/http/http.ts +++ b/http/http.ts @@ -6,14 +6,28 @@ global.moduleMerge(httpRequest, exports); export function getString(arg: any): Promise { return new Promise((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(arg: any): Promise { return new Promise((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 { return new Promise((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)); }); } \ No newline at end of file