From 4714e79dc153b9d7cb0fcdb551893aba398cf216 Mon Sep 17 00:00:00 2001 From: Bert De Block Date: Wed, 26 Jan 2022 03:50:43 +0100 Subject: [PATCH] Avoid mutating upload options (#676) Closes #607 --- addon/system/upload.js | 10 +++++++--- tests/unit/upload-file-test.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/addon/system/upload.js b/addon/system/upload.js index 79dab4be..0a10f6de 100644 --- a/addon/system/upload.js +++ b/addon/system/upload.js @@ -5,13 +5,17 @@ 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'; @@ -19,8 +23,8 @@ function normalizeOptions(file, url, options) { 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) { diff --git a/tests/unit/upload-file-test.js b/tests/unit/upload-file-test.js index 00436eaf..dbf4b1b1 100644 --- a/tests/unit/upload-file-test.js +++ b/tests/unit/upload-file-test.js @@ -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', + }, + }); + }); });