Skip to content
Permalink
Browse files
fix(compute/metadata): remove leading slash for Get suffix (#2760)
Updates #2750
  • Loading branch information
codyoss committed Aug 21, 2020
1 parent 03d78b5 commit f0d605ccf32391a9da056a2c551158bd076c128d
Showing with 37 additions and 0 deletions.
  1. +1 −0 compute/metadata/metadata.go
  2. +36 −0 compute/metadata/metadata_test.go
@@ -296,6 +296,7 @@ func (c *Client) getETag(suffix string) (value, etag string, err error) {
// being stable anyway.
host = metadataIP
}
suffix = strings.TrimLeft(suffix, "/")
u := "http://" + host + "/computeMetadata/v1/" + suffix
req, err := http.NewRequest("GET", u, nil)
if err != nil {
@@ -73,6 +73,42 @@ func TestGetFailsOnBadURL(t *testing.T) {
}
}

func TestGet_LeadingSlash(t *testing.T) {
want := "http://169.254.169.254/computeMetadata/v1/instance/service-accounts/default/identity?audience=http://example.com"
tests := []struct {
name string
suffix string
}{
{
name: "without leading slash",
suffix: "instance/service-accounts/default/identity?audience=http://example.com",
},
{
name: "with leading slash",
suffix: "/instance/service-accounts/default/identity?audience=http://example.com",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ct := &captureTransport{}
c := NewClient(&http.Client{Transport: ct})
c.Get(tc.suffix)
if ct.url != want {
t.Fatalf("got %v, want %v", ct.url, want)
}
})
}
}

type captureTransport struct {
url string
}

func (ct *captureTransport) RoundTrip(req *http.Request) (*http.Response, error) {
ct.url = req.URL.String()
return &http.Response{Body: ioutil.NopCloser(bytes.NewReader(nil))}, nil
}

type userAgentTransport struct {
userAgent string
base http.RoundTripper

0 comments on commit f0d605c

Please sign in to comment.