Skip to content

Commit

Permalink
Avoid mutating upload options (#676)
Browse files Browse the repository at this point in the history
Closes #607
  • Loading branch information
bertdeblock authored Jan 26, 2022
1 parent 973d151 commit 4714e79
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
10 changes: 7 additions & 3 deletions addon/system/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,26 @@ import { buildWaiter } from '@ember/test-waiters';

const uploadWaiter = buildWaiter('ember-file-upload:upload');

function clone(object) {
return object ? { ...object } : {};
}

function normalizeOptions(file, url, options) {
if (typeof url === 'object') {
options = url;
url = null;
}

options = options || {};
options = clone(options);

options.url = options.url || url;
options.method = options.method || 'POST';
options.accepts = options.accepts || ['application/json', 'text/javascript'];
if (!Object.prototype.hasOwnProperty.call(options, 'contentType')) {
options.contentType = file.type;
}
options.headers = options.headers || {};
options.data = options.data || {};
options.headers = clone(options.headers);
options.data = clone(options.data);
options.fileKey = options.fileKey || 'file';

if (options.headers.Accept == null) {
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/upload-file-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,34 @@ module('Unit | UploadFile', function (hooks) {

await file.upload('/image');
});

test('it does not mutate the provided options', async function (assert) {
this.server.post('/image', () => {});

const file = UploadFile.fromBlob(
new Blob(['My Test File'], { type: 'text' })
);

const options = {
accepts: ['foo'],
data: {
bar: 'bar',
},
headers: {
baz: 'baz',
},
};

await file.upload('/image', options);

assert.deepEqual(options, {
accepts: ['foo'],
data: {
bar: 'bar',
},
headers: {
baz: 'baz',
},
});
});
});

0 comments on commit 4714e79

Please sign in to comment.