mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-04-09 23:04:02 +02:00
Merge pull request #20830 from Snuffleupagus/validateRangeRequestCapabilities-fix-tests
Improve the `validateRangeRequestCapabilities` unit-tests
This commit is contained in:
commit
15e58f3912
@ -107,7 +107,7 @@ class PDFFetchStreamReader extends BasePDFStreamReader {
|
||||
|
||||
const responseHeaders = response.headers;
|
||||
|
||||
const { allowRangeRequests, suggestedLength } =
|
||||
const { contentLength, isRangeSupported } =
|
||||
validateRangeRequestCapabilities({
|
||||
responseHeaders,
|
||||
isHttp: true,
|
||||
@ -115,9 +115,9 @@ class PDFFetchStreamReader extends BasePDFStreamReader {
|
||||
disableRange,
|
||||
});
|
||||
|
||||
this._isRangeSupported = allowRangeRequests;
|
||||
this._isRangeSupported = isRangeSupported;
|
||||
// Setting right content length.
|
||||
this._contentLength = suggestedLength || this._contentLength;
|
||||
this._contentLength = contentLength || this._contentLength;
|
||||
|
||||
this._filename = extractFilenameFromHeader(responseHeaders);
|
||||
|
||||
|
||||
@ -222,7 +222,7 @@ class PDFNetworkStreamReader extends BasePDFStreamReader {
|
||||
: []
|
||||
);
|
||||
|
||||
const { allowRangeRequests, suggestedLength } =
|
||||
const { contentLength, isRangeSupported } =
|
||||
validateRangeRequestCapabilities({
|
||||
responseHeaders,
|
||||
isHttp: stream.isHttp,
|
||||
@ -230,11 +230,11 @@ class PDFNetworkStreamReader extends BasePDFStreamReader {
|
||||
disableRange,
|
||||
});
|
||||
|
||||
if (allowRangeRequests) {
|
||||
if (isRangeSupported) {
|
||||
this._isRangeSupported = true;
|
||||
}
|
||||
// Setting right content length.
|
||||
this._contentLength = suggestedLength || this._contentLength;
|
||||
this._contentLength = contentLength || this._contentLength;
|
||||
|
||||
this._filename = extractFilenameFromHeader(responseHeaders);
|
||||
|
||||
|
||||
@ -49,38 +49,34 @@ function validateRangeRequestCapabilities({
|
||||
"rangeChunkSize must be an integer larger than zero."
|
||||
);
|
||||
}
|
||||
const returnValues = {
|
||||
allowRangeRequests: false,
|
||||
suggestedLength: undefined,
|
||||
const rv = {
|
||||
contentLength: undefined,
|
||||
isRangeSupported: false,
|
||||
};
|
||||
|
||||
const length = parseInt(responseHeaders.get("Content-Length"), 10);
|
||||
if (!Number.isInteger(length)) {
|
||||
return returnValues;
|
||||
return rv;
|
||||
}
|
||||
|
||||
returnValues.suggestedLength = length;
|
||||
rv.contentLength = length;
|
||||
|
||||
if (length <= 2 * rangeChunkSize) {
|
||||
// The file size is smaller than the size of two chunks, so it does not
|
||||
// make any sense to abort the request and retry with a range request.
|
||||
return returnValues;
|
||||
return rv;
|
||||
}
|
||||
|
||||
if (disableRange || !isHttp) {
|
||||
return returnValues;
|
||||
return rv;
|
||||
}
|
||||
if (responseHeaders.get("Accept-Ranges") !== "bytes") {
|
||||
return returnValues;
|
||||
return rv;
|
||||
}
|
||||
|
||||
const contentEncoding = responseHeaders.get("Content-Encoding") || "identity";
|
||||
if (contentEncoding !== "identity") {
|
||||
return returnValues;
|
||||
if (contentEncoding === "identity") {
|
||||
rv.isRangeSupported = true;
|
||||
}
|
||||
|
||||
returnValues.allowRangeRequests = true;
|
||||
return returnValues;
|
||||
return rv;
|
||||
}
|
||||
|
||||
function extractFilenameFromHeader(responseHeaders) {
|
||||
|
||||
@ -81,13 +81,13 @@ describe("network_utils", function () {
|
||||
disableRange: true,
|
||||
isHttp: true,
|
||||
responseHeaders: new Headers({
|
||||
"Content-Length": 8,
|
||||
"Content-Length": 1024,
|
||||
}),
|
||||
rangeChunkSize: 64,
|
||||
})
|
||||
).toEqual({
|
||||
allowRangeRequests: false,
|
||||
suggestedLength: 8,
|
||||
isRangeSupported: false,
|
||||
contentLength: 1024,
|
||||
});
|
||||
|
||||
expect(
|
||||
@ -95,13 +95,13 @@ describe("network_utils", function () {
|
||||
disableRange: false,
|
||||
isHttp: false,
|
||||
responseHeaders: new Headers({
|
||||
"Content-Length": 8,
|
||||
"Content-Length": 1024,
|
||||
}),
|
||||
rangeChunkSize: 64,
|
||||
})
|
||||
).toEqual({
|
||||
allowRangeRequests: false,
|
||||
suggestedLength: 8,
|
||||
isRangeSupported: false,
|
||||
contentLength: 1024,
|
||||
});
|
||||
});
|
||||
|
||||
@ -112,13 +112,13 @@ describe("network_utils", function () {
|
||||
isHttp: true,
|
||||
responseHeaders: new Headers({
|
||||
"Accept-Ranges": "none",
|
||||
"Content-Length": 8,
|
||||
"Content-Length": 1024,
|
||||
}),
|
||||
rangeChunkSize: 64,
|
||||
})
|
||||
).toEqual({
|
||||
allowRangeRequests: false,
|
||||
suggestedLength: 8,
|
||||
isRangeSupported: false,
|
||||
contentLength: 1024,
|
||||
});
|
||||
});
|
||||
|
||||
@ -130,13 +130,13 @@ describe("network_utils", function () {
|
||||
responseHeaders: new Headers({
|
||||
"Accept-Ranges": "bytes",
|
||||
"Content-Encoding": "gzip",
|
||||
"Content-Length": 8,
|
||||
"Content-Length": 1024,
|
||||
}),
|
||||
rangeChunkSize: 64,
|
||||
})
|
||||
).toEqual({
|
||||
allowRangeRequests: false,
|
||||
suggestedLength: 8,
|
||||
isRangeSupported: false,
|
||||
contentLength: 1024,
|
||||
});
|
||||
});
|
||||
|
||||
@ -147,13 +147,13 @@ describe("network_utils", function () {
|
||||
isHttp: true,
|
||||
responseHeaders: new Headers({
|
||||
"Accept-Ranges": "bytes",
|
||||
"Content-Length": "eight",
|
||||
"Content-Length": "one thousand and twenty four",
|
||||
}),
|
||||
rangeChunkSize: 64,
|
||||
})
|
||||
).toEqual({
|
||||
allowRangeRequests: false,
|
||||
suggestedLength: undefined,
|
||||
isRangeSupported: false,
|
||||
contentLength: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
@ -164,13 +164,13 @@ describe("network_utils", function () {
|
||||
isHttp: true,
|
||||
responseHeaders: new Headers({
|
||||
"Accept-Ranges": "bytes",
|
||||
"Content-Length": 8,
|
||||
"Content-Length": 128,
|
||||
}),
|
||||
rangeChunkSize: 64,
|
||||
})
|
||||
).toEqual({
|
||||
allowRangeRequests: false,
|
||||
suggestedLength: 8,
|
||||
isRangeSupported: false,
|
||||
contentLength: 128,
|
||||
});
|
||||
});
|
||||
|
||||
@ -181,13 +181,13 @@ describe("network_utils", function () {
|
||||
isHttp: true,
|
||||
responseHeaders: new Headers({
|
||||
"Accept-Ranges": "bytes",
|
||||
"Content-Length": 8192,
|
||||
"Content-Length": 1024,
|
||||
}),
|
||||
rangeChunkSize: 64,
|
||||
})
|
||||
).toEqual({
|
||||
allowRangeRequests: true,
|
||||
suggestedLength: 8192,
|
||||
isRangeSupported: true,
|
||||
contentLength: 1024,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user