network_utils_spec.js 10.0 KB

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