From 7f968246e8d6148a91006bd2959d831c13f752b1 Mon Sep 17 00:00:00 2001 From: Vladimir Enchev Date: Thu, 12 Nov 2015 13:49:47 +0200 Subject: [PATCH] http toString will raise error if response cannot be converted to string --- apps/tests/http-tests.ts | 2 +- http/http-request.android.ts | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/apps/tests/http-tests.ts b/apps/tests/http-tests.ts index 6094a0da6..b50f0949b 100644 --- a/apps/tests/http-tests.ts +++ b/apps/tests/http-tests.ts @@ -53,7 +53,7 @@ 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) { + http.getString({ 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); diff --git a/http/http-request.android.ts b/http/http-request.android.ts index e11ae10c9..7a07b5b89 100644 --- a/http/http-request.android.ts +++ b/http/http-request.android.ts @@ -45,7 +45,13 @@ function onRequestComplete(requestId: number, result: com.tns.Async.Http.Request callbacks.resolveCallback({ content: { raw: result.raw, - toString: () => { return result.responseAsString; }, + toString: () => { + if (types.isString(result.responseAsString)) { + return result.responseAsString; + } else { + throw new Error("Response content may not be converted to string"); + } + }, toJSON: () => { return utils.parseJSON(result.responseAsString); }, toImage: () => { return new Promise((resolveImage, rejectImage) => {