From 4f7fcc8f101d76c8b2fc749a33dfd20b603a5acf Mon Sep 17 00:00:00 2001 From: Demetrio Girardi Date: Wed, 27 Sep 2023 07:01:25 -0700 Subject: [PATCH] Core: do not log errors about invalid XML when serializing XHR objects (#10483) --- src/ajax.js | 24 +++++++++++++++--------- test/spec/unit/core/ajax_spec.js | 22 +++++++++++++++++----- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/ajax.js b/src/ajax.js index 0601cc0e22b..6f2f45e935e 100644 --- a/src/ajax.js +++ b/src/ajax.js @@ -89,6 +89,17 @@ export function fetcherFactory(timeout = 3000, {request, done} = {}) { function toXHR({status, statusText = '', headers, url}, responseText) { let xml = 0; + function getXML(onError) { + if (xml === 0) { + try { + xml = new DOMParser().parseFromString(responseText, headers?.get(CTYPE)?.split(';')?.[0]) + } catch (e) { + xml = null; + onError && onError(e) + } + } + return xml; + } return { readyState: XMLHttpRequest.DONE, status, @@ -98,17 +109,12 @@ function toXHR({status, statusText = '', headers, url}, responseText) { responseType: '', responseURL: url, get responseXML() { - if (xml === 0) { - try { - xml = new DOMParser().parseFromString(responseText, headers?.get(CTYPE)?.split(';')?.[0]) - } catch (e) { - xml = null; - logError(e); - } - } - return xml; + return getXML(logError); }, getResponseHeader: (header) => headers?.has(header) ? headers.get(header) : null, + toJSON() { + return Object.assign({responseXML: getXML()}, this) + } } } diff --git a/test/spec/unit/core/ajax_spec.js b/test/spec/unit/core/ajax_spec.js index df0ce02c15c..a3a0459b980 100644 --- a/test/spec/unit/core/ajax_spec.js +++ b/test/spec/unit/core/ajax_spec.js @@ -1,7 +1,8 @@ -import {dep, attachCallbacks, fetcherFactory, toFetchRequest} from '../../../../src/ajax.js'; +import {attachCallbacks, dep, fetcherFactory, toFetchRequest} from '../../../../src/ajax.js'; import {config} from 'src/config.js'; import {server} from '../../../mocks/xhr.js'; -import {sandbox} from 'sinon'; +import * as utils from 'src/utils.js'; +import {logError} from 'src/utils.js'; const EXAMPLE_URL = 'https://www.example.com'; @@ -312,13 +313,24 @@ describe('attachCallbacks', () => { const cbType = success ? 'success' : 'error'; describe(`for ${t}`, () => { - let response, body; + let sandbox, response, body; beforeEach(() => { + sandbox = sinon.sandbox.create(); + sandbox.spy(utils, 'logError'); ({response, body} = makeResponse()); }); + afterEach(() => { + sandbox.restore(); + }) + function checkXHR(xhr) { - sinon.assert.match(xhr, { + utils.logError.resetHistory(); + const serialized = JSON.parse(JSON.stringify(xhr)) + // serialization of `responseXML` should not generate console messages + sinon.assert.notCalled(utils.logError); + + sinon.assert.match(serialized, { readyState: XMLHttpRequest.DONE, status: response.status, statusText: response.statusText, @@ -330,7 +342,7 @@ describe('attachCallbacks', () => { if (xml) { expect(xhr.responseXML.querySelectorAll('*').length > 0).to.be.true; } else { - expect(xhr.responseXML).to.not.exist; + expect(serialized.responseXML).to.not.exist; } Array.from(response.headers.entries()).forEach(([name, value]) => { expect(xhr.getResponseHeader(name)).to.eql(value);