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