Skip to content

Commit 15e58f3

Browse files
Merge pull request #20830 from Snuffleupagus/validateRangeRequestCapabilities-fix-tests
Improve the `validateRangeRequestCapabilities` unit-tests
2 parents bf20d3c + a1b769c commit 15e58f3

File tree

4 files changed

+38
-42
lines changed

4 files changed

+38
-42
lines changed

src/display/fetch_stream.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,17 @@ class PDFFetchStreamReader extends BasePDFStreamReader {
107107

108108
const responseHeaders = response.headers;
109109

110-
const { allowRangeRequests, suggestedLength } =
110+
const { contentLength, isRangeSupported } =
111111
validateRangeRequestCapabilities({
112112
responseHeaders,
113113
isHttp: true,
114114
rangeChunkSize,
115115
disableRange,
116116
});
117117

118-
this._isRangeSupported = allowRangeRequests;
118+
this._isRangeSupported = isRangeSupported;
119119
// Setting right content length.
120-
this._contentLength = suggestedLength || this._contentLength;
120+
this._contentLength = contentLength || this._contentLength;
121121

122122
this._filename = extractFilenameFromHeader(responseHeaders);
123123

src/display/network.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,19 +222,19 @@ class PDFNetworkStreamReader extends BasePDFStreamReader {
222222
: []
223223
);
224224

225-
const { allowRangeRequests, suggestedLength } =
225+
const { contentLength, isRangeSupported } =
226226
validateRangeRequestCapabilities({
227227
responseHeaders,
228228
isHttp: stream.isHttp,
229229
rangeChunkSize,
230230
disableRange,
231231
});
232232

233-
if (allowRangeRequests) {
233+
if (isRangeSupported) {
234234
this._isRangeSupported = true;
235235
}
236236
// Setting right content length.
237-
this._contentLength = suggestedLength || this._contentLength;
237+
this._contentLength = contentLength || this._contentLength;
238238

239239
this._filename = extractFilenameFromHeader(responseHeaders);
240240

src/display/network_utils.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,38 +49,34 @@ function validateRangeRequestCapabilities({
4949
"rangeChunkSize must be an integer larger than zero."
5050
);
5151
}
52-
const returnValues = {
53-
allowRangeRequests: false,
54-
suggestedLength: undefined,
52+
const rv = {
53+
contentLength: undefined,
54+
isRangeSupported: false,
5555
};
5656

5757
const length = parseInt(responseHeaders.get("Content-Length"), 10);
5858
if (!Number.isInteger(length)) {
59-
return returnValues;
59+
return rv;
6060
}
61-
62-
returnValues.suggestedLength = length;
61+
rv.contentLength = length;
6362

6463
if (length <= 2 * rangeChunkSize) {
6564
// The file size is smaller than the size of two chunks, so it does not
6665
// make any sense to abort the request and retry with a range request.
67-
return returnValues;
66+
return rv;
6867
}
69-
7068
if (disableRange || !isHttp) {
71-
return returnValues;
69+
return rv;
7270
}
7371
if (responseHeaders.get("Accept-Ranges") !== "bytes") {
74-
return returnValues;
72+
return rv;
7573
}
7674

7775
const contentEncoding = responseHeaders.get("Content-Encoding") || "identity";
78-
if (contentEncoding !== "identity") {
79-
return returnValues;
76+
if (contentEncoding === "identity") {
77+
rv.isRangeSupported = true;
8078
}
81-
82-
returnValues.allowRangeRequests = true;
83-
return returnValues;
79+
return rv;
8480
}
8581

8682
function extractFilenameFromHeader(responseHeaders) {

test/unit/network_utils_spec.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -81,27 +81,27 @@ describe("network_utils", function () {
8181
disableRange: true,
8282
isHttp: true,
8383
responseHeaders: new Headers({
84-
"Content-Length": 8,
84+
"Content-Length": 1024,
8585
}),
8686
rangeChunkSize: 64,
8787
})
8888
).toEqual({
89-
allowRangeRequests: false,
90-
suggestedLength: 8,
89+
isRangeSupported: false,
90+
contentLength: 1024,
9191
});
9292

9393
expect(
9494
validateRangeRequestCapabilities({
9595
disableRange: false,
9696
isHttp: false,
9797
responseHeaders: new Headers({
98-
"Content-Length": 8,
98+
"Content-Length": 1024,
9999
}),
100100
rangeChunkSize: 64,
101101
})
102102
).toEqual({
103-
allowRangeRequests: false,
104-
suggestedLength: 8,
103+
isRangeSupported: false,
104+
contentLength: 1024,
105105
});
106106
});
107107

@@ -112,13 +112,13 @@ describe("network_utils", function () {
112112
isHttp: true,
113113
responseHeaders: new Headers({
114114
"Accept-Ranges": "none",
115-
"Content-Length": 8,
115+
"Content-Length": 1024,
116116
}),
117117
rangeChunkSize: 64,
118118
})
119119
).toEqual({
120-
allowRangeRequests: false,
121-
suggestedLength: 8,
120+
isRangeSupported: false,
121+
contentLength: 1024,
122122
});
123123
});
124124

@@ -130,13 +130,13 @@ describe("network_utils", function () {
130130
responseHeaders: new Headers({
131131
"Accept-Ranges": "bytes",
132132
"Content-Encoding": "gzip",
133-
"Content-Length": 8,
133+
"Content-Length": 1024,
134134
}),
135135
rangeChunkSize: 64,
136136
})
137137
).toEqual({
138-
allowRangeRequests: false,
139-
suggestedLength: 8,
138+
isRangeSupported: false,
139+
contentLength: 1024,
140140
});
141141
});
142142

@@ -147,13 +147,13 @@ describe("network_utils", function () {
147147
isHttp: true,
148148
responseHeaders: new Headers({
149149
"Accept-Ranges": "bytes",
150-
"Content-Length": "eight",
150+
"Content-Length": "one thousand and twenty four",
151151
}),
152152
rangeChunkSize: 64,
153153
})
154154
).toEqual({
155-
allowRangeRequests: false,
156-
suggestedLength: undefined,
155+
isRangeSupported: false,
156+
contentLength: undefined,
157157
});
158158
});
159159

@@ -164,13 +164,13 @@ describe("network_utils", function () {
164164
isHttp: true,
165165
responseHeaders: new Headers({
166166
"Accept-Ranges": "bytes",
167-
"Content-Length": 8,
167+
"Content-Length": 128,
168168
}),
169169
rangeChunkSize: 64,
170170
})
171171
).toEqual({
172-
allowRangeRequests: false,
173-
suggestedLength: 8,
172+
isRangeSupported: false,
173+
contentLength: 128,
174174
});
175175
});
176176

@@ -181,13 +181,13 @@ describe("network_utils", function () {
181181
isHttp: true,
182182
responseHeaders: new Headers({
183183
"Accept-Ranges": "bytes",
184-
"Content-Length": 8192,
184+
"Content-Length": 1024,
185185
}),
186186
rangeChunkSize: 64,
187187
})
188188
).toEqual({
189-
allowRangeRequests: true,
190-
suggestedLength: 8192,
189+
isRangeSupported: true,
190+
contentLength: 1024,
191191
});
192192
});
193193
});

0 commit comments

Comments
 (0)