@zimic/interceptor 1.1.4-canary.1 → 1.1.5-canary.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.
package/dist/http.mjs CHANGED
@@ -614,23 +614,30 @@ var HttpRequestHandlerClient = class {
614
614
  return this;
615
615
  }
616
616
  async matchesRequest(request) {
617
- const hasDeclaredResponse = this.createResponseDeclaration !== void 0;
618
- const restrictionsMatch = await this.matchesRequestRestrictions(request);
619
- if (restrictionsMatch.success) {
620
- this.numberOfMatchedRequests++;
621
- } else {
622
- const shouldSaveUnmatchedGroup = this.interceptor.requestSaving.enabled && this.restrictions.length > 0 && this.timesDeclarationPointer !== void 0;
623
- if (shouldSaveUnmatchedGroup) {
624
- this.unmatchedRequestGroups.push({ request, diff: restrictionsMatch.diff });
625
- }
617
+ const restrictionsMatch = await this.matchesRestrictions(request);
618
+ if (!restrictionsMatch.success) {
619
+ return { success: false, cause: "unmatchedRestrictions", diff: restrictionsMatch.diff };
626
620
  }
627
- if (!hasDeclaredResponse) {
628
- return false;
621
+ const hasResponseDeclaration = this.createResponseDeclaration !== void 0;
622
+ if (!hasResponseDeclaration) {
623
+ return { success: false, cause: "missingResponseDeclaration" };
629
624
  }
630
- const isWithinLimits = this.numberOfMatchedRequests <= this.limits.numberOfRequests.max;
631
- return restrictionsMatch.success && isWithinLimits;
625
+ const canAcceptMoreRequests = this.numberOfMatchedRequests < this.limits.numberOfRequests.max;
626
+ if (!canAcceptMoreRequests) {
627
+ return { success: false, cause: "exceededNumberOfRequests" };
628
+ }
629
+ return { success: true };
632
630
  }
633
- async matchesRequestRestrictions(request) {
631
+ markRequestAsMatched(_request) {
632
+ this.numberOfMatchedRequests++;
633
+ }
634
+ markRequestAsUnmatched(request, options) {
635
+ const shouldSaveUnmatchedRequests = this.interceptor.requestSaving.enabled && this.restrictions.length > 0 && this.timesDeclarationPointer !== void 0;
636
+ if (shouldSaveUnmatchedRequests) {
637
+ this.unmatchedRequestGroups.push({ request, diff: options.diff });
638
+ }
639
+ }
640
+ async matchesRestrictions(request) {
634
641
  for (const restriction of this.restrictions) {
635
642
  if (this.isComputedRequestRestriction(restriction)) {
636
643
  const matchesComputedRestriction = await restriction(request);
@@ -828,8 +835,16 @@ var LocalHttpRequestHandler = class {
828
835
  get requests() {
829
836
  return this.client.requests;
830
837
  }
831
- matchesRequest(request) {
832
- return this.client.matchesRequest(request);
838
+ async matchesRequest(request) {
839
+ const requestMatch = await this.client.matchesRequest(request);
840
+ if (requestMatch.success) {
841
+ this.client.markRequestAsMatched(request);
842
+ } else if (requestMatch.cause === "unmatchedRestrictions") {
843
+ this.client.markRequestAsUnmatched(request, { diff: requestMatch.diff });
844
+ } else {
845
+ this.client.markRequestAsMatched(request);
846
+ }
847
+ return requestMatch;
833
848
  }
834
849
  async applyResponseDeclaration(request) {
835
850
  return this.client.applyResponseDeclaration(request);
@@ -1312,8 +1327,16 @@ var RemoteHttpRequestHandler = class {
1312
1327
  get requests() {
1313
1328
  return this.client.requests;
1314
1329
  }
1315
- matchesRequest(request) {
1316
- return this.client.matchesRequest(request);
1330
+ async matchesRequest(request) {
1331
+ const requestMatch = await this.client.matchesRequest(request);
1332
+ if (requestMatch.success) {
1333
+ this.client.markRequestAsMatched(request);
1334
+ } else if (requestMatch.cause === "unmatchedRestrictions") {
1335
+ this.client.markRequestAsUnmatched(request, { diff: requestMatch.diff });
1336
+ } else {
1337
+ this.client.markRequestAsMatched(request);
1338
+ }
1339
+ return requestMatch;
1317
1340
  }
1318
1341
  async applyResponseDeclaration(request) {
1319
1342
  return this.client.applyResponseDeclaration(request);
@@ -1357,7 +1380,7 @@ var HttpInterceptorClient = class {
1357
1380
  onUnhandledRequest;
1358
1381
  isRunning = false;
1359
1382
  Handler;
1360
- handlerClientsByMethod = {
1383
+ handlers = {
1361
1384
  GET: /* @__PURE__ */ new Map(),
1362
1385
  POST: /* @__PURE__ */ new Map(),
1363
1386
  PATCH: /* @__PURE__ */ new Map(),
@@ -1478,17 +1501,17 @@ var HttpInterceptorClient = class {
1478
1501
  return handler;
1479
1502
  }
1480
1503
  registerRequestHandler(handler) {
1481
- const handlerClients = this.handlerClientsByMethod[handler.method].get(handler.path) ?? [];
1482
- const isAlreadyRegistered = handlerClients.includes(handler.client);
1504
+ const pathHandlers = this.handlers[handler.method].get(handler.path) ?? [];
1505
+ const isAlreadyRegistered = pathHandlers.includes(handler.client);
1483
1506
  if (isAlreadyRegistered) {
1484
1507
  return;
1485
1508
  }
1486
- handlerClients.push(handler.client);
1487
- const isFirstHandlerForMethodPath = handlerClients.length === 1;
1509
+ pathHandlers.push(handler.client);
1510
+ const isFirstHandlerForMethodPath = pathHandlers.length === 1;
1488
1511
  if (!isFirstHandlerForMethodPath) {
1489
1512
  return;
1490
1513
  }
1491
- this.handlerClientsByMethod[handler.method].set(handler.path, handlerClients);
1514
+ this.handlers[handler.method].set(handler.path, pathHandlers);
1492
1515
  const pathRegex = createRegexFromPath_default(handler.path);
1493
1516
  const registrationResult = this.workerOrThrow.use(this, handler.method, handler.path, async (context) => {
1494
1517
  const response = await this.handleInterceptedRequest(
@@ -1529,21 +1552,36 @@ var HttpInterceptorClient = class {
1529
1552
  console.warn(error);
1530
1553
  }
1531
1554
  }
1532
- async findMatchedHandler(method, path, parsedRequest) {
1533
- const handlersByPath = this.handlerClientsByMethod[method].get(path) ?? [];
1534
- for (let handlerIndex = handlersByPath.length - 1; handlerIndex >= 0; handlerIndex--) {
1535
- const handler = handlersByPath[handlerIndex];
1536
- if (await handler.matchesRequest(parsedRequest)) {
1555
+ async findMatchedHandler(method, path, request) {
1556
+ const pathHandlers = this.handlers[method].get(path) ?? [];
1557
+ const failedRequestMatches = /* @__PURE__ */ new Map();
1558
+ for (let handlerIndex = pathHandlers.length - 1; handlerIndex >= 0; handlerIndex--) {
1559
+ const handler = pathHandlers[handlerIndex];
1560
+ const requestMatch = await handler.matchesRequest(request);
1561
+ if (requestMatch.success) {
1562
+ handler.markRequestAsMatched(request);
1537
1563
  return handler;
1538
1564
  }
1565
+ failedRequestMatches.set(handler, requestMatch);
1566
+ }
1567
+ for (let handlerIndex = pathHandlers.length - 1; handlerIndex >= 0; handlerIndex--) {
1568
+ const handler = pathHandlers[handlerIndex];
1569
+ const requestMatch = failedRequestMatches.get(handler);
1570
+ if (requestMatch?.cause === "unmatchedRestrictions") {
1571
+ handler.markRequestAsUnmatched(request, { diff: requestMatch.diff });
1572
+ } else {
1573
+ handler.markRequestAsMatched(request);
1574
+ break;
1575
+ }
1539
1576
  }
1540
1577
  return void 0;
1541
1578
  }
1542
1579
  checkTimes() {
1543
1580
  for (const method of HTTP_METHODS) {
1544
- const handlersByPath = this.handlerClientsByMethod[method];
1545
- for (const handlers of handlersByPath.values()) {
1546
- for (const handler of handlers) {
1581
+ const pathHandlers = this.handlers[method];
1582
+ for (const handlers of pathHandlers.values()) {
1583
+ for (let handlerIndex = handlers.length - 1; handlerIndex >= 0; handlerIndex--) {
1584
+ const handler = handlers[handlerIndex];
1547
1585
  handler.checkTimes();
1548
1586
  }
1549
1587
  }
@@ -1558,17 +1596,17 @@ var HttpInterceptorClient = class {
1558
1596
  for (const result of newClearResults) {
1559
1597
  clearResults.push(Promise.resolve(result));
1560
1598
  }
1561
- const handlersByPath = this.handlerClientsByMethod[method];
1562
- handlersByPath.clear();
1599
+ const pathHandlers = this.handlers[method];
1600
+ pathHandlers.clear();
1563
1601
  }
1564
1602
  if (options.onCommitSuccess) {
1565
1603
  void Promise.all(clearResults).then(options.onCommitSuccess, options.onCommitError);
1566
1604
  }
1567
1605
  }
1568
1606
  clearMethodHandlers(method) {
1569
- const handlersByPath = this.handlerClientsByMethod[method];
1607
+ const pathHandlers = this.handlers[method];
1570
1608
  const clearResults = [];
1571
- for (const handlers of handlersByPath.values()) {
1609
+ for (const handlers of pathHandlers.values()) {
1572
1610
  for (const handler of handlers) {
1573
1611
  clearResults.push(handler.clear());
1574
1612
  }