From b1f3c9f85afa4993ec4607f4bfd1b70465766c72 Mon Sep 17 00:00:00 2001 From: Abraham Haskins Date: Fri, 7 May 2021 15:36:19 -0500 Subject: [PATCH 1/6] Fix storage emulator env formatting --- src/storage/storage.ts | 11 ++++++++++- test/unit/storage/storage.spec.ts | 8 +++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/storage/storage.ts b/src/storage/storage.ts index aabf9dd30b..7bac81ff46 100644 --- a/src/storage/storage.ts +++ b/src/storage/storage.ts @@ -49,7 +49,16 @@ export class Storage implements StorageInterface { } if (!process.env.STORAGE_EMULATOR_HOST && process.env.FIREBASE_STORAGE_EMULATOR_HOST) { - process.env.STORAGE_EMULATOR_HOST = process.env.FIREBASE_STORAGE_EMULATOR_HOST; + const firebaseStorageEmulatorHost = process.env.FIREBASE_STORAGE_EMULATOR_HOST; + + if (firebaseStorageEmulatorHost.match(/https?:\/\//)) { + throw new FirebaseError({ + code: 'storage/invalid-emulator-host', + message: 'FIREBASE_STORAGE_EMULATOR_HOST should not contain a protocol (http or https).', + }); + } + + process.env.STORAGE_EMULATOR_HOST = `http://${process.env.FIREBASE_STORAGE_EMULATOR_HOST}`; } let storage: typeof StorageClient; diff --git a/test/unit/storage/storage.spec.ts b/test/unit/storage/storage.spec.ts index 69ea28b217..7595312602 100644 --- a/test/unit/storage/storage.spec.ts +++ b/test/unit/storage/storage.spec.ts @@ -123,9 +123,11 @@ describe('Storage', () => { }); it('sets STORAGE_EMULATOR_HOST if FIREBASE_STORAGE_EMULATOR_HOST is set', () => { - new Storage(mockApp); - - expect(process.env.STORAGE_EMULATOR_HOST).to.equal(EMULATOR_HOST); + expect(process.env.STORAGE_EMULATOR_HOST).to.equal(`http://${EMULATOR_HOST}`); + }); + + it('sets STORAGE_EMULATOR_HOST if FIREBASE_STORAGE_EMULATOR_HOST is set', () => { + expect(() => new Storage(mockApp)).to.throw; }); after(() => { From 03845b15c691db575e1eb39d5cd19e5dc7687c44 Mon Sep 17 00:00:00 2001 From: Abraham Haskins Date: Fri, 7 May 2021 15:37:14 -0500 Subject: [PATCH 2/6] Repair test --- test/unit/storage/storage.spec.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/test/unit/storage/storage.spec.ts b/test/unit/storage/storage.spec.ts index 7595312602..83368a0372 100644 --- a/test/unit/storage/storage.spec.ts +++ b/test/unit/storage/storage.spec.ts @@ -123,6 +123,7 @@ describe('Storage', () => { }); it('sets STORAGE_EMULATOR_HOST if FIREBASE_STORAGE_EMULATOR_HOST is set', () => { + new Storage(mockApp) expect(process.env.STORAGE_EMULATOR_HOST).to.equal(`http://${EMULATOR_HOST}`); }); From 8d59ffadb0e6fdded8f54086f7847d0b79bb7c7e Mon Sep 17 00:00:00 2001 From: Abraham Haskins Date: Fri, 7 May 2021 15:38:19 -0500 Subject: [PATCH 3/6] Rename test --- test/unit/storage/storage.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/storage/storage.spec.ts b/test/unit/storage/storage.spec.ts index 83368a0372..fae1c31e14 100644 --- a/test/unit/storage/storage.spec.ts +++ b/test/unit/storage/storage.spec.ts @@ -127,7 +127,7 @@ describe('Storage', () => { expect(process.env.STORAGE_EMULATOR_HOST).to.equal(`http://${EMULATOR_HOST}`); }); - it('sets STORAGE_EMULATOR_HOST if FIREBASE_STORAGE_EMULATOR_HOST is set', () => { + it('throws if FIREBASE_STORAGE_EMULATOR_HOST has a protocol', () => { expect(() => new Storage(mockApp)).to.throw; }); From 4351856aae1f3e183bcb6d1f772306e094985b18 Mon Sep 17 00:00:00 2001 From: Abraham Haskins Date: Fri, 7 May 2021 15:41:21 -0500 Subject: [PATCH 4/6] Dang tests 2 good 4 me --- test/unit/storage/storage.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/storage/storage.spec.ts b/test/unit/storage/storage.spec.ts index fae1c31e14..c36ad29d5c 100644 --- a/test/unit/storage/storage.spec.ts +++ b/test/unit/storage/storage.spec.ts @@ -115,7 +115,7 @@ describe('Storage', () => { }); describe('Emulator mode', () => { - const EMULATOR_HOST = 'http://localhost:9199'; + const EMULATOR_HOST = 'localhost:9199'; before(() => { delete process.env.STORAGE_EMULATOR_HOST; From c1313e897a9da3f8dc1d4f61ee4034f1d63976d9 Mon Sep 17 00:00:00 2001 From: Abraham Haskins Date: Mon, 10 May 2021 11:50:04 -0500 Subject: [PATCH 5/6] Fix test --- test/unit/storage/storage.spec.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/unit/storage/storage.spec.ts b/test/unit/storage/storage.spec.ts index c36ad29d5c..84ffa8fdd3 100644 --- a/test/unit/storage/storage.spec.ts +++ b/test/unit/storage/storage.spec.ts @@ -128,7 +128,10 @@ describe('Storage', () => { }); it('throws if FIREBASE_STORAGE_EMULATOR_HOST has a protocol', () => { - expect(() => new Storage(mockApp)).to.throw; + process.env.FIREBASE_STORAGE_EMULATOR_HOST = `https://${EMULATOR_HOST}`; + + expect(() => new Storage(mockApp)).to.throw( + 'FIREBASE_STORAGE_EMULATOR_HOST should not contain a protocol'); }); after(() => { From ebea0984ec8eb198da02d3b00d3e1c16bf9742ae Mon Sep 17 00:00:00 2001 From: Abraham Haskins Date: Mon, 10 May 2021 11:59:37 -0500 Subject: [PATCH 6/6] Fix tests again --- test/unit/storage/storage.spec.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/test/unit/storage/storage.spec.ts b/test/unit/storage/storage.spec.ts index 84ffa8fdd3..e7d2360ee9 100644 --- a/test/unit/storage/storage.spec.ts +++ b/test/unit/storage/storage.spec.ts @@ -114,21 +114,24 @@ describe('Storage', () => { }); }); - describe('Emulator mode', () => { - const EMULATOR_HOST = 'localhost:9199'; + describe.only('Emulator mode', () => { + const VALID_EMULATOR_HOST = 'localhost:9199'; + const INVALID_EMULATOR_HOST = 'https://localhost:9199'; - before(() => { + beforeEach(() => { delete process.env.STORAGE_EMULATOR_HOST; - process.env.FIREBASE_STORAGE_EMULATOR_HOST = EMULATOR_HOST; + delete process.env.FIREBASE_STORAGE_EMULATOR_HOST; }); it('sets STORAGE_EMULATOR_HOST if FIREBASE_STORAGE_EMULATOR_HOST is set', () => { + process.env.FIREBASE_STORAGE_EMULATOR_HOST = VALID_EMULATOR_HOST; + new Storage(mockApp) - expect(process.env.STORAGE_EMULATOR_HOST).to.equal(`http://${EMULATOR_HOST}`); + expect(process.env.STORAGE_EMULATOR_HOST).to.equal(`http://${VALID_EMULATOR_HOST}`); }); it('throws if FIREBASE_STORAGE_EMULATOR_HOST has a protocol', () => { - process.env.FIREBASE_STORAGE_EMULATOR_HOST = `https://${EMULATOR_HOST}`; + process.env.FIREBASE_STORAGE_EMULATOR_HOST = INVALID_EMULATOR_HOST; expect(() => new Storage(mockApp)).to.throw( 'FIREBASE_STORAGE_EMULATOR_HOST should not contain a protocol');