Skip to content

Commit

Permalink
fix(storage): preserve supplied endpoint's scheme (#4609)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennaEpp committed Aug 13, 2021
1 parent 8cfcf53 commit ee2756f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
5 changes: 4 additions & 1 deletion storage/storage.go
Expand Up @@ -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,
Expand Down
27 changes: 24 additions & 3 deletions storage/storage_test.go
Expand Up @@ -1252,38 +1252,59 @@ 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
WantReadHost string
WantScheme string
}{
{
desc: "No endpoint or emulator host specified",
CustomEndpoint: "",
StorageEmulatorHost: "",
WantRawBasePath: "https://storage.googleapis.com/storage/v1/",
WantReadHost: "storage.googleapis.com",
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",
WantReadHost: "fake.gcs.com:8080",
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",
WantReadHost: "emu.com",
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",
},
}
Expand All @@ -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)
Expand Down

0 comments on commit ee2756f

Please sign in to comment.