From b6bcd0e54aac769a9543d65b66143ee9762da2b4 Mon Sep 17 00:00:00 2001 From: Wes Garland Date: Wed, 18 Nov 2026 19:27:08 +0000 Subject: [PATCH] XMLHttpRequest: return the parsed json response on its first read get response() parsed and cached a json response, then fell through to the unsupported-responseType throw; only later reads, served from the cache, returned it. --- .../builtin_modules/XMLHttpRequest.js | 1 + tests/python/test_xhr.py | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/python/pythonmonkey/builtin_modules/XMLHttpRequest.js b/python/pythonmonkey/builtin_modules/XMLHttpRequest.js index 3cbacfc1..50f135fe 100644 --- a/python/pythonmonkey/builtin_modules/XMLHttpRequest.js +++ b/python/pythonmonkey/builtin_modules/XMLHttpRequest.js @@ -643,6 +643,7 @@ class XMLHttpRequest extends XMLHttpRequestEventTarget } // step 8.4 this.#responseObject = jsonObject; + return this.#responseObject; } // step 6 and step 7 ("blob" or "document") are not supported diff --git a/tests/python/test_xhr.py b/tests/python/test_xhr.py index 990193eb..9c810d03 100644 --- a/tests/python/test_xhr.py +++ b/tests/python/test_xhr.py @@ -93,5 +93,37 @@ async def async_fn(): result_json = json.loads(post_result) assert result_json["fromPM"] == "snakesandmonkeys" assert result_json["User-Agent"].startswith("Python/") + + # responseType 'json': the first read of response returns the parsed object, later reads the same one + json_result = await pm.eval(""" + new Promise(function (resolve, reject) + { + let xhr = new XMLHttpRequest(); + xhr.open('POST', 'http://localhost:4001'); + xhr.responseType = 'json'; + + xhr.onload = function () + { + try + { + const first = this.response; + resolve({ first, same: first === this.response }); + } + catch (error) + { + reject(error); + } + }; + + xhr.onerror = function (ev) + { + reject(ev.error); + }; + + xhr.send(JSON.stringify({fromPM: "json"})); + }) + """) + assert json_result["first"]["fromPM"] == "json" + assert json_result["same"] is True httpd.shutdown() asyncio.run(async_fn()) \ No newline at end of file