Skip to content

Commit 3ae6a69

Browse files
authored
Fix storage emulator env formatting (#1257)
* Fix storage emulator env formatting * Repair test * Rename test * Dang tests 2 good 4 me * Fix test * Fix tests again
1 parent e65dbcf commit 3ae6a69

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

src/storage/storage.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,16 @@ export class Storage implements StorageInterface {
4949
}
5050

5151
if (!process.env.STORAGE_EMULATOR_HOST && process.env.FIREBASE_STORAGE_EMULATOR_HOST) {
52-
process.env.STORAGE_EMULATOR_HOST = process.env.FIREBASE_STORAGE_EMULATOR_HOST;
52+
const firebaseStorageEmulatorHost = process.env.FIREBASE_STORAGE_EMULATOR_HOST;
53+
54+
if (firebaseStorageEmulatorHost.match(/https?:\/\//)) {
55+
throw new FirebaseError({
56+
code: 'storage/invalid-emulator-host',
57+
message: 'FIREBASE_STORAGE_EMULATOR_HOST should not contain a protocol (http or https).',
58+
});
59+
}
60+
61+
process.env.STORAGE_EMULATOR_HOST = `http://${process.env.FIREBASE_STORAGE_EMULATOR_HOST}`;
5362
}
5463

5564
let storage: typeof StorageClient;

test/unit/storage/storage.spec.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,27 @@ describe('Storage', () => {
114114
});
115115
});
116116

117-
describe('Emulator mode', () => {
118-
const EMULATOR_HOST = 'http://localhost:9199';
117+
describe.only('Emulator mode', () => {
118+
const VALID_EMULATOR_HOST = 'localhost:9199';
119+
const INVALID_EMULATOR_HOST = 'https://localhost:9199';
119120

120-
before(() => {
121+
beforeEach(() => {
121122
delete process.env.STORAGE_EMULATOR_HOST;
122-
process.env.FIREBASE_STORAGE_EMULATOR_HOST = EMULATOR_HOST;
123+
delete process.env.FIREBASE_STORAGE_EMULATOR_HOST;
123124
});
124125

125126
it('sets STORAGE_EMULATOR_HOST if FIREBASE_STORAGE_EMULATOR_HOST is set', () => {
126-
new Storage(mockApp);
127-
128-
expect(process.env.STORAGE_EMULATOR_HOST).to.equal(EMULATOR_HOST);
127+
process.env.FIREBASE_STORAGE_EMULATOR_HOST = VALID_EMULATOR_HOST;
128+
129+
new Storage(mockApp)
130+
expect(process.env.STORAGE_EMULATOR_HOST).to.equal(`http://${VALID_EMULATOR_HOST}`);
131+
});
132+
133+
it('throws if FIREBASE_STORAGE_EMULATOR_HOST has a protocol', () => {
134+
process.env.FIREBASE_STORAGE_EMULATOR_HOST = INVALID_EMULATOR_HOST;
135+
136+
expect(() => new Storage(mockApp)).to.throw(
137+
'FIREBASE_STORAGE_EMULATOR_HOST should not contain a protocol');
129138
});
130139

131140
after(() => {

0 commit comments

Comments
 (0)