@schmock/core 1.0.3 → 1.1.0

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 (39) hide show
  1. package/dist/builder.d.ts +13 -5
  2. package/dist/builder.d.ts.map +1 -1
  3. package/dist/builder.js +147 -60
  4. package/dist/constants.d.ts +6 -0
  5. package/dist/constants.d.ts.map +1 -0
  6. package/dist/constants.js +20 -0
  7. package/dist/errors.d.ts.map +1 -1
  8. package/dist/errors.js +3 -1
  9. package/dist/index.d.ts +3 -3
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +20 -11
  12. package/dist/parser.d.ts.map +1 -1
  13. package/dist/parser.js +2 -17
  14. package/dist/types.d.ts +17 -210
  15. package/dist/types.d.ts.map +1 -1
  16. package/dist/types.js +1 -0
  17. package/package.json +4 -4
  18. package/src/builder.test.ts +2 -2
  19. package/src/builder.ts +232 -108
  20. package/src/constants.test.ts +59 -0
  21. package/src/constants.ts +25 -0
  22. package/src/errors.ts +3 -1
  23. package/src/index.ts +41 -29
  24. package/src/namespace.test.ts +3 -2
  25. package/src/parser.property.test.ts +495 -0
  26. package/src/parser.ts +2 -20
  27. package/src/route-matching.test.ts +1 -1
  28. package/src/steps/async-support.steps.ts +101 -91
  29. package/src/steps/basic-usage.steps.ts +49 -36
  30. package/src/steps/developer-experience.steps.ts +110 -94
  31. package/src/steps/error-handling.steps.ts +90 -66
  32. package/src/steps/fluent-api.steps.ts +75 -72
  33. package/src/steps/http-methods.steps.ts +33 -33
  34. package/src/steps/performance-reliability.steps.ts +52 -88
  35. package/src/steps/plugin-integration.steps.ts +176 -176
  36. package/src/steps/request-history.steps.ts +333 -0
  37. package/src/steps/state-concurrency.steps.ts +418 -316
  38. package/src/steps/stateful-workflows.steps.ts +138 -136
  39. package/src/types.ts +20 -259
package/src/index.ts CHANGED
@@ -1,12 +1,4 @@
1
- import { CallableMockInstance as CallableMockInstanceImpl } from "./builder.js";
2
- import type {
3
- CallableMockInstance,
4
- Generator,
5
- GlobalConfig,
6
- Plugin,
7
- RouteConfig,
8
- RouteKey,
9
- } from "./types.js";
1
+ import { CallableMockInstance } from "./builder.js";
10
2
 
11
3
  /**
12
4
  * Create a new Schmock mock instance with callable API.
@@ -31,30 +23,48 @@ import type {
31
23
  * @param config Optional global configuration
32
24
  * @returns A callable mock instance
33
25
  */
34
- export function schmock(config?: GlobalConfig): CallableMockInstance {
26
+ export function schmock(
27
+ config?: Schmock.GlobalConfig,
28
+ ): Schmock.CallableMockInstance {
35
29
  // Always use new callable API
36
- const instance = new CallableMockInstanceImpl(config || {});
37
-
38
- // Create a callable function that wraps the instance
39
- const callableInstance = ((
40
- route: RouteKey,
41
- generator: Generator,
42
- routeConfig: RouteConfig = {},
43
- ) => {
44
- instance.defineRoute(route, generator, routeConfig);
45
- return callableInstance; // Return the callable function for chaining
46
- }) as any;
30
+ const instance = new CallableMockInstance(config || {});
47
31
 
48
- // Manually bind all instance methods to the callable function with proper return values
49
- callableInstance.pipe = (plugin: Plugin) => {
50
- instance.pipe(plugin);
51
- return callableInstance; // Return callable function for chaining
52
- };
53
- callableInstance.handle = instance.handle.bind(instance);
32
+ // Callable proxy: a function with attached methods
33
+ const callableInstance: Schmock.CallableMockInstance = Object.assign(
34
+ (
35
+ route: Schmock.RouteKey,
36
+ generator: Schmock.Generator,
37
+ routeConfig: Schmock.RouteConfig = {},
38
+ ) => {
39
+ instance.defineRoute(route, generator, routeConfig);
40
+ return callableInstance;
41
+ },
42
+ {
43
+ pipe: (plugin: Schmock.Plugin) => {
44
+ instance.pipe(plugin);
45
+ return callableInstance;
46
+ },
47
+ handle: instance.handle.bind(instance),
48
+ history: instance.history.bind(instance),
49
+ called: instance.called.bind(instance),
50
+ callCount: instance.callCount.bind(instance),
51
+ lastRequest: instance.lastRequest.bind(instance),
52
+ reset: instance.reset.bind(instance),
53
+ resetHistory: instance.resetHistory.bind(instance),
54
+ resetState: instance.resetState.bind(instance),
55
+ },
56
+ );
54
57
 
55
- return callableInstance as CallableMockInstance;
58
+ return callableInstance;
56
59
  }
57
60
 
61
+ // Re-export constants and utilities
62
+ export {
63
+ HTTP_METHODS,
64
+ isHttpMethod,
65
+ ROUTE_NOT_FOUND_CODE,
66
+ toHttpMethod,
67
+ } from "./constants.js";
58
68
  // Re-export errors
59
69
  export {
60
70
  PluginError,
@@ -67,7 +77,6 @@ export {
67
77
  SchemaValidationError,
68
78
  SchmockError,
69
79
  } from "./errors.js";
70
-
71
80
  // Re-export types
72
81
  export type {
73
82
  CallableMockInstance,
@@ -80,8 +89,11 @@ export type {
80
89
  PluginResult,
81
90
  RequestContext,
82
91
  RequestOptions,
92
+ RequestRecord,
83
93
  Response,
94
+ ResponseBody,
84
95
  ResponseResult,
85
96
  RouteConfig,
86
97
  RouteKey,
98
+ StaticData,
87
99
  } from "./types.js";
@@ -95,8 +95,9 @@ describe("namespace functionality", () => {
95
95
  const response2 = await mock.handle("GET", "/api//users");
96
96
 
97
97
  expect(response1.body).toBe("users");
98
- // This might not match depending on implementation
99
- expect(response2.status).toBe(404);
98
+ // New logic gracefully handles double slashes by stripping the full namespace
99
+ expect(response2.status).toBe(200);
100
+ expect(response2.body).toBe("users");
100
101
  });
101
102
 
102
103
  it("handles empty namespace", async () => {
@@ -0,0 +1,495 @@
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
128
+ .tuple(safeSeg, safeSeg)
129
+ .map(([a, b]) => `/${a}/${b}`), // nested
130
+ );
131
+
132
+ // ============================================================
133
+ // parseRouteKey properties
134
+ // ============================================================
135
+
136
+ describe("parseRouteKey — fuzz", () => {
137
+ it("never throws an unhandled error on arbitrary ASCII input", () => {
138
+ fc.assert(
139
+ fc.property(arbitraryString, (input) => {
140
+ try {
141
+ parseRouteKey(input);
142
+ } catch (e) {
143
+ expect(e).toBeInstanceOf(RouteParseError);
144
+ }
145
+ }),
146
+ { numRuns: 1000 },
147
+ );
148
+ });
149
+
150
+ it("never throws an unhandled error on arbitrary unicode input", () => {
151
+ fc.assert(
152
+ fc.property(arbitraryString, (input) => {
153
+ try {
154
+ parseRouteKey(input);
155
+ } catch (e) {
156
+ expect(e).toBeInstanceOf(RouteParseError);
157
+ }
158
+ }),
159
+ { numRuns: 1000 },
160
+ );
161
+ });
162
+
163
+ it("rejects inputs that look like methods but have hostile paths", () => {
164
+ fc.assert(
165
+ fc.property(hostileRouteKey, (key) => {
166
+ try {
167
+ const result = parseRouteKey(key);
168
+ // If it parses, the regex must be valid
169
+ expect(result.pattern).toBeInstanceOf(RegExp);
170
+ } catch (e) {
171
+ expect(e).toBeInstanceOf(RouteParseError);
172
+ }
173
+ }),
174
+ { numRuns: 500 },
175
+ );
176
+ });
177
+
178
+ it("always produces a valid regex for well-formed route keys", () => {
179
+ fc.assert(
180
+ fc.property(routeKey, (key) => {
181
+ const result = parseRouteKey(key);
182
+ expect(result.pattern).toBeInstanceOf(RegExp);
183
+ expect(result.pattern.test(result.path)).toBe(true);
184
+ }),
185
+ { numRuns: 500 },
186
+ );
187
+ });
188
+
189
+ it("produced regex never matches a completely unrelated path", () => {
190
+ fc.assert(
191
+ fc.property(routeKey, (key) => {
192
+ const result = parseRouteKey(key);
193
+ // An empty string should never match a route pattern
194
+ expect(result.pattern.test("")).toBe(false);
195
+ }),
196
+ { numRuns: 500 },
197
+ );
198
+ });
199
+
200
+ it("produced regex is safe against hostile test paths", () => {
201
+ fc.assert(
202
+ fc.property(routeKey, hostilePath, (key, reqPath) => {
203
+ const result = parseRouteKey(key);
204
+ const matched = result.pattern.test(reqPath);
205
+ expect(typeof matched).toBe("boolean");
206
+ }),
207
+ { numRuns: 1000 },
208
+ );
209
+ });
210
+
211
+ it("produced regex handles double-slash paths without crashing", () => {
212
+ fc.assert(
213
+ fc.property(routeKey, doubleSlashPath, (key, reqPath) => {
214
+ const result = parseRouteKey(key);
215
+ const matched = result.pattern.test(reqPath);
216
+ expect(typeof matched).toBe("boolean");
217
+ }),
218
+ { numRuns: 300 },
219
+ );
220
+ });
221
+
222
+ it("produced regex handles trailing-slash paths without crashing", () => {
223
+ fc.assert(
224
+ fc.property(routeKey, trailingSlashPath, (key, reqPath) => {
225
+ const result = parseRouteKey(key);
226
+ const matched = result.pattern.test(reqPath);
227
+ expect(typeof matched).toBe("boolean");
228
+ }),
229
+ { numRuns: 300 },
230
+ );
231
+ });
232
+
233
+ it("parameter count always matches colon segments", () => {
234
+ fc.assert(
235
+ fc.property(paramRouteKey, (key) => {
236
+ const result = parseRouteKey(key);
237
+ const colonCount = (key.match(/:/g) || []).length;
238
+ expect(result.params).toHaveLength(colonCount);
239
+ }),
240
+ { numRuns: 500 },
241
+ );
242
+ });
243
+
244
+ it("parameter names never contain slashes", () => {
245
+ fc.assert(
246
+ fc.property(paramRouteKey, (key) => {
247
+ const result = parseRouteKey(key);
248
+ for (const param of result.params) {
249
+ expect(param).not.toContain("/");
250
+ }
251
+ }),
252
+ { numRuns: 500 },
253
+ );
254
+ });
255
+
256
+ it("pattern is always anchored (^ and $)", () => {
257
+ fc.assert(
258
+ fc.property(routeKey, (key) => {
259
+ const result = parseRouteKey(key);
260
+ const src = result.pattern.source;
261
+ expect(src.startsWith("^")).toBe(true);
262
+ expect(src.endsWith("$")).toBe(true);
263
+ }),
264
+ { numRuns: 300 },
265
+ );
266
+ });
267
+
268
+ it("method in result always matches method in input", () => {
269
+ fc.assert(
270
+ fc.property(routeKey, (key) => {
271
+ const result = parseRouteKey(key);
272
+ const inputMethod = key.split(" ")[0];
273
+ expect(result.method).toBe(inputMethod);
274
+ }),
275
+ { numRuns: 300 },
276
+ );
277
+ });
278
+ });
279
+
280
+ // ============================================================
281
+ // handle() properties
282
+ // ============================================================
283
+
284
+ describe("handle() — fuzz", () => {
285
+ it("never throws on arbitrary request paths", () => {
286
+ const mock = schmock();
287
+ mock("GET /users/:id", () => ({ id: 1 }));
288
+ mock("POST /items", () => ({ ok: true }));
289
+ mock("DELETE /items/:id/comments/:cid", () => ({ deleted: true }));
290
+
291
+ fc.assert(
292
+ fc.asyncProperty(method, arbitraryString, async (m, reqPath) => {
293
+ const response = await mock.handle(m, reqPath);
294
+ expect(response).toBeDefined();
295
+ expect(typeof response.status).toBe("number");
296
+ expect([200, 404, 500]).toContain(response.status);
297
+ }),
298
+ { numRuns: 1000 },
299
+ );
300
+ });
301
+
302
+ it("never throws on arbitrary unicode request paths", () => {
303
+ const mock = schmock();
304
+ mock("GET /data/:key", () => ({ ok: true }));
305
+
306
+ fc.assert(
307
+ fc.asyncProperty(arbitraryString, async (reqPath) => {
308
+ const response = await mock.handle("GET", reqPath);
309
+ expect(response).toBeDefined();
310
+ expect(typeof response.status).toBe("number");
311
+ }),
312
+ { numRuns: 500 },
313
+ );
314
+ });
315
+
316
+ it("never throws on hostile path segments", () => {
317
+ const mock = schmock();
318
+ mock("GET /api/:resource/:id", () => ({ found: true }));
319
+
320
+ fc.assert(
321
+ fc.asyncProperty(hostilePath, async (reqPath) => {
322
+ const response = await mock.handle("GET", reqPath);
323
+ expect(response).toBeDefined();
324
+ expect(typeof response.status).toBe("number");
325
+ }),
326
+ { numRuns: 500 },
327
+ );
328
+ });
329
+
330
+ it("never throws on double-slash paths", () => {
331
+ const mock = schmock();
332
+ mock("GET /a/:b", () => ({ ok: true }));
333
+
334
+ fc.assert(
335
+ fc.asyncProperty(doubleSlashPath, async (reqPath) => {
336
+ const response = await mock.handle("GET", reqPath);
337
+ expect(response).toBeDefined();
338
+ expect(typeof response.status).toBe("number");
339
+ }),
340
+ { numRuns: 300 },
341
+ );
342
+ });
343
+
344
+ it("never throws on deeply nested paths", () => {
345
+ const mock = schmock();
346
+ mock("GET /a/:b", () => ({ ok: true }));
347
+
348
+ fc.assert(
349
+ fc.asyncProperty(deepPath, async (reqPath) => {
350
+ const response = await mock.handle("GET", reqPath);
351
+ expect(response).toBeDefined();
352
+ }),
353
+ { numRuns: 200 },
354
+ );
355
+ });
356
+
357
+ it("matched params are always strings", () => {
358
+ let capturedParams: Record<string, string> = {};
359
+ const mock = schmock();
360
+ mock("GET /x/:a/:b", (ctx) => {
361
+ capturedParams = ctx.params;
362
+ return { ok: true };
363
+ });
364
+
365
+ fc.assert(
366
+ fc.asyncProperty(hostileSeg, hostileSeg, async (a, b) => {
367
+ // Avoid slashes in segments since they'd split the path differently
368
+ if (a.includes("/") || b.includes("/")) return;
369
+ const response = await mock.handle("GET", `/x/${a}/${b}`);
370
+ if (response.status === 200) {
371
+ expect(typeof capturedParams.a).toBe("string");
372
+ expect(typeof capturedParams.b).toBe("string");
373
+ expect(capturedParams.a).toBe(a);
374
+ expect(capturedParams.b).toBe(b);
375
+ }
376
+ }),
377
+ { numRuns: 500 },
378
+ );
379
+ });
380
+
381
+ it("emoji params are captured verbatim", () => {
382
+ let capturedParams: Record<string, string> = {};
383
+ const mock = schmock();
384
+ mock("GET /emoji/:val", (ctx) => {
385
+ capturedParams = ctx.params;
386
+ return { ok: true };
387
+ });
388
+
389
+ fc.assert(
390
+ fc.asyncProperty(emojiSeg, async (emoji) => {
391
+ const response = await mock.handle("GET", `/emoji/${emoji}`);
392
+ expect(response.status).toBe(200);
393
+ expect(capturedParams.val).toBe(emoji);
394
+ }),
395
+ { numRuns: 100 },
396
+ );
397
+ });
398
+
399
+ it("mixed emoji + ASCII paths resolve without error", () => {
400
+ const mock = schmock();
401
+ mock("GET /search/:query", () => ({ results: [] }));
402
+
403
+ fc.assert(
404
+ fc.asyncProperty(mixedEmojiSeg, async (seg) => {
405
+ const response = await mock.handle("GET", `/search/${seg}`);
406
+ expect(response).toBeDefined();
407
+ expect(typeof response.status).toBe("number");
408
+ }),
409
+ { numRuns: 200 },
410
+ );
411
+ });
412
+
413
+ it("namespace stripping handles arbitrary prefixes", () => {
414
+ fc.assert(
415
+ fc.asyncProperty(namespace, hostilePath, async (ns, reqPath) => {
416
+ const mock = schmock({ namespace: ns });
417
+ mock("GET /test", () => ({ ok: true }));
418
+ const response = await mock.handle("GET", reqPath);
419
+ expect(response).toBeDefined();
420
+ expect(typeof response.status).toBe("number");
421
+ }),
422
+ { numRuns: 500 },
423
+ );
424
+ });
425
+
426
+ it("namespace + valid route resolves correctly", () => {
427
+ fc.assert(
428
+ fc.asyncProperty(safeSeg, async (ns) => {
429
+ const mock = schmock({ namespace: `/${ns}` });
430
+ mock("GET /ping", () => ({ pong: true }));
431
+ const response = await mock.handle("GET", `/${ns}/ping`);
432
+ expect(response.status).toBe(200);
433
+ }),
434
+ { numRuns: 300 },
435
+ );
436
+ });
437
+
438
+ it("unmatched routes always return 404 with ROUTE_NOT_FOUND code", () => {
439
+ const mock = schmock();
440
+ mock("GET /only-this", () => ({ ok: true }));
441
+
442
+ // Generate paths that definitely won't match "/only-this"
443
+ const nonMatchingPath = fc
444
+ .array(safeSeg, { minLength: 2, maxLength: 5 })
445
+ .map((segs) => `/${segs.join("/")}`);
446
+
447
+ fc.assert(
448
+ fc.asyncProperty(nonMatchingPath, async (reqPath) => {
449
+ const response = await mock.handle("GET", reqPath);
450
+ expect(response.status).toBe(404);
451
+ expect((response.body as any).code).toBe("ROUTE_NOT_FOUND");
452
+ }),
453
+ { numRuns: 500 },
454
+ );
455
+ });
456
+
457
+ it("regex matching terminates quickly (no ReDoS)", () => {
458
+ const mock = schmock();
459
+ mock("GET /a/:b/:c/:d/:e", () => ({ ok: true }));
460
+
461
+ const longRepeated = fc
462
+ .integer({ min: 100, max: 1000 })
463
+ .map((n) => `/${"a/".repeat(n)}z`);
464
+
465
+ fc.assert(
466
+ fc.asyncProperty(longRepeated, async (reqPath) => {
467
+ const start = performance.now();
468
+ const response = await mock.handle("GET", reqPath);
469
+ const elapsed = performance.now() - start;
470
+ expect(response).toBeDefined();
471
+ expect(elapsed).toBeLessThan(50);
472
+ }),
473
+ { numRuns: 200 },
474
+ );
475
+ });
476
+
477
+ it("many registered routes still resolve without delay", () => {
478
+ const mock = schmock();
479
+ // Register 100 routes
480
+ for (let i = 0; i < 100; i++) {
481
+ mock(`GET /r${i}/:id` as any, () => ({ route: i }));
482
+ }
483
+
484
+ fc.assert(
485
+ fc.asyncProperty(hostilePath, async (reqPath) => {
486
+ const start = performance.now();
487
+ const response = await mock.handle("GET", reqPath);
488
+ const elapsed = performance.now() - start;
489
+ expect(response).toBeDefined();
490
+ expect(elapsed).toBeLessThan(50);
491
+ }),
492
+ { numRuns: 300 },
493
+ );
494
+ });
495
+ });
package/src/parser.ts CHANGED
@@ -1,20 +1,7 @@
1
+ import { toHttpMethod } from "./constants.js";
1
2
  import { RouteParseError } from "./errors.js";
2
3
  import type { HttpMethod } from "./types.js";
3
4
 
4
- const HTTP_METHODS: readonly HttpMethod[] = [
5
- "GET",
6
- "POST",
7
- "PUT",
8
- "DELETE",
9
- "PATCH",
10
- "HEAD",
11
- "OPTIONS",
12
- ];
13
-
14
- function isHttpMethod(method: string): method is HttpMethod {
15
- return HTTP_METHODS.includes(method as HttpMethod);
16
- }
17
-
18
5
  export interface ParsedRoute {
19
6
  method: HttpMethod;
20
7
  path: string;
@@ -66,13 +53,8 @@ export function parseRouteKey(routeKey: string): ParsedRoute {
66
53
 
67
54
  const pattern = new RegExp(`^${regexPath}$`);
68
55
 
69
- // The regex guarantees method is valid, but we use the type guard for type safety
70
- if (!isHttpMethod(method)) {
71
- throw new RouteParseError(routeKey, `Invalid HTTP method: ${method}`);
72
- }
73
-
74
56
  return {
75
- method,
57
+ method: toHttpMethod(method),
76
58
  path,
77
59
  pattern,
78
60
  params,
@@ -311,7 +311,7 @@ describe("route matching", () => {
311
311
  mock("GET /items/:id", ({ params }) => ({
312
312
  id: params.id,
313
313
  type: typeof params.id,
314
- parsed: Number.parseInt(params.id),
314
+ parsed: Number.parseInt(params.id, 10),
315
315
  }));
316
316
 
317
317
  const response = await mock.handle("GET", "/items/12345");