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

Code before:
        lenResult.value += result.value.byteLength;
        return read(reader, lenResult);
      });
    };

    await Promise.all([
      read(range1Reader, result1),
      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);
  });

  describe("Redirects", function () {
    async function simulateFetchFullAndSomeRange(url, readMoreThanOnce) {
      const rangeSize = 32768;
      const stream = new PDFNetworkStream({
        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

Review comment (suggestion):
Nit: Can we please add a blank line here, to aid readability a little bit?
```suggestion
    }

    it("redirects allowed if all responses are same-origin", async function () {
```

Code after:
        return read(reader, lenResult);
      });
    };

    await Promise.all([
      read(range1Reader, result1),
      read(range2Reader, result2),
    ]);

    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:.*"\.$/
          );
        },
      });
    });