desktop: Skip CORS preflights in E2E tests

WebKit sometimes loses a request in the burst the app sends at startup,
while it churns the connections it used for preflights, and the test
fails on an "access control checks" console error. Sending JSON as
`text/plain` makes these simple CORS requests that need no preflight.
This commit is contained in:
Rūdolfs Ošiņš
2026-11-24 17:40:49 +01:00
parent 90166426ef
commit 2ba5230c1b
2 changed files with 20 additions and 1 deletions
+17
View File
@@ -297,6 +297,7 @@ pub fn router(shared: Shared) -> Router {
.route("/notification_count", post(notification_count_handler))
.merge(writes)
.layer(middleware::from_fn(read_body))
.layer(middleware::from_fn(plain_text_is_json))
.layer(
CorsLayer::new()
.allow_origin(cors::AllowOrigin::list(
@@ -321,6 +322,22 @@ async fn read_body(request: Request, next: Next) -> Response {
}
}
/// The app sends JSON as `text/plain` so the browser skips the CORS
/// preflight; `Json` only accepts `application/json`.
async fn plain_text_is_json(mut request: Request, next: Next) -> Response {
let plain = request
.headers()
.get(CONTENT_TYPE)
.and_then(|value| value.to_str().ok())
.is_some_and(|value| value.starts_with("text/plain"));
if plain {
request
.headers_mut()
.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
}
next.run(request).await
}
async fn serialize_writes(
State(lock): State<Arc<Mutex<()>>>,
request: Request,
+3 -1
View File
@@ -97,9 +97,11 @@ async function withTestBackend<T>(
} else {
const raw = args instanceof Uint8Array;
const headers = new Headers(options?.headers);
// `text/plain` keeps JSON calls CORS-simple, so WebKit sends no preflight.
// Its connection churn around preflights loses requests at startup.
headers.set(
"Content-Type",
raw ? "application/octet-stream" : "application/json",
raw ? "application/octet-stream" : "text/plain;charset=UTF-8",
);
return fetch(`http://127.0.0.1:${testHttpApiPort()}/${cmd}`, {
method: "POST",