test.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. 'use strict';
  2. /**
  3. * This is an helper function to transform the input into a binary string.
  4. * The transformation is normaly handled by JSZip.
  5. * @param {String|ArrayBuffer} input the input to convert.
  6. * @return {String} the binary string.
  7. */
  8. function toString(input) {
  9. var result = "",
  10. i, len, isArray = (typeof input !== "string");
  11. if (isArray) {
  12. input = new Uint8Array(input);
  13. }
  14. for (i = 0, len = input.length; i < len; i++) {
  15. result += String.fromCharCode(
  16. (isArray ? input[i] : input.charCodeAt(i)) % 0xFF
  17. );
  18. }
  19. return result;
  20. }
  21. QUnit.module("callback");
  22. QUnit.test("JSZipUtils.getBinaryContent, text, 200 OK", function (assert) {
  23. var done = assert.async();
  24. var p = JSZipUtils.getBinaryContent("ref/amount.txt", function (err, data) {
  25. assert.equal(err, null, "no error");
  26. assert.equal(toString(data), "\xe2\x82\xac\x31\x35\x0a", "The content has been fetched");
  27. done();
  28. });
  29. assert.strictEqual(p, undefined, 'not return promise');
  30. });
  31. QUnit.test("JSZipUtils.getBinaryContent, image, 200 OK", function (assert) {
  32. var done = assert.async();
  33. var p = JSZipUtils.getBinaryContent("ref/smile.gif", function (err, data) {
  34. assert.equal(err, null, "no error");
  35. assert.equal(toString(data).indexOf("\x47\x49\x46\x38\x37\x61"), 0, "The content has been fetched");
  36. done();
  37. });
  38. assert.strictEqual(p, undefined, 'not return promise');
  39. });
  40. QUnit.test("JSZipUtils.getBinaryContent, 404 NOT FOUND", function (assert) {
  41. var done = assert.async();
  42. var p = JSZipUtils.getBinaryContent("ref/nothing", function (err, data) {
  43. assert.equal(data, null, "no error");
  44. assert.ok(err instanceof Error, "The error is an Error");
  45. done();
  46. });
  47. assert.strictEqual(p, undefined, 'not return promise');
  48. });
  49. QUnit.module("options={callback}");
  50. QUnit.test("JSZipUtils.getBinaryContent, text, 200 OK", function (assert) {
  51. var done = assert.async();
  52. var p = JSZipUtils.getBinaryContent("ref/amount.txt", {
  53. callback: function (err, data) {
  54. assert.equal(err, null, "no error");
  55. assert.equal(toString(data), "\xe2\x82\xac\x31\x35\x0a", "The content has been fetched");
  56. done();
  57. }
  58. });
  59. assert.strictEqual(p, undefined, 'not return promise');
  60. });
  61. QUnit.test("JSZipUtils.getBinaryContent, image, 200 OK", function (assert) {
  62. var done = assert.async();
  63. var p = JSZipUtils.getBinaryContent("ref/smile.gif", {
  64. callback: function (err, data) {
  65. assert.equal(err, null, "no error");
  66. assert.equal(toString(data).indexOf("\x47\x49\x46\x38\x37\x61"), 0, "The content has been fetched");
  67. done();
  68. }
  69. });
  70. assert.strictEqual(p, undefined, 'not return promise');
  71. });
  72. QUnit.test("JSZipUtils.getBinaryContent, 404 NOT FOUND", function (assert) {
  73. var done = assert.async();
  74. var p = JSZipUtils.getBinaryContent("ref/nothing", {
  75. callback: function (err, data) {
  76. assert.equal(data, null, "no error");
  77. assert.ok(err instanceof Error, "The error is an Error");
  78. done();
  79. }
  80. });
  81. assert.strictEqual(p, undefined, 'not return promise');
  82. });
  83. // Guard Promise tests for IE
  84. if (typeof Promise === "undefined") {
  85. QUnit.module("Promises");
  86. QUnit.skip("Skipping promise tests");
  87. } else {
  88. QUnit.module("Promise (no parameters)");
  89. QUnit.test("JSZipUtils.getBinaryContent amount, text, 200 OK", function (assert) {
  90. var done = assert.async();
  91. JSZipUtils.getBinaryContent("ref/amount.txt").then(function (data) {
  92. assert.equal(toString(data), "\xe2\x82\xac\x31\x35\x0a", "The content has been fetched");
  93. done();
  94. })
  95. .catch(function (err) {
  96. assert.equal(err, null, "no error");
  97. done();
  98. });
  99. });
  100. QUnit.test("JSZipUtils.getBinaryContent smile, image, 200 OK", function (assert) {
  101. var done = assert.async();
  102. JSZipUtils.getBinaryContent("ref/smile.gif").then(function (data) {
  103. assert.equal(toString(data).indexOf("\x47\x49\x46\x38\x37\x61"), 0, "The content has been fetched");
  104. done();
  105. }).catch(function (err) {
  106. assert.equal(err, null, "no error");
  107. done();
  108. });
  109. });
  110. QUnit.test("JSZipUtils.getBinaryContent nothing, 404 NOT FOUND", function (assert) {
  111. var done = assert.async();
  112. JSZipUtils.getBinaryContent("ref/nothing").then(function (data) {
  113. assert.equal(data, null, "no error");
  114. done();
  115. }).catch(function (err) {
  116. assert.ok(err instanceof Error, "The error is an Error");
  117. done();
  118. });
  119. });
  120. QUnit.module("Promise, options={}");
  121. QUnit.test("JSZipUtils.getBinaryContent amount, text, 200 OK", function (assert) {
  122. var done = assert.async();
  123. JSZipUtils.getBinaryContent("ref/amount.txt", {}).then(function (data) {
  124. assert.equal(toString(data), "\xe2\x82\xac\x31\x35\x0a", "The content has been fetched");
  125. done();
  126. })
  127. .catch(function (err) {
  128. assert.equal(err, null, "no error");
  129. done();
  130. });
  131. });
  132. QUnit.test("JSZipUtils.getBinaryContent smile, image, 200 OK", function (assert) {
  133. var done = assert.async();
  134. JSZipUtils.getBinaryContent("ref/smile.gif", {}).then(function (data) {
  135. assert.equal(toString(data).indexOf("\x47\x49\x46\x38\x37\x61"), 0, "The content has been fetched");
  136. done();
  137. }).catch(function (err) {
  138. assert.equal(err, null, "no error");
  139. done();
  140. });
  141. });
  142. QUnit.test("JSZipUtils.getBinaryContent nothing, 404 NOT FOUND", function (assert) {
  143. var done = assert.async();
  144. JSZipUtils.getBinaryContent("ref/nothing", {}).then(function (data) {
  145. assert.equal(data, null, "no error");
  146. done();
  147. }).catch(function (err) {
  148. assert.ok(err instanceof Error, "The error is an Error");
  149. done();
  150. });
  151. });
  152. QUnit.module("Promise, options={progress}");
  153. QUnit.test("JSZipUtils.getBinaryContent amount, text, 200 OK", function (assert) {
  154. var done = assert.async();
  155. var progress = assert.async();
  156. JSZipUtils.getBinaryContent("ref/amount.txt", { progress: function(e){
  157. assert.ok(true, 'progress to be called');
  158. assert.strictEqual(e.total, 6, 'total');
  159. progress();
  160. }}).then(function (data) {
  161. assert.equal(toString(data), "\xe2\x82\xac\x31\x35\x0a", "The content has been fetched");
  162. done();
  163. })
  164. .catch(function (err) {
  165. assert.equal(err, null, "no error");
  166. done();
  167. });
  168. });
  169. QUnit.test("JSZipUtils.getBinaryContent smile, image, 200 OK", function (assert) {
  170. var done = assert.async();
  171. var progress = assert.async();
  172. JSZipUtils.getBinaryContent("ref/smile.gif", { progress: function(e){
  173. assert.ok(true, 'progress to be called');
  174. assert.strictEqual(e.total, 41, 'total');
  175. progress();
  176. }}).then(function (data) {
  177. assert.equal(toString(data).indexOf("\x47\x49\x46\x38\x37\x61"), 0, "The content has been fetched");
  178. done();
  179. }).catch(function (err) {
  180. assert.equal(err, null, "no error");
  181. done();
  182. });
  183. });
  184. QUnit.test("JSZipUtils.getBinaryContent nothing, 404 NOT FOUND", function (assert) {
  185. var done = assert.async();
  186. JSZipUtils.getBinaryContent("ref/nothing", { progress: function(e){
  187. }}).then(function (data) {
  188. assert.equal(data, null, "no error");
  189. done();
  190. }).catch(function (err) {
  191. assert.ok(err instanceof Error, "The error is an Error");
  192. done();
  193. });
  194. });
  195. } // Promise tests
  196. // enforcing Stuk's coding style
  197. // vim: set shiftwidth=4 softtabstop=4: