Merge pull request #1089 from NativeScript/http-toString

http toString will raise error if response cannot be converted to string
This commit is contained in:
Vladimir Enchev
2015-11-12 14:09:28 +02:00
2 changed files with 8 additions and 2 deletions

View File

@ -53,7 +53,7 @@ export var test_getString_fail = function (done) {
export var test_getString_fail_when_result_is_not_string = function (done) { export var test_getString_fail_when_result_is_not_string = function (done) {
var result; 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; result = e;
try { try {
TKUnit.assert(result instanceof Error, "Result from getString().catch() should be Error! Current type is " + typeof result); TKUnit.assert(result instanceof Error, "Result from getString().catch() should be Error! Current type is " + typeof result);

View File

@ -45,7 +45,13 @@ function onRequestComplete(requestId: number, result: com.tns.Async.Http.Request
callbacks.resolveCallback({ callbacks.resolveCallback({
content: { content: {
raw: result.raw, 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); }, toJSON: () => { return utils.parseJSON(result.responseAsString); },
toImage: () => { toImage: () => {
return new Promise<imageSource.ImageSource>((resolveImage, rejectImage) => { return new Promise<imageSource.ImageSource>((resolveImage, rejectImage) => {