Skip to content

Commit 195ce4b

Browse files
committed
fix: undefined query parameter will not crash the lib
1 parent bc3f8ee commit 195ce4b

File tree

2 files changed

+41
-20
lines changed

2 files changed

+41
-20
lines changed

src/resumable.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -699,10 +699,14 @@
699699
* Abort current upload
700700
* @function
701701
*/
702-
abort: function () {
702+
abort: function (reset) {
703703
this.currentSpeed = 0;
704704
this.averageSpeed = 0;
705-
each(this.chunks, function (c) {
705+
var chunks = this.chunks;
706+
if (reset) {
707+
this.chunks = [];
708+
}
709+
each(chunks, function (c) {
706710
if (c.status() === 'uploading') {
707711
c.abort();
708712
this.resumableObj.uploadNextChunk();
@@ -732,10 +736,9 @@
732736
* @function
733737
*/
734738
bootstrap: function () {
735-
this.abort();
739+
this.abort(true);
736740
this.error = false;
737741
// Rebuild stack of chunks from file
738-
this.chunks = [];
739742
this._prevProgress = 0;
740743
var round = this.resumableObj.opts.forceChunkSize ? Math.ceil : Math.floor;
741744
var chunks = Math.max(
@@ -1267,18 +1270,21 @@
12671270
* @param {Object=} context Object to become context (`this`) for the iterator function.
12681271
*/
12691272
function each(obj, callback, context) {
1273+
if (!obj) {
1274+
return ;
1275+
}
12701276
var key;
12711277
// Is Array?
12721278
if (typeof(obj.length) !== 'undefined') {
12731279
for (key = 0; key < obj.length; key++) {
12741280
if (callback.call(context, obj[key], key) === false) {
1275-
return;
1281+
return ;
12761282
}
12771283
}
12781284
} else {
12791285
for (key in obj) {
12801286
if (obj.hasOwnProperty(key) && callback.call(context, obj[key], key) === false) {
1281-
return;
1287+
return ;
12821288
}
12831289
}
12841290
}

test/uploadSpec.js

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,37 @@ describe('upload file', function() {
3131
xhr.restore();
3232
});
3333

34-
it('should test simple file upload with testing', function() {
35-
resumable.addFile(new Blob(['file part']));
36-
var file = resumable.files[0];
37-
expect(file.isComplete()).toBeFalsy();
38-
expect(file.isUploading()).toBeFalsy();
39-
expect(file.chunks.length).toBe(1);
40-
expect(file.progress()).toBe(0);
34+
it('should pass query params', function() {
35+
resumable.opts.query = {};
36+
resumable.opts.target = 'file';
37+
resumable.addFile(new Blob(['123']));
4138
resumable.upload();
4239
expect(requests.length).toBe(1);
43-
expect(file.isComplete()).toBeFalsy();
44-
expect(file.isUploading()).toBeTruthy();
45-
requests[0].respond(200);
46-
expect(file.isComplete()).toBeTruthy();
47-
expect(file.isUploading()).toBeFalsy();
48-
expect(file.progress()).toBe(1);
49-
expect(resumable.progress()).toBe(1);
40+
expect(requests[0].url).toContain('file');
41+
42+
resumable.opts.query = {a:1};
43+
resumable.files[0].retry();
44+
expect(requests.length).toBe(2);
45+
expect(requests[1].url).toContain('file');
46+
expect(requests[1].url).toContain('a=1');
47+
48+
resumable.opts.query = function (file, chunk) {
49+
expect(file).toBe(resumable.files[0]);
50+
expect(chunk).toBe(resumable.files[0].chunks[0]);
51+
return {b:2};
52+
};
53+
resumable.files[0].retry();
54+
expect(requests.length).toBe(3);
55+
expect(requests[2].url).toContain('file');
56+
expect(requests[2].url).toContain('b=2');
57+
expect(requests[2].url).not.toContain('a=1');
58+
59+
resumable.opts.query = undefined;
60+
resumable.files[0].retry();
61+
expect(requests.length).toBe(4);
62+
expect(requests[3].url).toContain('file');
63+
expect(requests[3].url).not.toContain('a=1');
64+
expect(requests[3].url).not.toContain('b=2');
5065
});
5166

5267
it('should track file upload status with lots of chunks', function() {

0 commit comments

Comments
 (0)