@schmock/core 2.2.3 → 2.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +35 -0
  3. package/dist/abort.d.ts +3 -0
  4. package/dist/abort.d.ts.map +1 -0
  5. package/dist/abort.js +30 -0
  6. package/dist/binary.d.ts +8 -0
  7. package/dist/binary.d.ts.map +1 -0
  8. package/dist/binary.js +9 -0
  9. package/dist/builder.d.ts +31 -10
  10. package/dist/builder.d.ts.map +1 -1
  11. package/dist/builder.js +804 -203
  12. package/dist/constants.d.ts +38 -0
  13. package/dist/constants.d.ts.map +1 -1
  14. package/dist/constants.js +123 -2
  15. package/dist/errors.d.ts +14 -3
  16. package/dist/errors.d.ts.map +1 -1
  17. package/dist/errors.js +41 -6
  18. package/dist/helpers.d.ts +9 -0
  19. package/dist/helpers.d.ts.map +1 -1
  20. package/dist/helpers.js +18 -2
  21. package/dist/http-helpers.d.ts +52 -5
  22. package/dist/http-helpers.d.ts.map +1 -1
  23. package/dist/http-helpers.js +153 -37
  24. package/dist/index.d.ts +381 -60
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +9 -3
  27. package/dist/interceptor.d.ts +11 -1
  28. package/dist/interceptor.d.ts.map +1 -1
  29. package/dist/interceptor.js +354 -156
  30. package/dist/parser.d.ts.map +1 -1
  31. package/dist/parser.js +12 -3
  32. package/dist/plugin-pipeline.d.ts +11 -4
  33. package/dist/plugin-pipeline.d.ts.map +1 -1
  34. package/dist/plugin-pipeline.js +117 -39
  35. package/dist/response-normalizer.d.ts +16 -0
  36. package/dist/response-normalizer.d.ts.map +1 -0
  37. package/dist/response-normalizer.js +316 -0
  38. package/dist/response-parser.d.ts.map +1 -1
  39. package/dist/response-parser.js +85 -18
  40. package/dist/route-matcher.d.ts +3 -0
  41. package/dist/route-matcher.d.ts.map +1 -1
  42. package/dist/route-matcher.js +12 -6
  43. package/dist/types.d.ts +7 -2
  44. package/dist/types.d.ts.map +1 -1
  45. package/package.json +18 -8
  46. package/src/audit-core-builder.test.ts +0 -218
  47. package/src/audit-response-guard.test.ts +0 -38
  48. package/src/builder.test.ts +0 -289
  49. package/src/builder.ts +0 -701
  50. package/src/constants.test.ts +0 -99
  51. package/src/constants.ts +0 -73
  52. package/src/debug.test.ts +0 -241
  53. package/src/delay.test.ts +0 -319
  54. package/src/dist-shape.test.ts +0 -35
  55. package/src/errors.test.ts +0 -223
  56. package/src/errors.ts +0 -130
  57. package/src/factory.test.ts +0 -133
  58. package/src/helpers.test.ts +0 -147
  59. package/src/helpers.ts +0 -58
  60. package/src/http-helpers.ts +0 -113
  61. package/src/index.ts +0 -151
  62. package/src/interceptor.test.ts +0 -291
  63. package/src/interceptor.ts +0 -320
  64. package/src/namespace.test.ts +0 -274
  65. package/src/parser.property.test.ts +0 -594
  66. package/src/parser.test.ts +0 -148
  67. package/src/parser.ts +0 -64
  68. package/src/plugin-pipeline.ts +0 -103
  69. package/src/plugin-system.test.ts +0 -602
  70. package/src/response-parser.ts +0 -75
  71. package/src/response-parsing.test.ts +0 -333
  72. package/src/route-matcher.ts +0 -69
  73. package/src/route-matching.test.ts +0 -394
  74. package/src/server.test.ts +0 -165
  75. package/src/smart-defaults.test.ts +0 -361
  76. package/src/steps/async-support.steps.ts +0 -400
  77. package/src/steps/audit-core-builder.steps.ts +0 -188
  78. package/src/steps/audit-onerror-tuple.steps.ts +0 -99
  79. package/src/steps/basic-usage.steps.ts +0 -245
  80. package/src/steps/developer-experience.steps.ts +0 -204
  81. package/src/steps/error-handling.steps.ts +0 -345
  82. package/src/steps/fluent-api.steps.ts +0 -255
  83. package/src/steps/http-methods.steps.ts +0 -331
  84. package/src/steps/interceptor.steps.ts +0 -260
  85. package/src/steps/lifecycle-events.steps.ts +0 -142
  86. package/src/steps/performance-reliability.steps.ts +0 -423
  87. package/src/steps/plugin-integration.steps.ts +0 -280
  88. package/src/steps/request-history.steps.ts +0 -276
  89. package/src/steps/response-delay.steps.ts +0 -88
  90. package/src/steps/route-key-format.steps.ts +0 -99
  91. package/src/steps/standalone-server.steps.ts +0 -233
  92. package/src/steps/state-concurrency.steps.ts +0 -739
  93. package/src/steps/stateful-workflows.steps.ts +0 -353
  94. package/src/types.ts +0 -36
@@ -1,594 +0,0 @@
1
- import fc from "fast-check";
2
- import { describe, expect, it } from "vitest";
3
- import { RouteParseError } from "./errors";
4
- import { schmock } from "./index";
5
- import { parseRouteKey } from "./parser";
6
-
7
- const HTTP_METHODS = [
8
- "GET",
9
- "POST",
10
- "PUT",
11
- "DELETE",
12
- "PATCH",
13
- "HEAD",
14
- "OPTIONS",
15
- ] as const;
16
-
17
- const method = fc.constantFrom(...HTTP_METHODS);
18
-
19
- // --- Segment generators, from tame to hostile ---
20
-
21
- const safeSeg = fc.stringMatching(/^[a-z0-9_-]{1,20}$/);
22
- const upperSeg = fc.stringMatching(/^[A-Za-z0-9]{1,20}$/);
23
- // fc.string() in v4 generates full unicode by default
24
- const unicodeSeg = fc
25
- .string({ minLength: 1, maxLength: 20 })
26
- .filter((s) => !s.includes("/") && !s.includes("\0"));
27
- const regexMetaSeg = fc.stringMatching(/^[.*+?^${}()|[\]\\]{1,10}$/);
28
- const encodedSeg = safeSeg.map((s) => encodeURIComponent(s));
29
- const doubleEncodedSeg = safeSeg.map((s) =>
30
- encodeURIComponent(encodeURIComponent(s)),
31
- );
32
- const nullByteSeg = safeSeg.map((s) => `${s}\x00`);
33
- const whitespaceSeg = fc.stringMatching(/^[ \t]{1,5}$/);
34
- const traversalSeg = fc.constantFrom(
35
- "..",
36
- ".",
37
- "%2e%2e",
38
- "%2e",
39
- "..%2f",
40
- "%2f..",
41
- );
42
- const emptySeg = fc.constant("");
43
- const emojiSeg = fc.constantFrom(
44
- "\u{1F600}",
45
- "\u{1F4A9}",
46
- "\u{1F1E7}\u{1F1F7}",
47
- "\u{1F468}\u{200D}\u{1F469}\u{200D}\u{1F467}",
48
- "\u{1F3F3}\u{FE0F}\u{200D}\u{1F308}",
49
- "\u{0041}\u{030A}",
50
- "\u{2764}\u{FE0F}",
51
- "caf\u{00E9}",
52
- "\u{D55C}\u{AD6D}\u{C5B4}",
53
- "\u{0639}\u{0631}\u{0628}\u{064A}",
54
- );
55
- const mixedEmojiSeg = fc.tuple(safeSeg, emojiSeg).map(([a, b]) => `${a}${b}`);
56
-
57
- const hostileSeg = fc.oneof(
58
- { weight: 3, arbitrary: safeSeg },
59
- { weight: 2, arbitrary: unicodeSeg },
60
- { weight: 2, arbitrary: regexMetaSeg },
61
- { weight: 2, arbitrary: emojiSeg },
62
- { weight: 1, arbitrary: mixedEmojiSeg },
63
- { weight: 1, arbitrary: encodedSeg },
64
- { weight: 1, arbitrary: doubleEncodedSeg },
65
- { weight: 1, arbitrary: nullByteSeg },
66
- { weight: 1, arbitrary: whitespaceSeg },
67
- { weight: 1, arbitrary: traversalSeg },
68
- { weight: 1, arbitrary: emptySeg },
69
- { weight: 1, arbitrary: upperSeg },
70
- );
71
-
72
- // --- Path generators ---
73
-
74
- const safePath = fc
75
- .array(safeSeg, { minLength: 1, maxLength: 6 })
76
- .map((segs) => `/${segs.join("/")}`);
77
-
78
- const hostilePath = fc
79
- .array(hostileSeg, { minLength: 1, maxLength: 6 })
80
- .map((segs) => `/${segs.join("/")}`);
81
-
82
- const doubleSlashPath = fc
83
- .array(safeSeg, { minLength: 2, maxLength: 4 })
84
- .map((segs) => `/${segs.join("//")}`);
85
-
86
- const trailingSlashPath = safePath.map((p) => `${p}/`);
87
-
88
- const deepPath = fc
89
- .array(safeSeg, { minLength: 10, maxLength: 30 })
90
- .map((segs) => `/${segs.join("/")}`);
91
-
92
- // --- Route key generators ---
93
-
94
- const routeKey = fc.tuple(method, safePath).map(([m, p]) => `${m} ${p}`);
95
-
96
- const paramRouteKey = fc
97
- .tuple(
98
- method,
99
- fc.array(safeSeg, { minLength: 1, maxLength: 4 }),
100
- fc.array(safeSeg, { minLength: 1, maxLength: 3 }),
101
- )
102
- .map(([m, statics, params]) => {
103
- const parts = statics.map((s, i) =>
104
- i < params.length ? `${s}/:${params[i]}` : s,
105
- );
106
- return `${m} /${parts.join("/")}`;
107
- });
108
-
109
- // Route keys with hostile characters in the path portion
110
- const hostileRouteKey = fc
111
- .tuple(method, hostilePath)
112
- .map(([m, p]) => `${m} ${p}`);
113
-
114
- // --- Arbitrary inputs ---
115
-
116
- const arbitraryString = fc.string({ minLength: 0, maxLength: 200 });
117
-
118
- // --- Namespace generators ---
119
-
120
- const namespace = fc.oneof(
121
- safeSeg.map((s) => `/${s}`),
122
- fc.constant("/"),
123
- safeSeg, // no leading slash
124
- safeSeg.map((s) => `/${s}/`), // trailing slash
125
- fc.constant(""),
126
- safeSeg.map((s) => `//${s}`), // double leading slash
127
- fc.tuple(safeSeg, safeSeg).map(([a, b]) => `/${a}/${b}`), // nested
128
- );
129
-
130
- // ============================================================
131
- // parseRouteKey properties
132
- // ============================================================
133
-
134
- describe("parseRouteKey — fuzz", () => {
135
- it("never throws an unhandled error on arbitrary ASCII input", () => {
136
- fc.assert(
137
- fc.property(arbitraryString, (input) => {
138
- try {
139
- parseRouteKey(input);
140
- } catch (e) {
141
- expect(e).toBeInstanceOf(RouteParseError);
142
- }
143
- }),
144
- { numRuns: 1000 },
145
- );
146
- });
147
-
148
- it("never throws an unhandled error on arbitrary unicode input", () => {
149
- fc.assert(
150
- fc.property(arbitraryString, (input) => {
151
- try {
152
- parseRouteKey(input);
153
- } catch (e) {
154
- expect(e).toBeInstanceOf(RouteParseError);
155
- }
156
- }),
157
- { numRuns: 1000 },
158
- );
159
- });
160
-
161
- it("rejects inputs that look like methods but have hostile paths", () => {
162
- fc.assert(
163
- fc.property(hostileRouteKey, (key) => {
164
- try {
165
- const result = parseRouteKey(key);
166
- // If it parses, the regex must be valid
167
- expect(result.pattern).toBeInstanceOf(RegExp);
168
- } catch (e) {
169
- expect(e).toBeInstanceOf(RouteParseError);
170
- }
171
- }),
172
- { numRuns: 500 },
173
- );
174
- });
175
-
176
- it("always produces a valid regex for well-formed route keys", () => {
177
- fc.assert(
178
- fc.property(routeKey, (key) => {
179
- const result = parseRouteKey(key);
180
- expect(result.pattern).toBeInstanceOf(RegExp);
181
- expect(result.pattern.test(result.path)).toBe(true);
182
- }),
183
- { numRuns: 500 },
184
- );
185
- });
186
-
187
- it("produced regex never matches a completely unrelated path", () => {
188
- fc.assert(
189
- fc.property(routeKey, (key) => {
190
- const result = parseRouteKey(key);
191
- // An empty string should never match a route pattern
192
- expect(result.pattern.test("")).toBe(false);
193
- }),
194
- { numRuns: 500 },
195
- );
196
- });
197
-
198
- it("produced regex is safe against hostile test paths", () => {
199
- fc.assert(
200
- fc.property(routeKey, hostilePath, (key, reqPath) => {
201
- const result = parseRouteKey(key);
202
- const matched = result.pattern.test(reqPath);
203
- expect(typeof matched).toBe("boolean");
204
- }),
205
- { numRuns: 1000 },
206
- );
207
- });
208
-
209
- it("produced regex handles double-slash paths without crashing", () => {
210
- fc.assert(
211
- fc.property(routeKey, doubleSlashPath, (key, reqPath) => {
212
- const result = parseRouteKey(key);
213
- const matched = result.pattern.test(reqPath);
214
- expect(typeof matched).toBe("boolean");
215
- }),
216
- { numRuns: 300 },
217
- );
218
- });
219
-
220
- it("produced regex handles trailing-slash paths without crashing", () => {
221
- fc.assert(
222
- fc.property(routeKey, trailingSlashPath, (key, reqPath) => {
223
- const result = parseRouteKey(key);
224
- const matched = result.pattern.test(reqPath);
225
- expect(typeof matched).toBe("boolean");
226
- }),
227
- { numRuns: 300 },
228
- );
229
- });
230
-
231
- it("parameter count always matches colon segments", () => {
232
- fc.assert(
233
- fc.property(paramRouteKey, (key) => {
234
- const result = parseRouteKey(key);
235
- const colonCount = (key.match(/:/g) || []).length;
236
- expect(result.params).toHaveLength(colonCount);
237
- }),
238
- { numRuns: 500 },
239
- );
240
- });
241
-
242
- it("parameter names never contain slashes", () => {
243
- fc.assert(
244
- fc.property(paramRouteKey, (key) => {
245
- const result = parseRouteKey(key);
246
- for (const param of result.params) {
247
- expect(param).not.toContain("/");
248
- }
249
- }),
250
- { numRuns: 500 },
251
- );
252
- });
253
-
254
- it("pattern is always anchored (^ and $)", () => {
255
- fc.assert(
256
- fc.property(routeKey, (key) => {
257
- const result = parseRouteKey(key);
258
- const src = result.pattern.source;
259
- expect(src.startsWith("^")).toBe(true);
260
- expect(src.endsWith("$")).toBe(true);
261
- }),
262
- { numRuns: 300 },
263
- );
264
- });
265
-
266
- it("method in result always matches method in input", () => {
267
- fc.assert(
268
- fc.property(routeKey, (key) => {
269
- const result = parseRouteKey(key);
270
- const inputMethod = key.split(" ")[0];
271
- expect(result.method).toBe(inputMethod);
272
- }),
273
- { numRuns: 300 },
274
- );
275
- });
276
- });
277
-
278
- // ============================================================
279
- // handle() properties
280
- // ============================================================
281
-
282
- describe("handle() — fuzz", () => {
283
- it("never throws on arbitrary request paths", () => {
284
- const mock = schmock();
285
- mock("GET /users/:id", () => ({ id: 1 }));
286
- mock("POST /items", () => ({ ok: true }));
287
- mock("DELETE /items/:id/comments/:cid", () => ({ deleted: true }));
288
-
289
- fc.assert(
290
- fc.asyncProperty(method, arbitraryString, async (m, reqPath) => {
291
- const response = await mock.handle(m, reqPath);
292
- expect(response).toBeDefined();
293
- expect(typeof response.status).toBe("number");
294
- expect([200, 404, 500]).toContain(response.status);
295
- }),
296
- { numRuns: 1000 },
297
- );
298
- });
299
-
300
- it("never throws on arbitrary unicode request paths", () => {
301
- const mock = schmock();
302
- mock("GET /data/:key", () => ({ ok: true }));
303
-
304
- fc.assert(
305
- fc.asyncProperty(arbitraryString, async (reqPath) => {
306
- const response = await mock.handle("GET", reqPath);
307
- expect(response).toBeDefined();
308
- expect(typeof response.status).toBe("number");
309
- }),
310
- { numRuns: 500 },
311
- );
312
- });
313
-
314
- it("never throws on hostile path segments", () => {
315
- const mock = schmock();
316
- mock("GET /api/:resource/:id", () => ({ found: true }));
317
-
318
- fc.assert(
319
- fc.asyncProperty(hostilePath, async (reqPath) => {
320
- const response = await mock.handle("GET", reqPath);
321
- expect(response).toBeDefined();
322
- expect(typeof response.status).toBe("number");
323
- }),
324
- { numRuns: 500 },
325
- );
326
- });
327
-
328
- it("never throws on double-slash paths", () => {
329
- const mock = schmock();
330
- mock("GET /a/:b", () => ({ ok: true }));
331
-
332
- fc.assert(
333
- fc.asyncProperty(doubleSlashPath, async (reqPath) => {
334
- const response = await mock.handle("GET", reqPath);
335
- expect(response).toBeDefined();
336
- expect(typeof response.status).toBe("number");
337
- }),
338
- { numRuns: 300 },
339
- );
340
- });
341
-
342
- it("never throws on deeply nested paths", () => {
343
- const mock = schmock();
344
- mock("GET /a/:b", () => ({ ok: true }));
345
-
346
- fc.assert(
347
- fc.asyncProperty(deepPath, async (reqPath) => {
348
- const response = await mock.handle("GET", reqPath);
349
- expect(response).toBeDefined();
350
- }),
351
- { numRuns: 200 },
352
- );
353
- });
354
-
355
- it("matched params are always strings", () => {
356
- let capturedParams: Record<string, string> = {};
357
- const mock = schmock();
358
- mock("GET /x/:a/:b", (ctx) => {
359
- capturedParams = ctx.params;
360
- return { ok: true };
361
- });
362
-
363
- fc.assert(
364
- fc.asyncProperty(hostileSeg, hostileSeg, async (a, b) => {
365
- // Avoid slashes in segments since they'd split the path differently
366
- if (a.includes("/") || b.includes("/")) return;
367
- const response = await mock.handle("GET", `/x/${a}/${b}`);
368
- if (response.status === 200) {
369
- expect(typeof capturedParams.a).toBe("string");
370
- expect(typeof capturedParams.b).toBe("string");
371
- expect(capturedParams.a).toBe(a);
372
- expect(capturedParams.b).toBe(b);
373
- }
374
- }),
375
- { numRuns: 500 },
376
- );
377
- });
378
-
379
- it("emoji params are captured verbatim", () => {
380
- let capturedParams: Record<string, string> = {};
381
- const mock = schmock();
382
- mock("GET /emoji/:val", (ctx) => {
383
- capturedParams = ctx.params;
384
- return { ok: true };
385
- });
386
-
387
- fc.assert(
388
- fc.asyncProperty(emojiSeg, async (emoji) => {
389
- const response = await mock.handle("GET", `/emoji/${emoji}`);
390
- expect(response.status).toBe(200);
391
- expect(capturedParams.val).toBe(emoji);
392
- }),
393
- { numRuns: 100 },
394
- );
395
- });
396
-
397
- it("mixed emoji + ASCII paths resolve without error", () => {
398
- const mock = schmock();
399
- mock("GET /search/:query", () => ({ results: [] }));
400
-
401
- fc.assert(
402
- fc.asyncProperty(mixedEmojiSeg, async (seg) => {
403
- const response = await mock.handle("GET", `/search/${seg}`);
404
- expect(response).toBeDefined();
405
- expect(typeof response.status).toBe("number");
406
- }),
407
- { numRuns: 200 },
408
- );
409
- });
410
-
411
- it("namespace stripping handles arbitrary prefixes", () => {
412
- fc.assert(
413
- fc.asyncProperty(namespace, hostilePath, async (ns, reqPath) => {
414
- const mock = schmock({ namespace: ns });
415
- mock("GET /test", () => ({ ok: true }));
416
- const response = await mock.handle("GET", reqPath);
417
- expect(response).toBeDefined();
418
- expect(typeof response.status).toBe("number");
419
- }),
420
- { numRuns: 500 },
421
- );
422
- });
423
-
424
- it("namespace + valid route resolves correctly", () => {
425
- fc.assert(
426
- fc.asyncProperty(safeSeg, async (ns) => {
427
- const mock = schmock({ namespace: `/${ns}` });
428
- mock("GET /ping", () => ({ pong: true }));
429
- const response = await mock.handle("GET", `/${ns}/ping`);
430
- expect(response.status).toBe(200);
431
- }),
432
- { numRuns: 300 },
433
- );
434
- });
435
-
436
- it("unmatched routes always return 404 with ROUTE_NOT_FOUND code", () => {
437
- const mock = schmock();
438
- mock("GET /only-this", () => ({ ok: true }));
439
-
440
- // Generate paths that definitely won't match "/only-this"
441
- const nonMatchingPath = fc
442
- .array(safeSeg, { minLength: 2, maxLength: 5 })
443
- .map((segs) => `/${segs.join("/")}`);
444
-
445
- fc.assert(
446
- fc.asyncProperty(nonMatchingPath, async (reqPath) => {
447
- const response = await mock.handle("GET", reqPath);
448
- expect(response.status).toBe(404);
449
- expect((response.body as any).code).toBe("ROUTE_NOT_FOUND");
450
- }),
451
- { numRuns: 500 },
452
- );
453
- });
454
-
455
- it("regex matching terminates quickly (no ReDoS)", () => {
456
- const mock = schmock();
457
- mock("GET /a/:b/:c/:d/:e", () => ({ ok: true }));
458
-
459
- const longRepeated = fc
460
- .integer({ min: 100, max: 1000 })
461
- .map((n) => `/${"a/".repeat(n)}z`);
462
-
463
- fc.assert(
464
- fc.asyncProperty(longRepeated, async (reqPath) => {
465
- const start = performance.now();
466
- const response = await mock.handle("GET", reqPath);
467
- const elapsed = performance.now() - start;
468
- expect(response).toBeDefined();
469
- expect(elapsed).toBeLessThan(50);
470
- }),
471
- { numRuns: 200 },
472
- );
473
- });
474
-
475
- it("many registered routes still resolve without delay", () => {
476
- const mock = schmock();
477
- // Register 100 routes
478
- for (let i = 0; i < 100; i++) {
479
- mock(`GET /r${i}/:id` as any, () => ({ route: i }));
480
- }
481
-
482
- fc.assert(
483
- fc.asyncProperty(hostilePath, async (reqPath) => {
484
- const start = performance.now();
485
- const response = await mock.handle("GET", reqPath);
486
- const elapsed = performance.now() - start;
487
- expect(response).toBeDefined();
488
- expect(elapsed).toBeLessThan(50);
489
- }),
490
- { numRuns: 300 },
491
- );
492
- });
493
- });
494
-
495
- // ============================================================
496
- // Route matching round-trip properties
497
- // ============================================================
498
-
499
- describe("route matching round-trip — property", () => {
500
- it("parameterized route: arbitrary param values are extracted correctly", () => {
501
- fc.assert(
502
- fc.asyncProperty(
503
- safeSeg,
504
- safeSeg,
505
- safeSeg,
506
- async (resource, paramA, paramB) => {
507
- const mock = schmock();
508
- let capturedParams: Record<string, string> = {};
509
- mock(`GET /${resource}/:a/:b` as any, (ctx) => {
510
- capturedParams = ctx.params;
511
- return { ok: true };
512
- });
513
-
514
- const response = await mock.handle(
515
- "GET",
516
- `/${resource}/${paramA}/${paramB}`,
517
- );
518
- expect(response.status).toBe(200);
519
- expect(capturedParams.a).toBe(paramA);
520
- expect(capturedParams.b).toBe(paramB);
521
- },
522
- ),
523
- { numRuns: 300 },
524
- );
525
- });
526
-
527
- it("any valid response format produces a valid Response with status/body/headers", () => {
528
- const responseGen = fc.oneof(
529
- // Object body
530
- fc.record({ key: safeSeg }),
531
- // Tuple [status, body]
532
- fc.tuple(fc.integer({ min: 100, max: 599 }), fc.record({ v: safeSeg })),
533
- // Tuple [status, body, headers]
534
- fc.tuple(
535
- fc.integer({ min: 100, max: 599 }),
536
- fc.record({ v: safeSeg }),
537
- fc.constant({}),
538
- ),
539
- // null/undefined
540
- fc.constant(null),
541
- fc.constant(undefined),
542
- );
543
-
544
- fc.assert(
545
- fc.asyncProperty(responseGen, async (genValue) => {
546
- const mock = schmock();
547
- mock("GET /test", () => genValue);
548
-
549
- const response = await mock.handle("GET", "/test");
550
- expect(typeof response.status).toBe("number");
551
- expect(response.status).toBeGreaterThanOrEqual(100);
552
- expect(response.status).toBeLessThanOrEqual(599);
553
- expect(response).toHaveProperty("headers");
554
- expect(typeof response.headers).toBe("object");
555
- }),
556
- { numRuns: 300 },
557
- );
558
- });
559
-
560
- it("header keys are always lowercased after extractHeaders in interceptor", () => {
561
- const headerKey = fc
562
- .stringMatching(/^[A-Za-z][A-Za-z0-9-]{0,20}$/)
563
- .filter((k) => k.length > 0);
564
- const headerVal = safeSeg;
565
-
566
- fc.assert(
567
- fc.asyncProperty(headerKey, headerVal, async (key, val) => {
568
- const mock = schmock();
569
- let capturedHeaders: Record<string, string> = {};
570
- mock("GET /hdr", (ctx) => {
571
- capturedHeaders = ctx.headers;
572
- return { ok: true };
573
- });
574
- const handle = mock.intercept();
575
-
576
- try {
577
- await fetch("http://localhost/hdr", {
578
- headers: { [key]: val },
579
- });
580
-
581
- // All header keys should be lowercase
582
- for (const hdrKey of Object.keys(capturedHeaders)) {
583
- expect(hdrKey).toBe(hdrKey.toLowerCase());
584
- }
585
- // The value should be preserved
586
- expect(capturedHeaders[key.toLowerCase()]).toBe(val);
587
- } finally {
588
- handle.restore();
589
- }
590
- }),
591
- { numRuns: 200 },
592
- );
593
- });
594
- });