forked from mozilla/pdf.js
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test cases for redirected responses
Regression tests for issue mozilla#12744 and PR mozilla#19028
- Loading branch information
Showing
6 changed files
with
222 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* Copyright 2024 Mozilla Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { AbortException, isNodeJS } from "../../src/shared/util.js"; | ||
import { getCrossOriginHostname, TestPdfsServer } from "./test_utils.js"; | ||
|
||
// Common tests to verify behavior across implementations of the IPDFStream | ||
// interface: | ||
// - PDFNetworkStream by network_spec.js | ||
// - PDFFetchStream by fetch_stream_spec.js | ||
async function testCrossOriginRedirects({ | ||
PDFStreamClass, | ||
redirectIfRange, | ||
testRangeReader, | ||
}) { | ||
const basicApiUrl = TestPdfsServer.resolveURL("basicapi.pdf").href; | ||
const basicApiFileLength = 105779; | ||
|
||
const rangeSize = 32768; | ||
const stream = new PDFStreamClass({ | ||
url: getCrossOriginUrlWithRedirects(basicApiUrl, redirectIfRange), | ||
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(true); | ||
|
||
fullReader.cancel(new AbortException("Don't need fullReader.")); | ||
|
||
const rangeReader = stream.getRangeReader( | ||
basicApiFileLength - rangeSize, | ||
basicApiFileLength | ||
); | ||
|
||
try { | ||
await testRangeReader(rangeReader); | ||
} finally { | ||
rangeReader.cancel(new AbortException("Don't need rangeReader")); | ||
} | ||
} | ||
|
||
/** | ||
* @param {string} testserverUrl - A URL handled that supports CORS and | ||
* redirects (see crossOriginHandler and redirectHandler in webserver.mjs). | ||
* @param {boolean} redirectIfRange - Whether Range requests should be | ||
* redirected to a different origin compared to the initial request. | ||
* @returns {string} A URL that will be redirected by the server. | ||
*/ | ||
function getCrossOriginUrlWithRedirects(testserverUrl, redirectIfRange) { | ||
const url = new URL(testserverUrl); | ||
if (!isNodeJS) { | ||
// The responses are going to be cross-origin. In Node.js, fetch() allows | ||
// cross-origin requests for any request, but in browser environments we | ||
// need to enable CORS. | ||
// 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; | ||
} | ||
|
||
export { testCrossOriginRedirects }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters