Skip to content

Commit ee2756f

Browse files
authored
fix(storage): preserve supplied endpoint's scheme (#4609)
1 parent 8cfcf53 commit ee2756f

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

‎storage/storage.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,15 @@ func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error
139139
if err != nil {
140140
return nil, fmt.Errorf("storage client: %v", err)
141141
}
142-
// Update readHost with the chosen endpoint.
142+
// Update readHost and scheme with the chosen endpoint.
143143
u, err := url.Parse(ep)
144144
if err != nil {
145145
return nil, fmt.Errorf("supplied endpoint %q is not valid: %v", ep, err)
146146
}
147147
readHost = u.Host
148+
if u.Scheme != "" {
149+
scheme = u.Scheme
150+
}
148151

149152
return &Client{
150153
hc: hc,

‎storage/storage_test.go‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,38 +1252,59 @@ func TestAttrToFieldMapCoverage(t *testing.T) {
12521252
func TestWithEndpoint(t *testing.T) {
12531253
originalStorageEmulatorHost := os.Getenv("STORAGE_EMULATOR_HOST")
12541254
testCases := []struct {
1255+
desc string
12551256
CustomEndpoint string
12561257
StorageEmulatorHost string
12571258
WantRawBasePath string
12581259
WantReadHost string
12591260
WantScheme string
12601261
}{
12611262
{
1263+
desc: "No endpoint or emulator host specified",
12621264
CustomEndpoint: "",
12631265
StorageEmulatorHost: "",
12641266
WantRawBasePath: "https://storage.googleapis.com/storage/v1/",
12651267
WantReadHost: "storage.googleapis.com",
12661268
WantScheme: "https",
12671269
},
12681270
{
1271+
desc: "With specified https endpoint, no specified emulator host",
12691272
CustomEndpoint: "https://fake.gcs.com:8080/storage/v1",
12701273
StorageEmulatorHost: "",
12711274
WantRawBasePath: "https://fake.gcs.com:8080/storage/v1",
12721275
WantReadHost: "fake.gcs.com:8080",
12731276
WantScheme: "https",
12741277
},
12751278
{
1279+
desc: "With specified http endpoint, no specified emulator host",
1280+
CustomEndpoint: "http://fake.gcs.com:8080/storage/v1",
1281+
StorageEmulatorHost: "",
1282+
WantRawBasePath: "http://fake.gcs.com:8080/storage/v1",
1283+
WantReadHost: "fake.gcs.com:8080",
1284+
WantScheme: "http",
1285+
},
1286+
{
1287+
desc: "Emulator host specified, no specified endpoint",
12761288
CustomEndpoint: "",
12771289
StorageEmulatorHost: "http://emu.com",
12781290
WantRawBasePath: "http://emu.com",
12791291
WantReadHost: "emu.com",
12801292
WantScheme: "http",
12811293
},
12821294
{
1295+
desc: "Endpoint overrides emulator host when both are specified - https",
12831296
CustomEndpoint: "https://fake.gcs.com:8080/storage/v1",
12841297
StorageEmulatorHost: "http://emu.com",
12851298
WantRawBasePath: "https://fake.gcs.com:8080/storage/v1",
12861299
WantReadHost: "fake.gcs.com:8080",
1300+
WantScheme: "https",
1301+
},
1302+
{
1303+
desc: "Endpoint overrides emulator host when both are specified - http",
1304+
CustomEndpoint: "http://fake.gcs.com:8080/storage/v1",
1305+
StorageEmulatorHost: "https://emu.com",
1306+
WantRawBasePath: "http://fake.gcs.com:8080/storage/v1",
1307+
WantReadHost: "fake.gcs.com:8080",
12871308
WantScheme: "http",
12881309
},
12891310
}
@@ -1299,13 +1320,13 @@ func TestWithEndpoint(t *testing.T) {
12991320
}
13001321

13011322
if c.raw.BasePath != tc.WantRawBasePath {
1302-
t.Errorf("raw.BasePath not set correctly: got %v, want %v", c.raw.BasePath, tc.WantRawBasePath)
1323+
t.Errorf("%s: raw.BasePath not set correctly\n\tgot %v, want %v", tc.desc, c.raw.BasePath, tc.WantRawBasePath)
13031324
}
13041325
if c.readHost != tc.WantReadHost {
1305-
t.Errorf("readHost not set correctly: got %v, want %v", c.readHost, tc.WantReadHost)
1326+
t.Errorf("%s: readHost not set correctly\n\tgot %v, want %v", tc.desc, c.readHost, tc.WantReadHost)
13061327
}
13071328
if c.scheme != tc.WantScheme {
1308-
t.Errorf("scheme not set correctly: got %v, want %v", c.scheme, tc.WantScheme)
1329+
t.Errorf("%s: scheme not set correctly\n\tgot %v, want %v", tc.desc, c.scheme, tc.WantScheme)
13091330
}
13101331
}
13111332
os.Setenv("STORAGE_EMULATOR_HOST", originalStorageEmulatorHost)

0 commit comments

Comments
 (0)