From 4fdffec77b52df6314e1cb0bda7da082ed2498a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Da=CC=81vid=20Istva=CC=81n=20Bi=CC=81ro=CC=81?= Date: Tue, 15 Sep 2026 19:15:53 +0200 Subject: [PATCH] test(xhr): add JSON echo fixture --- examples/runtime/xhr/src/xhr.js | 23 ++++++++++++++++++++++- tests/common/test_server.rs | 8 ++++++++ tests/runtime/xhr.rs | 3 ++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/examples/runtime/xhr/src/xhr.js b/examples/runtime/xhr/src/xhr.js index 16ebacdea..7bcf3364f 100644 --- a/examples/runtime/xhr/src/xhr.js +++ b/examples/runtime/xhr/src/xhr.js @@ -260,7 +260,7 @@ export function postWithFormData(port) { export function postWithJsonBody(port) { console.log('XMLHttpRequest test 12: POST with JSON body'); - return new Promise((resolve) => { + return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); const jsonData = { @@ -271,8 +271,29 @@ export function postWithJsonBody(port) { xhr.onload = function() { console.log(`JSON POST status: ${xhr.status}`); console.log(`JSON response: ${xhr.response}`); + + try { + const response = JSON.parse(xhr.response); + if ( + xhr.status !== 200 || + response.name !== jsonData.name || + response.value !== jsonData.value + ) { + reject(new Error('JSON echo response did not match the request')); + return; + } + } catch (error) { + reject(error); + return; + } + + console.log('JSON response matched request: true'); resolve(); }; + + xhr.onerror = function() { + reject(new Error('JSON echo request failed')); + }; xhr.open('POST', `http://localhost:${port}/json-echo`); xhr.setRequestHeader('Content-Type', 'application/json'); diff --git a/tests/common/test_server.rs b/tests/common/test_server.rs index 0d90865bb..b56c912c0 100644 --- a/tests/common/test_server.rs +++ b/tests/common/test_server.rs @@ -187,6 +187,14 @@ pub async fn start_test_server() -> (u16, TestServerHandle) { })) }), ) + .route( + "/json-echo", + post(async move |Json(body): Json| { + // For accepted JSON requests, extraction drains and validates the body before + // this handler builds the response. + Json(body) + }), + ) .route( "/echo-referer", post(async move |headers: HeaderMap, _body: Bytes| { diff --git a/tests/runtime/xhr.rs b/tests/runtime/xhr.rs index 28c19fa0a..68d35b425 100644 --- a/tests/runtime/xhr.rs +++ b/tests/runtime/xhr.rs @@ -245,7 +245,8 @@ async fn xhr_post_with_json_body( println!("{output}"); assert!(output.contains("XMLHttpRequest test 12: POST with JSON body")); - assert!(output.contains("JSON POST status:") || output.contains("ReadyState changed")); + assert!(output.contains("JSON POST status: 200")); + assert!(output.contains("JSON response matched request: true")); Ok(()) }