-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathresponse.property.test.ts
More file actions
84 lines (72 loc) · 2.81 KB
/
response.property.test.ts
File metadata and controls
84 lines (72 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
/**
* Property-based tests for JSON response format.
*
* Verifies that for any HTTP request to any booking API path, the response
* always has `Content-Type: application/json` and a valid JSON body.
*
* Since DB-dependent handlers will fail at connection time, they return
* error JSON (503/500). The routing layer catches all errors and returns
* JSON. This test verifies that invariant holds across all paths and methods.
*
* @module response.property.test
*/
import fc from "fast-check";
import { assertEquals } from "@std/assert";
import { handleRequest, type AppContext } from "./handlers.ts";
import { makeMockAppContext } from "./test-mocks.ts";
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
const CTX: AppContext = makeMockAppContext();
const ALL_METHODS = ["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"];
/**
* Generates random URL paths — both valid booking paths and random strings.
*/
const pathArb = fc.oneof(
fc.constant("/bookings"),
fc.constant("/bookings/test-id-123"),
fc.constant("/"),
fc.constant("/unknown"),
fc.string({ minLength: 1, maxLength: 40 })
.map((s: string) => "/" + s.replace(/[^a-z0-9\-_/]/gi, "x")),
);
// ---------------------------------------------------------------------------
// Property 3: JSON response format
// ---------------------------------------------------------------------------
/**
* Feature: deno-aurora-dsql-samples, Property 3: JSON response format
*
* For any HTTP request to any booking API endpoint, the response has
* Content-Type: application/json and a valid JSON body.
*
* **Validates: Requirements 7.5**
*/
Deno.test("property: all responses have application/json content-type and valid JSON body", async () => {
await fc.assert(
fc.asyncProperty(
fc.constantFrom(...ALL_METHODS),
pathArb,
async (method, path) => {
const opts: RequestInit = { method };
// POST and PUT need a body to avoid hanging on req.json()
if (method === "POST" || method === "PUT") {
opts.body = JSON.stringify({ resource_name: "test" });
opts.headers = { "Content-Type": "application/json" };
}
const req = new Request(`http://localhost:8000${path}`, opts);
const res = await handleRequest(req, CTX);
// Content-Type must be application/json
assertEquals(
res.headers.get("Content-Type"),
"application/json",
);
// Body must be valid JSON (JSON.parse should not throw)
const text = await res.text();
JSON.parse(text); // throws if invalid JSON
},
),
{ numRuns: 100 },
);
});