Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix storage emulator env formatting #1257

Merged
merged 6 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/storage/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 16 additions & 7 deletions test/unit/storage/storage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,27 @@ describe('Storage', () => {
});
});

describe('Emulator mode', () => {
const EMULATOR_HOST = 'http://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', () => {
new Storage(mockApp);

expect(process.env.STORAGE_EMULATOR_HOST).to.equal(EMULATOR_HOST);
process.env.FIREBASE_STORAGE_EMULATOR_HOST = VALID_EMULATOR_HOST;

new Storage(mockApp)
expect(process.env.STORAGE_EMULATOR_HOST).to.equal(`http://${VALID_EMULATOR_HOST}`);
});

it('throws if FIREBASE_STORAGE_EMULATOR_HOST has a protocol', () => {
abeisgoat marked this conversation as resolved.
Show resolved Hide resolved
process.env.FIREBASE_STORAGE_EMULATOR_HOST = INVALID_EMULATOR_HOST;

expect(() => new Storage(mockApp)).to.throw(
'FIREBASE_STORAGE_EMULATOR_HOST should not contain a protocol');
});

after(() => {
Expand Down