@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.mjs CHANGED
@@ -483,6 +483,14 @@ function jsonEquals(value, otherValue) {
483
483
  }
484
484
  var jsonEquals_default = jsonEquals;
485
485
 
486
+ // ../zimic-utils/dist/chunk-QISSVK3C.mjs
487
+ function waitForDelay(milliseconds) {
488
+ return new Promise((resolve) => {
489
+ setTimeout(resolve, milliseconds);
490
+ });
491
+ }
492
+ var waitForDelay_default = waitForDelay;
493
+
486
494
  // src/utils/data.ts
487
495
  async function convertReadableStreamToBlob(stream, options) {
488
496
  const chunks = [];
@@ -525,6 +533,15 @@ function convertBase64ToArrayBuffer(base64Value) {
525
533
  }
526
534
  }
527
535
 
536
+ // src/utils/numbers.ts
537
+ function random(lowerLimit, upperLimit) {
538
+ const range = Math.max(upperLimit - lowerLimit, 0);
539
+ if (range === 0) {
540
+ return lowerLimit;
541
+ }
542
+ return Math.random() * range + lowerLimit;
543
+ }
544
+
528
545
  // src/http/requestHandler/errors/NoResponseDefinitionError.ts
529
546
  var NoResponseDefinitionError = class extends TypeError {
530
547
  constructor() {
@@ -559,15 +576,31 @@ var HttpRequestHandlerClient = class {
559
576
  limits = {
560
577
  numberOfRequests: DEFAULT_NUMBER_OF_REQUEST_LIMITS
561
578
  };
562
- timesDeclarationPointer;
579
+ timesPointer;
563
580
  numberOfMatchedRequests = 0;
564
581
  unmatchedRequestGroups = [];
565
582
  _requests = [];
566
583
  createResponseDeclaration;
584
+ createResponseDelay;
567
585
  with(restriction) {
568
586
  this.restrictions.push(restriction);
569
587
  return this;
570
588
  }
589
+ delay(minMilliseconds, maxMilliseconds) {
590
+ if (minMilliseconds === maxMilliseconds) {
591
+ return this.delay(minMilliseconds);
592
+ }
593
+ if (typeof minMilliseconds === "number" && typeof maxMilliseconds === "number") {
594
+ this.createResponseDelay = () => random(minMilliseconds, maxMilliseconds);
595
+ return this;
596
+ }
597
+ if (typeof minMilliseconds === "number") {
598
+ this.createResponseDelay = () => minMilliseconds;
599
+ return this;
600
+ }
601
+ this.createResponseDelay = minMilliseconds;
602
+ return this;
603
+ }
571
604
  respond(declaration) {
572
605
  const newThis = this;
573
606
  newThis.createResponseDeclaration = this.isResponseDeclarationFactory(declaration) ? declaration : () => declaration;
@@ -585,7 +618,7 @@ var HttpRequestHandlerClient = class {
585
618
  min: minNumberOfRequests,
586
619
  max: maxNumberOfRequests ?? minNumberOfRequests
587
620
  };
588
- this.timesDeclarationPointer = new TimesDeclarationPointer_default(minNumberOfRequests, maxNumberOfRequests);
621
+ this.timesPointer = new TimesDeclarationPointer_default(minNumberOfRequests, maxNumberOfRequests);
589
622
  return this;
590
623
  }
591
624
  checkTimes() {
@@ -594,7 +627,7 @@ var HttpRequestHandlerClient = class {
594
627
  throw new TimesCheckError_default({
595
628
  requestLimits: this.limits.numberOfRequests,
596
629
  numberOfMatchedRequests: this.numberOfMatchedRequests,
597
- declarationPointer: this.timesDeclarationPointer,
630
+ declarationPointer: this.timesPointer,
598
631
  unmatchedRequestGroups: this.unmatchedRequestGroups,
599
632
  hasRestrictions: this.restrictions.length > 0,
600
633
  requestSaving: this.interceptor.requestSaving
@@ -606,11 +639,12 @@ var HttpRequestHandlerClient = class {
606
639
  this.limits = {
607
640
  numberOfRequests: DEFAULT_NUMBER_OF_REQUEST_LIMITS
608
641
  };
609
- this.timesDeclarationPointer = void 0;
642
+ this.timesPointer = void 0;
610
643
  this.numberOfMatchedRequests = 0;
611
644
  this.unmatchedRequestGroups.length = 0;
612
645
  this.clearInterceptedRequests();
613
646
  this.createResponseDeclaration = void 0;
647
+ this.createResponseDelay = void 0;
614
648
  return this;
615
649
  }
616
650
  async matchesRequest(request) {
@@ -632,7 +666,7 @@ var HttpRequestHandlerClient = class {
632
666
  this.numberOfMatchedRequests++;
633
667
  }
634
668
  markRequestAsUnmatched(request, options) {
635
- const shouldSaveUnmatchedRequests = this.interceptor.requestSaving.enabled && this.restrictions.length > 0 && this.timesDeclarationPointer !== void 0;
669
+ const shouldSaveUnmatchedRequests = this.interceptor.requestSaving.enabled && this.restrictions.length > 0 && this.timesPointer !== void 0;
636
670
  if (shouldSaveUnmatchedRequests) {
637
671
  this.unmatchedRequestGroups.push({ request, diff: options.diff });
638
672
  }
@@ -768,6 +802,12 @@ var HttpRequestHandlerClient = class {
768
802
  if (!this.createResponseDeclaration) {
769
803
  throw new NoResponseDefinitionError_default();
770
804
  }
805
+ if (this.createResponseDelay) {
806
+ const delay = await this.createResponseDelay(request);
807
+ if (delay > 0) {
808
+ await waitForDelay_default(delay);
809
+ }
810
+ }
771
811
  const appliedDeclaration = await this.createResponseDeclaration(request);
772
812
  return appliedDeclaration;
773
813
  }
@@ -816,6 +856,10 @@ var LocalHttpRequestHandler = class {
816
856
  this.client.with(restriction);
817
857
  return this;
818
858
  }
859
+ delay(minMilliseconds, maxMilliseconds) {
860
+ this.client.delay(minMilliseconds, maxMilliseconds);
861
+ return this;
862
+ }
819
863
  respond(declaration) {
820
864
  this.client.respond(declaration);
821
865
  const newThis = this;
@@ -1301,6 +1345,10 @@ var RemoteHttpRequestHandler = class {
1301
1345
  this.client.with(restriction);
1302
1346
  return this.unsynced;
1303
1347
  }
1348
+ delay(minMilliseconds, maxMilliseconds) {
1349
+ this.client.delay(minMilliseconds, maxMilliseconds);
1350
+ return this.unsynced;
1351
+ }
1304
1352
  respond(declaration) {
1305
1353
  const newUnsyncedThis = this.unsynced;
1306
1354
  newUnsyncedThis.client.respond(declaration);