Skip to content

Commit 96101fd

Browse files
authored
fix: Map Firebase FIREBASE_STORAGE_EMULATOR_HOST to the Cloud API STORAGE_EMULATOR_HOST (#588)
* Mapped firebase storage emulator variable to gcloud equivalent * Added unit test * fix: also unset STORAGE_EMULATOR_HOST after test * fix: unset STORAGE_EMULATOR_HOST before test as well
1 parent 5e861fd commit 96101fd

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

storage/storage.go

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package storage
1818
import (
1919
"context"
2020
"errors"
21+
"os"
2122

2223
"cloud.google.com/go/storage"
2324
"firebase.google.com/go/v4/internal"
@@ -34,6 +35,9 @@ type Client struct {
3435
// This function can only be invoked from within the SDK. Client applications should access the
3536
// the Storage service through firebase.App.
3637
func NewClient(ctx context.Context, c *internal.StorageConfig) (*Client, error) {
38+
if os.Getenv("STORAGE_EMULATOR_HOST") == "" && os.Getenv("FIREBASE_STORAGE_EMULATOR_HOST") != "" {
39+
os.Setenv("STORAGE_EMULATOR_HOST", os.Getenv("FIREBASE_STORAGE_EMULATOR_HOST"))
40+
}
3741
client, err := storage.NewClient(ctx, c.Opts...)
3842
if err != nil {
3943
return nil, err

storage/storage_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package storage
1616

1717
import (
1818
"context"
19+
"os"
1920
"testing"
2021

2122
"firebase.google.com/go/v4/internal"
@@ -38,6 +39,25 @@ func TestNewClientError(t *testing.T) {
3839
}
3940
}
4041

42+
func TestNewClientEmulatorHostEnvVar(t *testing.T) {
43+
emulatorHost := "localhost:9099"
44+
os.Setenv("FIREBASE_STORAGE_EMULATOR_HOST", emulatorHost)
45+
defer os.Unsetenv("FIREBASE_STORAGE_EMULATOR_HOST")
46+
os.Unsetenv("STORAGE_EMULATOR_HOST")
47+
defer os.Unsetenv("STORAGE_EMULATOR_HOST")
48+
49+
_, err := NewClient(context.Background(), &internal.StorageConfig{
50+
Opts: opts,
51+
})
52+
if err != nil {
53+
t.Fatal(err)
54+
}
55+
56+
if host := os.Getenv("STORAGE_EMULATOR_HOST"); host != emulatorHost {
57+
t.Errorf("emulator host: %q; want: %q", host, emulatorHost)
58+
}
59+
}
60+
4161
func TestNoBucketName(t *testing.T) {
4262
client, err := NewClient(context.Background(), &internal.StorageConfig{
4363
Opts: opts,

0 commit comments

Comments
 (0)