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): accept emulator env var without scheme #4616

Merged
merged 14 commits into from Aug 13, 2021
Merged
21 changes: 17 additions & 4 deletions storage/storage.go
Expand Up @@ -120,13 +120,26 @@ func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error
opts = append(opts, internaloption.WithDefaultEndpoint("https://storage.googleapis.com/storage/v1/"))
opts = append(opts, internaloption.WithDefaultMTLSEndpoint("https://storage.mtls.googleapis.com/storage/v1/"))
} else {
scheme = "http"
readHost = host
BrennaEpp marked this conversation as resolved.
Show resolved Hide resolved
// Add scheme for user if not supplied in STORAGE_EMULATOR_HOST
var hostURL *url.URL
if strings.Contains(host, "://") {
BrennaEpp marked this conversation as resolved.
Show resolved Hide resolved
h, err := url.Parse(host)
if err != nil {
return nil, err
}
hostURL = h
} else {
hostURL = &url.URL{Scheme: "http", Host: host}
}

// Prepend an endpoint for the user in case they don't supply one
hostURL.Path = "storage/v1/"
endpoint := hostURL.String()

opts = append([]option.ClientOption{option.WithoutAuthentication()}, opts...)

opts = append(opts, internaloption.WithDefaultEndpoint(host))
opts = append(opts, internaloption.WithDefaultMTLSEndpoint(host))
opts = append(opts, internaloption.WithDefaultEndpoint(endpoint))
opts = append(opts, internaloption.WithDefaultMTLSEndpoint(endpoint))
}

// htransport selects the correct endpoint among WithEndpoint (user override), WithDefaultEndpoint, and WithDefaultMTLSEndpoint.
Expand Down
13 changes: 9 additions & 4 deletions storage/storage_test.go
Expand Up @@ -1287,7 +1287,15 @@ func TestWithEndpoint(t *testing.T) {
desc: "Emulator host specified, no specified endpoint",
CustomEndpoint: "",
StorageEmulatorHost: "http://emu.com",
WantRawBasePath: "http://emu.com",
WantRawBasePath: "http://emu.com/storage/v1/",
WantReadHost: "emu.com",
WantScheme: "http",
},
{
desc: "Emulator host specified without scheme",
CustomEndpoint: "",
StorageEmulatorHost: "emu.com",
WantRawBasePath: "http://emu.com/storage/v1/",
WantReadHost: "emu.com",
WantScheme: "http",
},
Expand Down Expand Up @@ -1315,9 +1323,6 @@ func TestWithEndpoint(t *testing.T) {
if err != nil {
t.Fatalf("error creating client: %v", err)
}
if err != nil {
t.Fatalf("error creating client: %v", err)
}

if 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)
Expand Down