storage: honor WithEndpoint option for readHost
Currently this is getting ignored for reads.
Fixes #1619
Change-Id: I50e20daec5ddd1b8a4ee53c1907f6d9d8091f8ca
Reviewed-on: https://code-review.googlesource.com/c/gocloud/+/49550
Reviewed-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Chris Broadfoot <cbro@google.com>
diff --git a/storage/storage.go b/storage/storage.go
index c85cc83..ae7c783 100644
--- a/storage/storage.go
+++ b/storage/storage.go
@@ -121,7 +121,14 @@
// TODO: remove when the raw client uses this endpoint as its default (~end of 2020)
rawService.BasePath = "https://storage.googleapis.com/storage/v1/"
} else {
+ // If the endpoint has been set explicitly, use this for the BasePath
+ // as well as readHost
rawService.BasePath = ep
+ u, err := url.Parse(ep)
+ if err != nil {
+ return nil, fmt.Errorf("supplied endpoint %v is not valid: %v", ep, err)
+ }
+ readHost = u.Hostname()
}
return &Client{
diff --git a/storage/storage_test.go b/storage/storage_test.go
index 2b12e41..fdfda9e 100644
--- a/storage/storage_test.go
+++ b/storage/storage_test.go
@@ -1143,3 +1143,23 @@
}
}
}
+
+// Create a client using a custom endpoint, and verify that raw.BasePath (used
+// for writes) and readHost (used for reads) are both set correctly.
+func TestWithEndpoint(t *testing.T) {
+ ctx := context.Background()
+ endpoint := "https://fake.gcs.com/storage/v1"
+ c, err := NewClient(ctx, option.WithEndpoint(endpoint))
+ if err != nil {
+ t.Fatalf("error creating client: %v", err)
+ }
+
+ if c.raw.BasePath != endpoint {
+ t.Errorf("raw.BasePath not set correctly: got %v, want %v", c.raw.BasePath, endpoint)
+ }
+
+ want := "fake.gcs.com"
+ if c.readHost != want {
+ t.Errorf("readHost not set correctly: got %v, want %v", c.readHost, want)
+ }
+}