mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-07 04:12:04 +00:00
test(e2e): fail clearly on a medications POST body that is not JSON
The route stub parsed the body inside the un-awaited route callback, so a malformed body threw there and the request hung. The parse error is now recorded and the route is fulfilled; reading capture.postBody in the spec throws with the parse error.
This commit is contained in:
1 parent
63882bf3ec
commit
8b18b1c9fe
1 file changed
+19
-2
@@ -30,7 +30,20 @@ interface StubbedMedication {
|
||||
}
|
||||
|
||||
export function mockMedicationsApi(page: Page, stubbed: StubbedMedication) {
|
||||
const capture: { postBody: unknown } = { postBody: null };
|
||||
// A body that is not JSON must fail the spec where it reads `postBody`,
|
||||
// with the parse error, not throw inside the un-awaited route callback.
|
||||
const capture = {
|
||||
rawPostBody: null as unknown,
|
||||
parseError: null as string | null,
|
||||
get postBody(): unknown {
|
||||
if (this.parseError !== null) {
|
||||
throw new Error(
|
||||
`POST /api/medications sent a body that is not JSON: ${this.parseError}`,
|
||||
);
|
||||
}
|
||||
return this.rawPostBody;
|
||||
},
|
||||
};
|
||||
|
||||
const listEntry = {
|
||||
id: stubbed.id,
|
||||
@@ -68,7 +81,11 @@ export function mockMedicationsApi(page: Page, stubbed: StubbedMedication) {
|
||||
return;
|
||||
}
|
||||
if (req.method() === "POST") {
|
||||
capture.postBody = JSON.parse(req.postData() ?? "null");
|
||||
try {
|
||||
capture.rawPostBody = JSON.parse(req.postData() ?? "null");
|
||||
} catch (err) {
|
||||
capture.parseError = err instanceof Error ? err.message : String(err);
|
||||
}
|
||||
await route.fulfill({
|
||||
status: 201,
|
||||
contentType: "application/json",
|
||||
|
||||
Reference in new issue
Block a user