Skip to content

Commit

Permalink
Remove need for auth header for versions endpoint
Browse files Browse the repository at this point in the history
[#165573409]

Signed-off-by: Ka Hin Ng <[email protected]>
  • Loading branch information
pivotal-ivan-wang authored and Ka Hin Ng committed May 6, 2019
1 parent e68f820 commit 142e332
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
23 changes: 15 additions & 8 deletions pivnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,20 @@ func (c Client) CreateRequest(
return nil, err
}

accessToken, err := c.token.AccessToken()
if err != nil {
return nil, err
}
if !isVersionsEndpoint(endpoint) {
accessToken, err := c.token.AccessToken()
if err != nil {
return nil, err
}

authorizationHeader, err := AuthorizationHeader(accessToken)
if err != nil {
return nil, fmt.Errorf("could not create authorization header: %s", err)
authorizationHeader, err := AuthorizationHeader(accessToken)
if err != nil {
return nil, fmt.Errorf("could not create authorization header: %s", err)
}

req.Header.Add("Authorization", authorizationHeader)
}

req.Header.Add("Authorization", authorizationHeader)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("User-Agent", c.userAgent)

Expand Down Expand Up @@ -283,3 +286,7 @@ func (c Client) handleUnexpectedResponse(resp *http.Response) error {
}
}
}

func isVersionsEndpoint(endpoint string) bool {
return endpoint == "/versions"
}
12 changes: 12 additions & 0 deletions pivnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,5 +503,17 @@ var _ = Describe("PivnetClient", func() {
Expect(err).NotTo(HaveOccurred())
Expect(req.URL.Path).To(Equal("/api/v2/foo/bar"))
})

It("does not add auth header if versions endpoint", func() {
req, err := client.CreateRequest(
"GET",
"/versions",
nil,
)

Expect(err).NotTo(HaveOccurred())
Expect(req.Header.Get("Authorization")).To(Equal(""))
Expect(req.Header.Get("Content-Type")).To(Equal("application/json"))
})
})
})

0 comments on commit 142e332

Please sign in to comment.