Fix: Content type with "+json" suffix are now parsed as JSON

This commit is contained in:
Alexis Cramatte
2016-08-17 21:27:10 +02:00
parent cdd6c7ace1
commit 450c5fb9cf
2 changed files with 22 additions and 2 deletions

View File

@@ -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 = <any>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");

View File

@@ -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;