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

Code before:
      read(range2Reader, result2),
      promise,
    ]);

    expect(result1.value).toEqual(rangeSize);
    expect(result2.value).toEqual(tailSize);
    expect(isStreamingSupported).toEqual(false);
    expect(isRangeSupported).toEqual(true);
    expect(fullReaderCancelled).toEqual(true);
  });

  // Common tests to verify behavior across implementations of the IPDFStream
  // interface (PDFNetworkStream / PDFFetchStream).
  function describeRedirectTests(PDFStreamClass) {
    async function simulateFetchFullAndSomeRange(url, readMoreThanOnce) {
      const rangeSize = 32768;
      const stream = new PDFStreamClass({
        url,
        length: basicApiFileLength,
        rangeChunkSize: rangeSize,
        disableStream: true,
        disableRange: false,
      });

      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.

Review comment (suggestion):
Given that `PDFFetchStream` has its tests placed in another file, see https://github.com/mozilla/pdf.js/blob/master/test/unit/fetch_stream_spec.js, it's a bit surprising for these tests to be found here.

Code after:

    expect(result1.value).toEqual(rangeSize);
    expect(result2.value).toEqual(tailSize);
    expect(isStreamingSupported).toEqual(false);
    expect(isRangeSupported).toEqual(true);
    expect(fullReaderCancelled).toEqual(true);
  });

  describe("Redirects", function () {
    beforeAll(async function () {
      await TestPdfsServer.ensureStarted();
    });

    afterAll(async function () {
      await TestPdfsServer.ensureStopped();
    });

    it("redirects allowed if all responses are same-origin", async function () {
      await testCrossOriginRedirects({
        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:.*"\.$/
          );
        },
      });
    });
  });
});