network_utils_spec.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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('createResponseStatusError', function () {
  130. it('handles missing PDF file responses', function () {
  131. expect((0, _network_utils.createResponseStatusError)(404, 'https://foo.com/bar.pdf')).toEqual(new _util.MissingPDFException('Missing PDF "https://foo.com/bar.pdf".'));
  132. expect((0, _network_utils.createResponseStatusError)(0, 'file://foo.pdf')).toEqual(new _util.MissingPDFException('Missing PDF "file://foo.pdf".'));
  133. });
  134. it('handles unexpected responses', function () {
  135. 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".'));
  136. 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".'));
  137. });
  138. });
  139. describe('validateResponseStatus', function () {
  140. it('accepts valid response statuses', function () {
  141. expect((0, _network_utils.validateResponseStatus)(200)).toEqual(true);
  142. expect((0, _network_utils.validateResponseStatus)(206)).toEqual(true);
  143. });
  144. it('rejects invalid response statuses', function () {
  145. expect((0, _network_utils.validateResponseStatus)(302)).toEqual(false);
  146. expect((0, _network_utils.validateResponseStatus)(404)).toEqual(false);
  147. expect((0, _network_utils.validateResponseStatus)(null)).toEqual(false);
  148. expect((0, _network_utils.validateResponseStatus)(undefined)).toEqual(false);
  149. });
  150. });
  151. });