Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: auth scheme and token from client level #959 #960

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading