123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- 'use strict';
- /**
- * This is an helper function to transform the input into a binary string.
- * The transformation is normaly handled by JSZip.
- * @param {String|ArrayBuffer} input the input to convert.
- * @return {String} the binary string.
- */
- function toString(input) {
- var result = "",
- i, len, isArray = (typeof input !== "string");
- if (isArray) {
- input = new Uint8Array(input);
- }
- for (i = 0, len = input.length; i < len; i++) {
- result += String.fromCharCode(
- (isArray ? input[i] : input.charCodeAt(i)) % 0xFF
- );
- }
- return result;
- }
- QUnit.module("callback");
- QUnit.test("JSZipUtils.getBinaryContent, text, 200 OK", function (assert) {
- var done = assert.async();
- var p = JSZipUtils.getBinaryContent("ref/amount.txt", function (err, data) {
- assert.equal(err, null, "no error");
- assert.equal(toString(data), "\xe2\x82\xac\x31\x35\x0a", "The content has been fetched");
- done();
- });
- assert.strictEqual(p, undefined, 'not return promise');
- });
- QUnit.test("JSZipUtils.getBinaryContent, image, 200 OK", function (assert) {
- var done = assert.async();
- var p = JSZipUtils.getBinaryContent("ref/smile.gif", function (err, data) {
- assert.equal(err, null, "no error");
- assert.equal(toString(data).indexOf("\x47\x49\x46\x38\x37\x61"), 0, "The content has been fetched");
- done();
- });
- assert.strictEqual(p, undefined, 'not return promise');
- });
- QUnit.test("JSZipUtils.getBinaryContent, 404 NOT FOUND", function (assert) {
- var done = assert.async();
- var p = JSZipUtils.getBinaryContent("ref/nothing", function (err, data) {
- assert.equal(data, null, "no error");
- assert.ok(err instanceof Error, "The error is an Error");
- done();
- });
- assert.strictEqual(p, undefined, 'not return promise');
- });
- QUnit.module("options={callback}");
- QUnit.test("JSZipUtils.getBinaryContent, text, 200 OK", function (assert) {
- var done = assert.async();
- var p = JSZipUtils.getBinaryContent("ref/amount.txt", {
- callback: function (err, data) {
- assert.equal(err, null, "no error");
- assert.equal(toString(data), "\xe2\x82\xac\x31\x35\x0a", "The content has been fetched");
- done();
- }
- });
- assert.strictEqual(p, undefined, 'not return promise');
- });
- QUnit.test("JSZipUtils.getBinaryContent, image, 200 OK", function (assert) {
- var done = assert.async();
- var p = JSZipUtils.getBinaryContent("ref/smile.gif", {
- callback: function (err, data) {
- assert.equal(err, null, "no error");
- assert.equal(toString(data).indexOf("\x47\x49\x46\x38\x37\x61"), 0, "The content has been fetched");
- done();
- }
- });
- assert.strictEqual(p, undefined, 'not return promise');
- });
- QUnit.test("JSZipUtils.getBinaryContent, 404 NOT FOUND", function (assert) {
- var done = assert.async();
- var p = JSZipUtils.getBinaryContent("ref/nothing", {
- callback: function (err, data) {
- assert.equal(data, null, "no error");
- assert.ok(err instanceof Error, "The error is an Error");
- done();
- }
- });
- assert.strictEqual(p, undefined, 'not return promise');
- });
- // Guard Promise tests for IE
- if (typeof Promise === "undefined") {
- QUnit.module("Promises");
- QUnit.skip("Skipping promise tests");
- } else {
- QUnit.module("Promise (no parameters)");
- QUnit.test("JSZipUtils.getBinaryContent amount, text, 200 OK", function (assert) {
- var done = assert.async();
- JSZipUtils.getBinaryContent("ref/amount.txt").then(function (data) {
- assert.equal(toString(data), "\xe2\x82\xac\x31\x35\x0a", "The content has been fetched");
- done();
- })
- .catch(function (err) {
- assert.equal(err, null, "no error");
- done();
- });
- });
- QUnit.test("JSZipUtils.getBinaryContent smile, image, 200 OK", function (assert) {
- var done = assert.async();
- JSZipUtils.getBinaryContent("ref/smile.gif").then(function (data) {
- assert.equal(toString(data).indexOf("\x47\x49\x46\x38\x37\x61"), 0, "The content has been fetched");
- done();
- }).catch(function (err) {
- assert.equal(err, null, "no error");
- done();
- });
- });
- QUnit.test("JSZipUtils.getBinaryContent nothing, 404 NOT FOUND", function (assert) {
- var done = assert.async();
- JSZipUtils.getBinaryContent("ref/nothing").then(function (data) {
- assert.equal(data, null, "no error");
- done();
- }).catch(function (err) {
- assert.ok(err instanceof Error, "The error is an Error");
- done();
- });
- });
- QUnit.module("Promise, options={}");
- QUnit.test("JSZipUtils.getBinaryContent amount, text, 200 OK", function (assert) {
- var done = assert.async();
- JSZipUtils.getBinaryContent("ref/amount.txt", {}).then(function (data) {
- assert.equal(toString(data), "\xe2\x82\xac\x31\x35\x0a", "The content has been fetched");
- done();
- })
- .catch(function (err) {
- assert.equal(err, null, "no error");
- done();
- });
- });
- QUnit.test("JSZipUtils.getBinaryContent smile, image, 200 OK", function (assert) {
- var done = assert.async();
- JSZipUtils.getBinaryContent("ref/smile.gif", {}).then(function (data) {
- assert.equal(toString(data).indexOf("\x47\x49\x46\x38\x37\x61"), 0, "The content has been fetched");
- done();
- }).catch(function (err) {
- assert.equal(err, null, "no error");
- done();
- });
- });
- QUnit.test("JSZipUtils.getBinaryContent nothing, 404 NOT FOUND", function (assert) {
- var done = assert.async();
- JSZipUtils.getBinaryContent("ref/nothing", {}).then(function (data) {
- assert.equal(data, null, "no error");
- done();
- }).catch(function (err) {
- assert.ok(err instanceof Error, "The error is an Error");
- done();
- });
- });
- QUnit.module("Promise, options={progress}");
- QUnit.test("JSZipUtils.getBinaryContent amount, text, 200 OK", function (assert) {
- var done = assert.async();
- var progress = assert.async();
- JSZipUtils.getBinaryContent("ref/amount.txt", { progress: function(e){
- assert.ok(true, 'progress to be called');
- assert.strictEqual(e.total, 6, 'total');
- progress();
- }}).then(function (data) {
- assert.equal(toString(data), "\xe2\x82\xac\x31\x35\x0a", "The content has been fetched");
- done();
- })
- .catch(function (err) {
- assert.equal(err, null, "no error");
- done();
- });
- });
- QUnit.test("JSZipUtils.getBinaryContent smile, image, 200 OK", function (assert) {
- var done = assert.async();
- var progress = assert.async();
- JSZipUtils.getBinaryContent("ref/smile.gif", { progress: function(e){
- assert.ok(true, 'progress to be called');
- assert.strictEqual(e.total, 41, 'total');
- progress();
- }}).then(function (data) {
- assert.equal(toString(data).indexOf("\x47\x49\x46\x38\x37\x61"), 0, "The content has been fetched");
- done();
- }).catch(function (err) {
- assert.equal(err, null, "no error");
- done();
- });
- });
- QUnit.test("JSZipUtils.getBinaryContent nothing, 404 NOT FOUND", function (assert) {
- var done = assert.async();
- JSZipUtils.getBinaryContent("ref/nothing", { progress: function(e){
- }}).then(function (data) {
- assert.equal(data, null, "no error");
- done();
- }).catch(function (err) {
- assert.ok(err instanceof Error, "The error is an Error");
- done();
- });
- });
- } // Promise tests
- // enforcing Stuk's coding style
- // vim: set shiftwidth=4 softtabstop=4:
|