Skip to content

Commit 5f8cbb9

Browse files
authored
fix(storage): accept emulator env var without scheme (#4616)
* fix(storage): remove unnecessary variable Variable seems to be duplicating information which led to a bug. * fix(storage): preserve supplied endpoint's scheme * fix(storage): accept emulator env var without scheme You can now use the client library with an emulator by setting the environment variable to a host, with or without a scheme You also no longer have to supply an endpoint to use the emulator; the endpoint is built for you * Rename var * Fix bug * Small refactorings * Add test case for host:port * Update comment
1 parent 2489cb6 commit 5f8cbb9

2 files changed

Lines changed: 39 additions & 19 deletions

File tree

‎storage/storage.go‎

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ type Client struct {
101101
// Clients should be reused instead of created as needed. The methods of Client
102102
// are safe for concurrent use by multiple goroutines.
103103
func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) {
104-
var host, readHost, scheme string
105104

106105
// In general, it is recommended to use raw.NewService instead of htransport.NewClient
107106
// since raw.NewService configures the correct default endpoints when initializing the
@@ -110,23 +109,35 @@ func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error
110109
// here so it can be re-used by both reader.go and raw.NewService. This means we need to
111110
// manually configure the default endpoint options on the http client. Furthermore, we
112111
// need to account for STORAGE_EMULATOR_HOST override when setting the default endpoints.
113-
if host = os.Getenv("STORAGE_EMULATOR_HOST"); host == "" {
114-
scheme = "https"
115-
readHost = "storage.googleapis.com"
116-
112+
if host := os.Getenv("STORAGE_EMULATOR_HOST"); host == "" {
117113
// Prepend default options to avoid overriding options passed by the user.
118114
opts = append([]option.ClientOption{option.WithScopes(ScopeFullControl), option.WithUserAgent(userAgent)}, opts...)
119115

120116
opts = append(opts, internaloption.WithDefaultEndpoint("https://storage.googleapis.com/storage/v1/"))
121117
opts = append(opts, internaloption.WithDefaultMTLSEndpoint("https://storage.mtls.googleapis.com/storage/v1/"))
122118
} else {
123-
scheme = "http"
124-
readHost = host
119+
var hostURL *url.URL
125120

121+
if strings.Contains(host, "://") {
122+
h, err := url.Parse(host)
123+
if err != nil {
124+
return nil, err
125+
}
126+
hostURL = h
127+
} else {
128+
// Add scheme for user if not supplied in STORAGE_EMULATOR_HOST
129+
// URL is only parsed correctly if it has a scheme, so we build it ourselves
130+
hostURL = &url.URL{Scheme: "http", Host: host}
131+
}
132+
133+
hostURL.Path = "storage/v1/"
134+
endpoint := hostURL.String()
135+
136+
// Append the emulator host as default endpoint for the user
126137
opts = append([]option.ClientOption{option.WithoutAuthentication()}, opts...)
127138

128-
opts = append(opts, internaloption.WithDefaultEndpoint(host))
129-
opts = append(opts, internaloption.WithDefaultMTLSEndpoint(host))
139+
opts = append(opts, internaloption.WithDefaultEndpoint(endpoint))
140+
opts = append(opts, internaloption.WithDefaultMTLSEndpoint(endpoint))
130141
}
131142

132143
// htransport selects the correct endpoint among WithEndpoint (user override), WithDefaultEndpoint, and WithDefaultMTLSEndpoint.
@@ -144,16 +155,12 @@ func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error
144155
if err != nil {
145156
return nil, fmt.Errorf("supplied endpoint %q is not valid: %v", ep, err)
146157
}
147-
readHost = u.Host
148-
if u.Scheme != "" {
149-
scheme = u.Scheme
150-
}
151158

152159
return &Client{
153160
hc: hc,
154161
raw: rawService,
155-
scheme: scheme,
156-
readHost: readHost,
162+
scheme: u.Scheme,
163+
readHost: u.Host,
157164
}, nil
158165
}
159166

‎storage/storage_test.go‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,10 +1287,26 @@ func TestWithEndpoint(t *testing.T) {
12871287
desc: "Emulator host specified, no specified endpoint",
12881288
CustomEndpoint: "",
12891289
StorageEmulatorHost: "http://emu.com",
1290-
WantRawBasePath: "http://emu.com",
1290+
WantRawBasePath: "http://emu.com/storage/v1/",
12911291
WantReadHost: "emu.com",
12921292
WantScheme: "http",
12931293
},
1294+
{
1295+
desc: "Emulator host specified without scheme",
1296+
CustomEndpoint: "",
1297+
StorageEmulatorHost: "emu.com",
1298+
WantRawBasePath: "http://emu.com/storage/v1/",
1299+
WantReadHost: "emu.com",
1300+
WantScheme: "http",
1301+
},
1302+
{
1303+
desc: "Emulator host specified as host:port",
1304+
CustomEndpoint: "",
1305+
StorageEmulatorHost: "localhost:9000",
1306+
WantRawBasePath: "http://localhost:9000/storage/v1/",
1307+
WantReadHost: "localhost:9000",
1308+
WantScheme: "http",
1309+
},
12941310
{
12951311
desc: "Endpoint overrides emulator host when both are specified - https",
12961312
CustomEndpoint: "https://fake.gcs.com:8080/storage/v1",
@@ -1315,9 +1331,6 @@ func TestWithEndpoint(t *testing.T) {
13151331
if err != nil {
13161332
t.Fatalf("error creating client: %v", err)
13171333
}
1318-
if err != nil {
1319-
t.Fatalf("error creating client: %v", err)
1320-
}
13211334

13221335
if c.raw.BasePath != tc.WantRawBasePath {
13231336
t.Errorf("%s: raw.BasePath not set correctly\n\tgot %v, want %v", tc.desc, c.raw.BasePath, tc.WantRawBasePath)

0 commit comments

Comments
 (0)