Skip to content

Commit a4d3d46

Browse files
authored
Merge 06e38d9 into 782c057
2 parents 782c057 + 06e38d9 commit a4d3d46

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

src/RESTController.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -169,24 +169,24 @@ const RESTController = {
169169
headers[key] = customHeaders[key];
170170
}
171171

172-
function handleProgress(type, event) {
173-
if (options && typeof options.progress === 'function') {
172+
if (options && typeof options.progress === 'function') {
173+
const handleProgress = function (type, event) {
174174
if (event.lengthComputable) {
175175
options.progress(event.loaded / event.total, event.loaded, event.total, { type });
176176
} else {
177177
options.progress(null, null, null, { type });
178178
}
179-
}
180-
}
181-
182-
xhr.onprogress = event => {
183-
handleProgress('download', event);
184-
};
179+
};
185180

186-
if (xhr.upload) {
187-
xhr.upload.onprogress = event => {
188-
handleProgress('upload', event);
181+
xhr.onprogress = event => {
182+
handleProgress('download', event);
189183
};
184+
185+
if (xhr.upload) {
186+
xhr.upload.onprogress = event => {
187+
handleProgress('upload', event);
188+
};
189+
}
190190
}
191191

192192
xhr.open(method, url, true);

src/__tests__/RESTController-test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,20 @@ describe('RESTController', () => {
570570
);
571571
});
572572

573+
it('does not set upload progress listener when callback is not provided to avoid CORS pre-flight', () => {
574+
const xhr = {
575+
setRequestHeader: jest.fn(),
576+
open: jest.fn(),
577+
upload: jest.fn(),
578+
send: jest.fn(),
579+
};
580+
RESTController._setXHR(function () {
581+
return xhr;
582+
});
583+
RESTController.ajax('POST', 'users', {});
584+
expect(xhr.upload.onprogress).toBeUndefined();
585+
});
586+
573587
it('does not upload progress when total is uncomputable', done => {
574588
const xhr = mockXHR([{ status: 200, response: { success: true } }], {
575589
progress: {

src/__tests__/test_helpers/mockXHR.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,14 @@ function mockXHR(results, options = {}) {
3535
this.readyState = 4;
3636
attempts++;
3737
this.onreadystatechange();
38-
this.onprogress(options.progress);
39-
this.upload.onprogress(options.progress);
38+
39+
if (typeof this.onprogress === 'function') {
40+
this.onprogress(options.progress);
41+
}
42+
43+
if (typeof this.upload.onprogress === 'function') {
44+
this.upload.onprogress(options.progress);
45+
}
4046
},
4147
};
4248
return XHR;

0 commit comments

Comments
 (0)