Skip to content

Commit

Permalink
fix: auth scheme and token from client level #959
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Jan 23, 2025
1 parent 7ad1178 commit 5316b6b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
1 change: 0 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,6 @@ func (c *Client) R() *Request {
RawPathParams: map[string]string{},
Debug: c.Debug,
AuthScheme: c.AuthScheme,
Token: c.Token,

client: c,
multipartFiles: []*File{},
Expand Down
2 changes: 2 additions & 0 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ func addCredentials(c *Client, r *Request) error {
// Build the token Auth header
if !IsStringEmpty(r.Token) {
r.RawRequest.Header.Set(c.HeaderAuthorizationKey, strings.TrimSpace(r.AuthScheme+" "+r.Token))
} else if !IsStringEmpty(c.Token) {
r.RawRequest.Header.Set(c.HeaderAuthorizationKey, strings.TrimSpace(r.AuthScheme+" "+c.Token))
}

return nil
Expand Down
34 changes: 33 additions & 1 deletion request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ func TestRequestAuthScheme(t *testing.T) {
assertEqual(t, http.StatusOK, resp.StatusCode())
})

t.Run("empty auth scheme GH954", func(t *testing.T) {
t.Run("empty auth scheme at client level GH954", func(t *testing.T) {
tokenValue := "004DDB79-6801-4587-B976-F093E6AC44FF"

// set client level
Expand All @@ -706,6 +706,38 @@ func TestRequestAuthScheme(t *testing.T) {
assertEqual(t, tokenValue, resp.Request.Header.Get(hdrAuthorizationKey))
})

t.Run("empty auth scheme at request level GH954", func(t *testing.T) {
tokenValue := "004DDB79-6801-4587-B976-F093E6AC44FF"

// set client level
c := dc().
SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}).
SetAuthToken(tokenValue)

resp, err := c.R().
SetAuthScheme("").
Get(ts.URL + "/profile")

assertError(t, err)
assertEqual(t, http.StatusOK, resp.StatusCode())
assertEqual(t, tokenValue, resp.Request.Header.Get(hdrAuthorizationKey))
})

t.Run("only client level auth token GH959", func(t *testing.T) {
tokenValue := "004DDB79-6801-4587-B976-F093E6AC44FF"

c := dc().
SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}).
SetAuthToken(tokenValue)

resp, err := c.R().
Get(ts.URL + "/profile")

assertError(t, err)
assertEqual(t, http.StatusOK, resp.StatusCode())
assertEqual(t, "Bearer "+tokenValue, resp.Request.Header.Get(hdrAuthorizationKey))
})

}

func TestRequestDigestAuth(t *testing.T) {
Expand Down

0 comments on commit 5316b6b

Please sign in to comment.