From 450c5fb9cf5cf24bc7e2272531b9803bc626ef38 Mon Sep 17 00:00:00 2001 From: Alexis Cramatte Date: Wed, 17 Aug 2016 21:27:10 +0200 Subject: [PATCH] Fix: Content type with "+json" suffix are now parsed as JSON --- tests/app/xhr-tests.ts | 22 +++++++++++++++++++++- tns-core-modules/xhr/xhr.ts | 2 +- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/tests/app/xhr-tests.ts b/tests/app/xhr-tests.ts index 21069d342..057b87445 100644 --- a/tests/app/xhr-tests.ts +++ b/tests/app/xhr-tests.ts @@ -298,7 +298,27 @@ export function test_xhr_responseType_switched_to_JSON_if_header_present() { "Content-Type": "application/json" } - } + }; + xhr._loadResponse(response); + + TKUnit.assertEqual(xhr.responseType, "json"); + TKUnit.assertEqual(xhr.response.data, 42); +} + +export function test_xhr_responseType_switched_to_JSON_if_headers_content_type_has_json_suffix() { + const xhr = new XMLHttpRequest(); + const response = { + statusCode: 200, + content: { + toString: function () { + return this.raw + }, + raw: '{"data": 42}' + }, + headers: { + "Content-Type": "type/media.type+json" + } + }; xhr._loadResponse(response); TKUnit.assertEqual(xhr.responseType, "json"); diff --git a/tns-core-modules/xhr/xhr.ts b/tns-core-modules/xhr/xhr.ts index 720b2abd2..7911d3c71 100644 --- a/tns-core-modules/xhr/xhr.ts +++ b/tns-core-modules/xhr/xhr.ts @@ -132,7 +132,7 @@ export class XMLHttpRequest { const contentType = header && header.toLowerCase(); if (contentType) { - if (contentType.indexOf('application/json') >= 0) { + if (contentType.indexOf('application/json') >= 0 || contentType.indexOf('+json') >= 0) { this.responseType = XMLHttpRequestResponseType.json; } else if (contentType.indexOf('text/plain') >= 0) { this.responseType = XMLHttpRequestResponseType.text;