network_utils_spec.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 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");
  24. var _util = require("../../shared/util");
  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: function getResponseHeader(headerName) {
  39. if (headerName === 'Content-Length') {
  40. return 8;
  41. }
  42. throw new Error("Unexpected headerName: ".concat(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: function getResponseHeader(headerName) {
  53. if (headerName === 'Content-Length') {
  54. return 8;
  55. }
  56. throw new Error("Unexpected headerName: ".concat(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: function 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: ".concat(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: function 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: ".concat(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: function 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: ".concat(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: function 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: ".concat(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: function 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: ".concat(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)(function (headerName) {
  166. if (headerName === 'Content-Disposition') {
  167. return null;
  168. }
  169. throw new Error("Unexpected headerName: ".concat(headerName));
  170. })).toBeNull();
  171. expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
  172. if (headerName === 'Content-Disposition') {
  173. return undefined;
  174. }
  175. throw new Error("Unexpected headerName: ".concat(headerName));
  176. })).toBeNull();
  177. expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
  178. if (headerName === 'Content-Disposition') {
  179. return '';
  180. }
  181. throw new Error("Unexpected headerName: ".concat(headerName));
  182. })).toBeNull();
  183. });
  184. it('gets the filename from the response header', function () {
  185. expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
  186. if (headerName === 'Content-Disposition') {
  187. return 'inline';
  188. }
  189. throw new Error("Unexpected headerName: ".concat(headerName));
  190. })).toBeNull();
  191. expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
  192. if (headerName === 'Content-Disposition') {
  193. return 'attachment';
  194. }
  195. throw new Error("Unexpected headerName: ".concat(headerName));
  196. })).toBeNull();
  197. expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
  198. if (headerName === 'Content-Disposition') {
  199. return 'attachment; filename="filename.pdf"';
  200. }
  201. throw new Error("Unexpected headerName: ".concat(headerName));
  202. })).toEqual('filename.pdf');
  203. expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
  204. if (headerName === 'Content-Disposition') {
  205. return 'attachment; filename="filename.pdf and spaces.pdf"';
  206. }
  207. throw new Error("Unexpected headerName: ".concat(headerName));
  208. })).toEqual('filename.pdf and spaces.pdf');
  209. expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
  210. if (headerName === 'Content-Disposition') {
  211. return 'attachment; filename="tl;dr.pdf"';
  212. }
  213. throw new Error("Unexpected headerName: ".concat(headerName));
  214. })).toEqual('tl;dr.pdf');
  215. expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
  216. if (headerName === 'Content-Disposition') {
  217. return 'attachment; filename=filename.pdf';
  218. }
  219. throw new Error("Unexpected headerName: ".concat(headerName));
  220. })).toEqual('filename.pdf');
  221. expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
  222. if (headerName === 'Content-Disposition') {
  223. return 'attachment; filename=filename.pdf someotherparam';
  224. }
  225. throw new Error("Unexpected headerName: ".concat(headerName));
  226. })).toEqual('filename.pdf');
  227. });
  228. it('gets the filename from the response header (RFC 6266)', function () {
  229. expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
  230. if (headerName === 'Content-Disposition') {
  231. return 'attachment; filename*=filename.pdf';
  232. }
  233. throw new Error("Unexpected headerName: ".concat(headerName));
  234. })).toEqual('filename.pdf');
  235. expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
  236. if (headerName === 'Content-Disposition') {
  237. return 'attachment; filename*=\'\'filename.pdf';
  238. }
  239. throw new Error("Unexpected headerName: ".concat(headerName));
  240. })).toEqual('filename.pdf');
  241. expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
  242. if (headerName === 'Content-Disposition') {
  243. return 'attachment; filename*=utf-8\'\'filename.pdf';
  244. }
  245. throw new Error("Unexpected headerName: ".concat(headerName));
  246. })).toEqual('filename.pdf');
  247. expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
  248. if (headerName === 'Content-Disposition') {
  249. return 'attachment; filename=no.pdf; filename*=utf-8\'\'filename.pdf';
  250. }
  251. throw new Error("Unexpected headerName: ".concat(headerName));
  252. })).toEqual('filename.pdf');
  253. expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
  254. if (headerName === 'Content-Disposition') {
  255. return 'attachment; filename*=utf-8\'\'filename.pdf; filename=no.pdf';
  256. }
  257. throw new Error("Unexpected headerName: ".concat(headerName));
  258. })).toEqual('filename.pdf');
  259. });
  260. it('gets the filename from the response header (RFC 2231)', function () {
  261. expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
  262. if (headerName === 'Content-Disposition') {
  263. return 'attachment; filename*0=filename; filename*1=.pdf';
  264. }
  265. throw new Error("Unexpected headerName: ".concat(headerName));
  266. })).toEqual('filename.pdf');
  267. });
  268. it('only extracts filename with pdf extension', function () {
  269. expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
  270. if (headerName === 'Content-Disposition') {
  271. return 'attachment; filename="filename.png"';
  272. }
  273. throw new Error("Unexpected headerName: ".concat(headerName));
  274. })).toBeNull();
  275. });
  276. it('extension validation is case insensitive', function () {
  277. expect((0, _network_utils.extractFilenameFromHeader)(function (headerName) {
  278. if (headerName === 'Content-Disposition') {
  279. return 'form-data; name="fieldName"; filename="file.PdF"';
  280. }
  281. throw new Error("Unexpected headerName: ".concat(headerName));
  282. })).toEqual('file.PdF');
  283. });
  284. });
  285. describe('createResponseStatusError', function () {
  286. it('handles missing PDF file responses', function () {
  287. expect((0, _network_utils.createResponseStatusError)(404, 'https://foo.com/bar.pdf')).toEqual(new _util.MissingPDFException('Missing PDF "https://foo.com/bar.pdf".'));
  288. expect((0, _network_utils.createResponseStatusError)(0, 'file://foo.pdf')).toEqual(new _util.MissingPDFException('Missing PDF "file://foo.pdf".'));
  289. });
  290. it('handles unexpected responses', function () {
  291. 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".'));
  292. 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".'));
  293. });
  294. });
  295. describe('validateResponseStatus', function () {
  296. it('accepts valid response statuses', function () {
  297. expect((0, _network_utils.validateResponseStatus)(200)).toEqual(true);
  298. expect((0, _network_utils.validateResponseStatus)(206)).toEqual(true);
  299. });
  300. it('rejects invalid response statuses', function () {
  301. expect((0, _network_utils.validateResponseStatus)(302)).toEqual(false);
  302. expect((0, _network_utils.validateResponseStatus)(404)).toEqual(false);
  303. expect((0, _network_utils.validateResponseStatus)(null)).toEqual(false);
  304. expect((0, _network_utils.validateResponseStatus)(undefined)).toEqual(false);
  305. });
  306. });
  307. });