diff --git a/storage/storage.go b/storage/storage.go index 82048923cd5..ed633f7208b 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -139,12 +139,15 @@ func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error if err != nil { return nil, fmt.Errorf("storage client: %v", err) } - // Update readHost with the chosen endpoint. + // Update readHost and scheme with the chosen endpoint. u, err := url.Parse(ep) if err != nil { return nil, fmt.Errorf("supplied endpoint %q is not valid: %v", ep, err) } readHost = u.Host + if u.Scheme != "" { + scheme = u.Scheme + } return &Client{ hc: hc, diff --git a/storage/storage_test.go b/storage/storage_test.go index 55d00e2252f..8f99e60ed65 100644 --- a/storage/storage_test.go +++ b/storage/storage_test.go @@ -1252,6 +1252,7 @@ func TestAttrToFieldMapCoverage(t *testing.T) { func TestWithEndpoint(t *testing.T) { originalStorageEmulatorHost := os.Getenv("STORAGE_EMULATOR_HOST") testCases := []struct { + desc string CustomEndpoint string StorageEmulatorHost string WantRawBasePath string @@ -1259,6 +1260,7 @@ func TestWithEndpoint(t *testing.T) { WantScheme string }{ { + desc: "No endpoint or emulator host specified", CustomEndpoint: "", StorageEmulatorHost: "", WantRawBasePath: "https://storage.googleapis.com/storage/v1/", @@ -1266,6 +1268,7 @@ func TestWithEndpoint(t *testing.T) { WantScheme: "https", }, { + desc: "With specified https endpoint, no specified emulator host", CustomEndpoint: "https://fake.gcs.com:8080/storage/v1", StorageEmulatorHost: "", WantRawBasePath: "https://fake.gcs.com:8080/storage/v1", @@ -1273,6 +1276,15 @@ func TestWithEndpoint(t *testing.T) { WantScheme: "https", }, { + desc: "With specified http endpoint, no specified emulator host", + CustomEndpoint: "http://fake.gcs.com:8080/storage/v1", + StorageEmulatorHost: "", + WantRawBasePath: "http://fake.gcs.com:8080/storage/v1", + WantReadHost: "fake.gcs.com:8080", + WantScheme: "http", + }, + { + desc: "Emulator host specified, no specified endpoint", CustomEndpoint: "", StorageEmulatorHost: "http://emu.com", WantRawBasePath: "http://emu.com", @@ -1280,10 +1292,19 @@ func TestWithEndpoint(t *testing.T) { WantScheme: "http", }, { + desc: "Endpoint overrides emulator host when both are specified - https", CustomEndpoint: "https://fake.gcs.com:8080/storage/v1", StorageEmulatorHost: "http://emu.com", WantRawBasePath: "https://fake.gcs.com:8080/storage/v1", WantReadHost: "fake.gcs.com:8080", + WantScheme: "https", + }, + { + desc: "Endpoint overrides emulator host when both are specified - http", + CustomEndpoint: "http://fake.gcs.com:8080/storage/v1", + StorageEmulatorHost: "https://emu.com", + WantRawBasePath: "http://fake.gcs.com:8080/storage/v1", + WantReadHost: "fake.gcs.com:8080", WantScheme: "http", }, } @@ -1299,13 +1320,13 @@ func TestWithEndpoint(t *testing.T) { } if c.raw.BasePath != tc.WantRawBasePath { - t.Errorf("raw.BasePath not set correctly: got %v, want %v", c.raw.BasePath, tc.WantRawBasePath) + t.Errorf("%s: raw.BasePath not set correctly\n\tgot %v, want %v", tc.desc, c.raw.BasePath, tc.WantRawBasePath) } if c.readHost != tc.WantReadHost { - t.Errorf("readHost not set correctly: got %v, want %v", c.readHost, tc.WantReadHost) + t.Errorf("%s: readHost not set correctly\n\tgot %v, want %v", tc.desc, c.readHost, tc.WantReadHost) } if c.scheme != tc.WantScheme { - t.Errorf("scheme not set correctly: got %v, want %v", c.scheme, tc.WantScheme) + t.Errorf("%s: scheme not set correctly\n\tgot %v, want %v", tc.desc, c.scheme, tc.WantScheme) } } os.Setenv("STORAGE_EMULATOR_HOST", originalStorageEmulatorHost)