Repository: mozilla/pdf.js
File: test/unit/network_spec.js (JavaScript)

Code before:
      });

      const fullReader = stream.getFullReader();

      await fullReader.headersReady;
      // Sanity check: We can only test range requests if supported:
      expect(fullReader.isRangeSupported).toEqual(false);

      fullReader.cancel(new AbortException("Don't need fullReader."));

      const rangeReader = stream.getRangeReader(
        basicApiFileLength - rangeSize,
        basicApiFileLength
      );
      // May throw or not throw - the caller will verify it:
      await rangeReader.read();
      if (readMoreThanOnce) {
        await rangeReader.read();
      }
      rangeReader.cancel(new AbortException("Don't need rangeReader"));
    }
    function getCrossOriginUrlWithRedirects({ redirectIfRange = false }) {
      const url = new URL(basicApiUrl);
      // The responses are going to be cross-origin, and therefore we need CORS
      // to read it. This option depends on crossOriginHandler in webserver.mjs.
      url.searchParams.set("cors", "withoutCredentials");

      // This redirect options depend on redirectHandler in webserver.mjs.

      // We will change the host to a cross-origin domain so that the initial
      // request will be cross-origin. Set "redirectToHost" to the original host
      // to force a cross-origin redirect (relative to the initial URL).
      url.searchParams.set("redirectToHost", url.hostname);
      url.hostname = getCrossOriginHostname(url.hostname);
      if (redirectIfRange) {
        url.searchParams.set("redirectIfRange", "1");
      }
      return url.href;
    }
    it("redirects allowed if all responses are same-origin", async function () {
      const pdfUrl = getCrossOriginUrlWithRedirects({ redirectIfRange: false });
      await expectAsync(simulateFetchFullAndSomeRange(pdfUrl)).toBeResolved();
    });

    it("redirects blocked if any response is cross-origin, read once", async function () {
      const pdfUrl = getCrossOriginUrlWithRedirects({ redirectIfRange: true });
      await expectAsync(
        simulateFetchFullAndSomeRange(pdfUrl)
      ).toBeRejectedWithError(
        /^Expected range response-origin "http:.*" to match "http:.*"\.$/
      );

Review comment (suggestion):
Nit: Can we please add a blank line here, to aid readability a little bit?
```suggestion
    }

    function getCrossOriginUrlWithRedirects({ redirectIfRange = false }) {
```

Code after:
        PDFStreamClass: PDFNetworkStream,
        redirectIfRange: false,
        async testRangeReader(rangeReader) {
          await expectAsync(rangeReader.read()).toBeResolved();
        },
      });
    });

    it("redirects blocked if any response is cross-origin", async function () {
      await testCrossOriginRedirects({
        PDFStreamClass: PDFNetworkStream,
        redirectIfRange: true,
        async testRangeReader(rangeReader) {
          // When read (sync), error should be reported.
          await expectAsync(rangeReader.read()).toBeRejectedWithError(
            /^Expected range response-origin "http:.*" to match "http:.*"\.$/
          );
          // When read again (async), error should be consistent.
          await expectAsync(rangeReader.read()).toBeRejectedWithError(
            /^Expected range response-origin "http:.*" to match "http:.*"\.$/
          );
        },
      });
    });
  });
});