network_utils_spec.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. var _network_utils = require("../../display/network_utils.js");
  24. var _util = require("../../shared/util.js");
  25. describe("network_utils", function () {
  26. describe("validateRangeRequestCapabilities", function () {
  27. it("rejects range chunk sizes that are not larger than zero", function () {
  28. expect(function () {
  29. (0, _network_utils.validateRangeRequestCapabilities)({
  30. rangeChunkSize: 0
  31. });
  32. }).toThrow(new Error("Range chunk size must be larger than zero"));
  33. });
  34. it("rejects disabled or non-HTTP range requests", function () {
  35. expect((0, _network_utils.validateRangeRequestCapabilities)({
  36. disableRange: true,
  37. isHttp: true,
  38. getResponseHeader: headerName => {
  39. if (headerName === "Content-Length") {
  40. return 8;
  41. }
  42. throw new Error(`Unexpected headerName: ${headerName}`);
  43. },
  44. rangeChunkSize: 64
  45. })).toEqual({
  46. allowRangeRequests: false,
  47. suggestedLength: 8
  48. });
  49. expect((0, _network_utils.validateRangeRequestCapabilities)({
  50. disableRange: false,
  51. isHttp: false,
  52. getResponseHeader: headerName => {
  53. if (headerName === "Content-Length") {
  54. return 8;
  55. }
  56. throw new Error(`Unexpected headerName: ${headerName}`);
  57. },
  58. rangeChunkSize: 64
  59. })).toEqual({
  60. allowRangeRequests: false,
  61. suggestedLength: 8
  62. });
  63. });
  64. it("rejects invalid Accept-Ranges header values", function () {
  65. expect((0, _network_utils.validateRangeRequestCapabilities)({
  66. disableRange: false,
  67. isHttp: true,
  68. getResponseHeader: headerName => {
  69. if (headerName === "Accept-Ranges") {
  70. return "none";
  71. } else if (headerName === "Content-Length") {
  72. return 8;
  73. }
  74. throw new Error(`Unexpected headerName: ${headerName}`);
  75. },
  76. rangeChunkSize: 64
  77. })).toEqual({
  78. allowRangeRequests: false,
  79. suggestedLength: 8
  80. });
  81. });
  82. it("rejects invalid Content-Encoding header values", function () {
  83. expect((0, _network_utils.validateRangeRequestCapabilities)({
  84. disableRange: false,
  85. isHttp: true,
  86. getResponseHeader: headerName => {
  87. if (headerName === "Accept-Ranges") {
  88. return "bytes";
  89. } else if (headerName === "Content-Encoding") {
  90. return "gzip";
  91. } else if (headerName === "Content-Length") {
  92. return 8;
  93. }
  94. throw new Error(`Unexpected headerName: ${headerName}`);
  95. },
  96. rangeChunkSize: 64
  97. })).toEqual({
  98. allowRangeRequests: false,
  99. suggestedLength: 8
  100. });
  101. });
  102. it("rejects invalid Content-Length header values", function () {
  103. expect((0, _network_utils.validateRangeRequestCapabilities)({
  104. disableRange: false,
  105. isHttp: true,
  106. getResponseHeader: headerName => {
  107. if (headerName === "Accept-Ranges") {
  108. return "bytes";
  109. } else if (headerName === "Content-Encoding") {
  110. return null;
  111. } else if (headerName === "Content-Length") {
  112. return "eight";
  113. }
  114. throw new Error(`Unexpected headerName: ${headerName}`);
  115. },
  116. rangeChunkSize: 64
  117. })).toEqual({
  118. allowRangeRequests: false,
  119. suggestedLength: undefined
  120. });
  121. });
  122. it("rejects file sizes that are too small for range requests", function () {
  123. expect((0, _network_utils.validateRangeRequestCapabilities)({
  124. disableRange: false,
  125. isHttp: true,
  126. getResponseHeader: headerName => {
  127. if (headerName === "Accept-Ranges") {
  128. return "bytes";
  129. } else if (headerName === "Content-Encoding") {
  130. return null;
  131. } else if (headerName === "Content-Length") {
  132. return 8;
  133. }
  134. throw new Error(`Unexpected headerName: ${headerName}`);
  135. },
  136. rangeChunkSize: 64
  137. })).toEqual({
  138. allowRangeRequests: false,
  139. suggestedLength: 8
  140. });
  141. });
  142. it("accepts file sizes large enough for range requests", function () {
  143. expect((0, _network_utils.validateRangeRequestCapabilities)({
  144. disableRange: false,
  145. isHttp: true,
  146. getResponseHeader: headerName => {
  147. if (headerName === "Accept-Ranges") {
  148. return "bytes";
  149. } else if (headerName === "Content-Encoding") {
  150. return null;
  151. } else if (headerName === "Content-Length") {
  152. return 8192;
  153. }
  154. throw new Error(`Unexpected headerName: ${headerName}`);
  155. },
  156. rangeChunkSize: 64
  157. })).toEqual({
  158. allowRangeRequests: true,
  159. suggestedLength: 8192
  160. });
  161. });
  162. });
  163. describe("extractFilenameFromHeader", function () {
  164. it("returns null when content disposition header is blank", function () {
  165. expect((0, _network_utils.extractFilenameFromHeader)(headerName => {
  166. if (headerName === "Content-Disposition") {
  167. return null;
  168. }
  169. throw new Error(`Unexpected headerName: ${headerName}`);
  170. })).toBeNull();
  171. expect((0, _network_utils.extractFilenameFromHeader)(headerName => {
  172. if (headerName === "Content-Disposition") {
  173. return undefined;
  174. }
  175. throw new Error(`Unexpected headerName: ${headerName}`);
  176. })).toBeNull();
  177. expect((0, _network_utils.extractFilenameFromHeader)(headerName => {
  178. if (headerName === "Content-Disposition") {
  179. return "";
  180. }
  181. throw new Error(`Unexpected headerName: ${headerName}`);
  182. })).toBeNull();
  183. });
  184. it("gets the filename from the response header", function () {
  185. expect((0, _network_utils.extractFilenameFromHeader)(headerName => {
  186. if (headerName === "Content-Disposition") {
  187. return "inline";
  188. }
  189. throw new Error(`Unexpected headerName: ${headerName}`);
  190. })).toBeNull();
  191. expect((0, _network_utils.extractFilenameFromHeader)(headerName => {
  192. if (headerName === "Content-Disposition") {
  193. return "attachment";
  194. }
  195. throw new Error(`Unexpected headerName: ${headerName}`);
  196. })).toBeNull();
  197. expect((0, _network_utils.extractFilenameFromHeader)(headerName => {
  198. if (headerName === "Content-Disposition") {
  199. return 'attachment; filename="filename.pdf"';
  200. }
  201. throw new Error(`Unexpected headerName: ${headerName}`);
  202. })).toEqual("filename.pdf");
  203. expect((0, _network_utils.extractFilenameFromHeader)(headerName => {
  204. if (headerName === "Content-Disposition") {
  205. return 'attachment; filename="filename.pdf and spaces.pdf"';
  206. }
  207. throw new Error(`Unexpected headerName: ${headerName}`);
  208. })).toEqual("filename.pdf and spaces.pdf");
  209. expect((0, _network_utils.extractFilenameFromHeader)(headerName => {
  210. if (headerName === "Content-Disposition") {
  211. return 'attachment; filename="tl;dr.pdf"';
  212. }
  213. throw new Error(`Unexpected headerName: ${headerName}`);
  214. })).toEqual("tl;dr.pdf");
  215. expect((0, _network_utils.extractFilenameFromHeader)(headerName => {
  216. if (headerName === "Content-Disposition") {
  217. return "attachment; filename=filename.pdf";
  218. }
  219. throw new Error(`Unexpected headerName: ${headerName}`);
  220. })).toEqual("filename.pdf");
  221. expect((0, _network_utils.extractFilenameFromHeader)(headerName => {
  222. if (headerName === "Content-Disposition") {
  223. return "attachment; filename=filename.pdf someotherparam";
  224. }
  225. throw new Error(`Unexpected headerName: ${headerName}`);
  226. })).toEqual("filename.pdf");
  227. expect((0, _network_utils.extractFilenameFromHeader)(headerName => {
  228. if (headerName === "Content-Disposition") {
  229. return 'attachment; filename="%e4%b8%ad%e6%96%87.pdf"';
  230. }
  231. throw new Error(`Unexpected headerName: ${headerName}`);
  232. })).toEqual("中文.pdf");
  233. expect((0, _network_utils.extractFilenameFromHeader)(headerName => {
  234. if (headerName === "Content-Disposition") {
  235. return 'attachment; filename="100%.pdf"';
  236. }
  237. throw new Error(`Unexpected headerName: ${headerName}`);
  238. })).toEqual("100%.pdf");
  239. });
  240. it("gets the filename from the response header (RFC 6266)", function () {
  241. expect((0, _network_utils.extractFilenameFromHeader)(headerName => {
  242. if (headerName === "Content-Disposition") {
  243. return "attachment; filename*=filename.pdf";
  244. }
  245. throw new Error(`Unexpected headerName: ${headerName}`);
  246. })).toEqual("filename.pdf");
  247. expect((0, _network_utils.extractFilenameFromHeader)(headerName => {
  248. if (headerName === "Content-Disposition") {
  249. return "attachment; filename*=''filename.pdf";
  250. }
  251. throw new Error(`Unexpected headerName: ${headerName}`);
  252. })).toEqual("filename.pdf");
  253. expect((0, _network_utils.extractFilenameFromHeader)(headerName => {
  254. if (headerName === "Content-Disposition") {
  255. return "attachment; filename*=utf-8''filename.pdf";
  256. }
  257. throw new Error(`Unexpected headerName: ${headerName}`);
  258. })).toEqual("filename.pdf");
  259. expect((0, _network_utils.extractFilenameFromHeader)(headerName => {
  260. if (headerName === "Content-Disposition") {
  261. return "attachment; filename=no.pdf; filename*=utf-8''filename.pdf";
  262. }
  263. throw new Error(`Unexpected headerName: ${headerName}`);
  264. })).toEqual("filename.pdf");
  265. expect((0, _network_utils.extractFilenameFromHeader)(headerName => {
  266. if (headerName === "Content-Disposition") {
  267. return "attachment; filename*=utf-8''filename.pdf; filename=no.pdf";
  268. }
  269. throw new Error(`Unexpected headerName: ${headerName}`);
  270. })).toEqual("filename.pdf");
  271. });
  272. it("gets the filename from the response header (RFC 2231)", function () {
  273. expect((0, _network_utils.extractFilenameFromHeader)(headerName => {
  274. if (headerName === "Content-Disposition") {
  275. return "attachment; filename*0=filename; filename*1=.pdf";
  276. }
  277. throw new Error(`Unexpected headerName: ${headerName}`);
  278. })).toEqual("filename.pdf");
  279. });
  280. it("only extracts filename with pdf extension", function () {
  281. expect((0, _network_utils.extractFilenameFromHeader)(headerName => {
  282. if (headerName === "Content-Disposition") {
  283. return 'attachment; filename="filename.png"';
  284. }
  285. throw new Error(`Unexpected headerName: ${headerName}`);
  286. })).toBeNull();
  287. });
  288. it("extension validation is case insensitive", function () {
  289. expect((0, _network_utils.extractFilenameFromHeader)(headerName => {
  290. if (headerName === "Content-Disposition") {
  291. return 'form-data; name="fieldName"; filename="file.PdF"';
  292. }
  293. throw new Error(`Unexpected headerName: ${headerName}`);
  294. })).toEqual("file.PdF");
  295. });
  296. });
  297. describe("createResponseStatusError", function () {
  298. it("handles missing PDF file responses", function () {
  299. expect((0, _network_utils.createResponseStatusError)(404, "https://foo.com/bar.pdf")).toEqual(new _util.MissingPDFException('Missing PDF "https://foo.com/bar.pdf".'));
  300. expect((0, _network_utils.createResponseStatusError)(0, "file://foo.pdf")).toEqual(new _util.MissingPDFException('Missing PDF "file://foo.pdf".'));
  301. });
  302. it("handles unexpected responses", function () {
  303. 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".'));
  304. 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".'));
  305. });
  306. });
  307. describe("validateResponseStatus", function () {
  308. it("accepts valid response statuses", function () {
  309. expect((0, _network_utils.validateResponseStatus)(200)).toEqual(true);
  310. expect((0, _network_utils.validateResponseStatus)(206)).toEqual(true);
  311. });
  312. it("rejects invalid response statuses", function () {
  313. expect((0, _network_utils.validateResponseStatus)(302)).toEqual(false);
  314. expect((0, _network_utils.validateResponseStatus)(404)).toEqual(false);
  315. expect((0, _network_utils.validateResponseStatus)(null)).toEqual(false);
  316. expect((0, _network_utils.validateResponseStatus)(undefined)).toEqual(false);
  317. });
  318. });
  319. });