@zimic/interceptor 1.1.7 → 1.2.0-canary.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.
package/dist/http.js CHANGED
@@ -510,6 +510,14 @@ function jsonEquals(value, otherValue) {
510
510
  }
511
511
  var jsonEquals_default = jsonEquals;
512
512
 
513
+ // ../zimic-utils/dist/chunk-QISSVK3C.mjs
514
+ function waitForDelay(milliseconds) {
515
+ return new Promise((resolve) => {
516
+ setTimeout(resolve, milliseconds);
517
+ });
518
+ }
519
+ var waitForDelay_default = waitForDelay;
520
+
513
521
  // src/utils/data.ts
514
522
  async function convertReadableStreamToBlob(stream, options) {
515
523
  const chunks = [];
@@ -552,6 +560,15 @@ function convertBase64ToArrayBuffer(base64Value) {
552
560
  }
553
561
  }
554
562
 
563
+ // src/utils/numbers.ts
564
+ function random(lowerLimit, upperLimit) {
565
+ const range = Math.max(upperLimit - lowerLimit, 0);
566
+ if (range === 0) {
567
+ return lowerLimit;
568
+ }
569
+ return Math.random() * range + lowerLimit;
570
+ }
571
+
555
572
  // src/http/requestHandler/errors/NoResponseDefinitionError.ts
556
573
  var NoResponseDefinitionError = class extends TypeError {
557
574
  constructor() {
@@ -586,15 +603,31 @@ var HttpRequestHandlerClient = class {
586
603
  limits = {
587
604
  numberOfRequests: DEFAULT_NUMBER_OF_REQUEST_LIMITS
588
605
  };
589
- timesDeclarationPointer;
606
+ timesPointer;
590
607
  numberOfMatchedRequests = 0;
591
608
  unmatchedRequestGroups = [];
592
609
  _requests = [];
593
610
  createResponseDeclaration;
611
+ createResponseDelay;
594
612
  with(restriction) {
595
613
  this.restrictions.push(restriction);
596
614
  return this;
597
615
  }
616
+ delay(minMilliseconds, maxMilliseconds) {
617
+ if (minMilliseconds === maxMilliseconds) {
618
+ return this.delay(minMilliseconds);
619
+ }
620
+ if (typeof minMilliseconds === "number" && typeof maxMilliseconds === "number") {
621
+ this.createResponseDelay = () => random(minMilliseconds, maxMilliseconds);
622
+ return this;
623
+ }
624
+ if (typeof minMilliseconds === "number") {
625
+ this.createResponseDelay = () => minMilliseconds;
626
+ return this;
627
+ }
628
+ this.createResponseDelay = minMilliseconds;
629
+ return this;
630
+ }
598
631
  respond(declaration) {
599
632
  const newThis = this;
600
633
  newThis.createResponseDeclaration = this.isResponseDeclarationFactory(declaration) ? declaration : () => declaration;
@@ -612,7 +645,7 @@ var HttpRequestHandlerClient = class {
612
645
  min: minNumberOfRequests,
613
646
  max: maxNumberOfRequests ?? minNumberOfRequests
614
647
  };
615
- this.timesDeclarationPointer = new TimesDeclarationPointer_default(minNumberOfRequests, maxNumberOfRequests);
648
+ this.timesPointer = new TimesDeclarationPointer_default(minNumberOfRequests, maxNumberOfRequests);
616
649
  return this;
617
650
  }
618
651
  checkTimes() {
@@ -621,7 +654,7 @@ var HttpRequestHandlerClient = class {
621
654
  throw new TimesCheckError_default({
622
655
  requestLimits: this.limits.numberOfRequests,
623
656
  numberOfMatchedRequests: this.numberOfMatchedRequests,
624
- declarationPointer: this.timesDeclarationPointer,
657
+ declarationPointer: this.timesPointer,
625
658
  unmatchedRequestGroups: this.unmatchedRequestGroups,
626
659
  hasRestrictions: this.restrictions.length > 0,
627
660
  requestSaving: this.interceptor.requestSaving
@@ -633,11 +666,12 @@ var HttpRequestHandlerClient = class {
633
666
  this.limits = {
634
667
  numberOfRequests: DEFAULT_NUMBER_OF_REQUEST_LIMITS
635
668
  };
636
- this.timesDeclarationPointer = void 0;
669
+ this.timesPointer = void 0;
637
670
  this.numberOfMatchedRequests = 0;
638
671
  this.unmatchedRequestGroups.length = 0;
639
672
  this.clearInterceptedRequests();
640
673
  this.createResponseDeclaration = void 0;
674
+ this.createResponseDelay = void 0;
641
675
  return this;
642
676
  }
643
677
  async matchesRequest(request) {
@@ -659,7 +693,7 @@ var HttpRequestHandlerClient = class {
659
693
  this.numberOfMatchedRequests++;
660
694
  }
661
695
  markRequestAsUnmatched(request, options) {
662
- const shouldSaveUnmatchedRequests = this.interceptor.requestSaving.enabled && this.restrictions.length > 0 && this.timesDeclarationPointer !== void 0;
696
+ const shouldSaveUnmatchedRequests = this.interceptor.requestSaving.enabled && this.restrictions.length > 0 && this.timesPointer !== void 0;
663
697
  if (shouldSaveUnmatchedRequests) {
664
698
  this.unmatchedRequestGroups.push({ request, diff: options.diff });
665
699
  }
@@ -795,6 +829,12 @@ var HttpRequestHandlerClient = class {
795
829
  if (!this.createResponseDeclaration) {
796
830
  throw new NoResponseDefinitionError_default();
797
831
  }
832
+ if (this.createResponseDelay) {
833
+ const delay = await this.createResponseDelay(request);
834
+ if (delay > 0) {
835
+ await waitForDelay_default(delay);
836
+ }
837
+ }
798
838
  const appliedDeclaration = await this.createResponseDeclaration(request);
799
839
  return appliedDeclaration;
800
840
  }
@@ -843,6 +883,10 @@ var LocalHttpRequestHandler = class {
843
883
  this.client.with(restriction);
844
884
  return this;
845
885
  }
886
+ delay(minMilliseconds, maxMilliseconds) {
887
+ this.client.delay(minMilliseconds, maxMilliseconds);
888
+ return this;
889
+ }
846
890
  respond(declaration) {
847
891
  this.client.respond(declaration);
848
892
  const newThis = this;
@@ -1328,6 +1372,10 @@ var RemoteHttpRequestHandler = class {
1328
1372
  this.client.with(restriction);
1329
1373
  return this.unsynced;
1330
1374
  }
1375
+ delay(minMilliseconds, maxMilliseconds) {
1376
+ this.client.delay(minMilliseconds, maxMilliseconds);
1377
+ return this.unsynced;
1378
+ }
1331
1379
  respond(declaration) {
1332
1380
  const newUnsyncedThis = this.unsynced;
1333
1381
  newUnsyncedThis.client.respond(declaration);