1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | 'use strict';
|
23 |
|
24 | var _network_utils = require('../../display/network_utils');
|
25 |
|
26 | var _util = require('../../shared/util');
|
27 |
|
28 | describe('network_utils', function () {
|
29 | describe('validateRangeRequestCapabilities', function () {
|
30 | var defaultValues = {
|
31 | allowRangeRequests: false,
|
32 | suggestedLength: undefined
|
33 | };
|
34 | it('rejects range chunk sizes that are not larger than zero', function () {
|
35 | expect(function () {
|
36 | (0, _network_utils.validateRangeRequestCapabilities)({ rangeChunkSize: 0 });
|
37 | }).toThrow(new Error('Range chunk size must be larger than zero'));
|
38 | });
|
39 | it('rejects disabled or non-HTTP range requests', function () {
|
40 | expect((0, _network_utils.validateRangeRequestCapabilities)({
|
41 | disableRange: true,
|
42 | isHttp: true,
|
43 | rangeChunkSize: 64
|
44 | })).toEqual(defaultValues);
|
45 | expect((0, _network_utils.validateRangeRequestCapabilities)({
|
46 | disableRange: false,
|
47 | isHttp: false,
|
48 | rangeChunkSize: 64
|
49 | })).toEqual(defaultValues);
|
50 | });
|
51 | it('rejects invalid Accept-Ranges header values', function () {
|
52 | expect((0, _network_utils.validateRangeRequestCapabilities)({
|
53 | disableRange: false,
|
54 | isHttp: true,
|
55 | getResponseHeader: function getResponseHeader(headerName) {
|
56 | if (headerName === 'Accept-Ranges') {
|
57 | return 'none';
|
58 | }
|
59 | },
|
60 | rangeChunkSize: 64
|
61 | })).toEqual(defaultValues);
|
62 | });
|
63 | it('rejects invalid Content-Encoding header values', function () {
|
64 | expect((0, _network_utils.validateRangeRequestCapabilities)({
|
65 | disableRange: false,
|
66 | isHttp: true,
|
67 | getResponseHeader: function getResponseHeader(headerName) {
|
68 | if (headerName === 'Accept-Ranges') {
|
69 | return 'bytes';
|
70 | } else if (headerName === 'Content-Encoding') {
|
71 | return 'gzip';
|
72 | }
|
73 | },
|
74 | rangeChunkSize: 64
|
75 | })).toEqual(defaultValues);
|
76 | });
|
77 | it('rejects invalid Content-Length header values', function () {
|
78 | expect((0, _network_utils.validateRangeRequestCapabilities)({
|
79 | disableRange: false,
|
80 | isHttp: true,
|
81 | getResponseHeader: function getResponseHeader(headerName) {
|
82 | if (headerName === 'Accept-Ranges') {
|
83 | return 'bytes';
|
84 | } else if (headerName === 'Content-Encoding') {
|
85 | return null;
|
86 | } else if (headerName === 'Content-Length') {
|
87 | return 'eight';
|
88 | }
|
89 | },
|
90 | rangeChunkSize: 64
|
91 | })).toEqual(defaultValues);
|
92 | });
|
93 | it('rejects file sizes that are too small for range requests', function () {
|
94 | expect((0, _network_utils.validateRangeRequestCapabilities)({
|
95 | disableRange: false,
|
96 | isHttp: true,
|
97 | getResponseHeader: function getResponseHeader(headerName) {
|
98 | if (headerName === 'Accept-Ranges') {
|
99 | return 'bytes';
|
100 | } else if (headerName === 'Content-Encoding') {
|
101 | return null;
|
102 | } else if (headerName === 'Content-Length') {
|
103 | return 8;
|
104 | }
|
105 | },
|
106 | rangeChunkSize: 64
|
107 | })).toEqual({
|
108 | allowRangeRequests: false,
|
109 | suggestedLength: 8
|
110 | });
|
111 | });
|
112 | it('accepts file sizes large enough for range requests', function () {
|
113 | expect((0, _network_utils.validateRangeRequestCapabilities)({
|
114 | disableRange: false,
|
115 | isHttp: true,
|
116 | getResponseHeader: function getResponseHeader(headerName) {
|
117 | if (headerName === 'Accept-Ranges') {
|
118 | return 'bytes';
|
119 | } else if (headerName === 'Content-Encoding') {
|
120 | return null;
|
121 | } else if (headerName === 'Content-Length') {
|
122 | return 8192;
|
123 | }
|
124 | },
|
125 | rangeChunkSize: 64
|
126 | })).toEqual({
|
127 | allowRangeRequests: true,
|
128 | suggestedLength: 8192
|
129 | });
|
130 | });
|
131 | });
|
132 | describe('extractFilenameFromHeader', function () {
|
133 | it('returns null when content disposition header is blank', function () {
|
134 | expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
|
135 | if (headerName === 'Content-Disposition') {
|
136 | return null;
|
137 | }
|
138 | })).toBeNull();
|
139 | expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
|
140 | if (headerName === 'Content-Disposition') {
|
141 | return undefined;
|
142 | }
|
143 | })).toBeNull();
|
144 | expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
|
145 | if (headerName === 'Content-Disposition') {
|
146 | return '';
|
147 | }
|
148 | })).toBeNull();
|
149 | });
|
150 | it('gets the filename from the response header', function () {
|
151 | expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
|
152 | if (headerName === 'Content-Disposition') {
|
153 | return 'inline';
|
154 | }
|
155 | })).toBeNull();
|
156 | expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
|
157 | if (headerName === 'Content-Disposition') {
|
158 | return 'attachment';
|
159 | }
|
160 | })).toBeNull();
|
161 | expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
|
162 | if (headerName === 'Content-Disposition') {
|
163 | return 'attachment; filename="filename.pdf"';
|
164 | }
|
165 | })).toEqual('filename.pdf');
|
166 | expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
|
167 | if (headerName === 'Content-Disposition') {
|
168 | return 'attachment; filename="filename.pdf and spaces.pdf"';
|
169 | }
|
170 | })).toEqual('filename.pdf and spaces.pdf');
|
171 | expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
|
172 | if (headerName === 'Content-Disposition') {
|
173 | return 'attachment; filename="tl;dr.pdf"';
|
174 | }
|
175 | })).toEqual('tl;dr.pdf');
|
176 | expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
|
177 | if (headerName === 'Content-Disposition') {
|
178 | return 'attachment; filename=filename.pdf';
|
179 | }
|
180 | })).toEqual('filename.pdf');
|
181 | expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
|
182 | if (headerName === 'Content-Disposition') {
|
183 | return 'attachment; filename=filename.pdf someotherparam';
|
184 | }
|
185 | })).toEqual('filename.pdf');
|
186 | });
|
187 | it('gets the filename from the response header (RFC 6266)', function () {
|
188 | expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
|
189 | if (headerName === 'Content-Disposition') {
|
190 | return 'attachment; filename*=filename.pdf';
|
191 | }
|
192 | })).toEqual('filename.pdf');
|
193 | expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
|
194 | if (headerName === 'Content-Disposition') {
|
195 | return 'attachment; filename*=\'\'filename.pdf';
|
196 | }
|
197 | })).toEqual('filename.pdf');
|
198 | expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
|
199 | if (headerName === 'Content-Disposition') {
|
200 | return 'attachment; filename*=utf-8\'\'filename.pdf';
|
201 | }
|
202 | })).toEqual('filename.pdf');
|
203 | expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
|
204 | if (headerName === 'Content-Disposition') {
|
205 | return 'attachment; filename=no.pdf; filename*=utf-8\'\'filename.pdf';
|
206 | }
|
207 | })).toEqual('filename.pdf');
|
208 | expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
|
209 | if (headerName === 'Content-Disposition') {
|
210 | return 'attachment; filename*=utf-8\'\'filename.pdf; filename=no.pdf';
|
211 | }
|
212 | })).toEqual('filename.pdf');
|
213 | });
|
214 | it('gets the filename from the response header (RFC 2231)', function () {
|
215 | expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
|
216 | if (headerName === 'Content-Disposition') {
|
217 | return 'attachment; filename*0=filename; filename*1=.pdf';
|
218 | }
|
219 | })).toEqual('filename.pdf');
|
220 | });
|
221 | it('only extracts filename with pdf extension', function () {
|
222 | expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
|
223 | if (headerName === 'Content-Disposition') {
|
224 | return 'attachment; filename="filename.png"';
|
225 | }
|
226 | })).toBeNull();
|
227 | });
|
228 | it('extension validation is case insensitive', function () {
|
229 | expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
|
230 | if (headerName === 'Content-Disposition') {
|
231 | return 'form-data; name="fieldName"; filename="file.PdF"';
|
232 | }
|
233 | })).toEqual('file.PdF');
|
234 | });
|
235 | });
|
236 | describe('createResponseStatusError', function () {
|
237 | it('handles missing PDF file responses', function () {
|
238 | expect((0, _network_utils.createResponseStatusError)(404, 'https://foo.com/bar.pdf')).toEqual(new _util.MissingPDFException('Missing PDF "https://foo.com/bar.pdf".'));
|
239 | expect((0, _network_utils.createResponseStatusError)(0, 'file://foo.pdf')).toEqual(new _util.MissingPDFException('Missing PDF "file://foo.pdf".'));
|
240 | });
|
241 | it('handles unexpected responses', function () {
|
242 | expect((0, _network_utils.createResponseStatusError)(302, 'https://foo.com/bar.pdf')).toEqual(new _util.UnexpectedResponseException('Unexpected server response (302) while retrieving PDF ' + '"https://foo.com/bar.pdf".'));
|
243 | expect((0, _network_utils.createResponseStatusError)(0, 'https://foo.com/bar.pdf')).toEqual(new _util.UnexpectedResponseException('Unexpected server response (0) while retrieving PDF ' + '"https://foo.com/bar.pdf".'));
|
244 | });
|
245 | });
|
246 | describe('validateResponseStatus', function () {
|
247 | it('accepts valid response statuses', function () {
|
248 | expect((0, _network_utils.validateResponseStatus)(200)).toEqual(true);
|
249 | expect((0, _network_utils.validateResponseStatus)(206)).toEqual(true);
|
250 | });
|
251 | it('rejects invalid response statuses', function () {
|
252 | expect((0, _network_utils.validateResponseStatus)(302)).toEqual(false);
|
253 | expect((0, _network_utils.validateResponseStatus)(404)).toEqual(false);
|
254 | expect((0, _network_utils.validateResponseStatus)(null)).toEqual(false);
|
255 | expect((0, _network_utils.validateResponseStatus)(undefined)).toEqual(false);
|
256 | });
|
257 | });
|
258 | }); |
\ | No newline at end of file |