Skip to content

Commit aee0cb5

Browse files
authored
fix: auth scheme and token from client level #959 (#960)
1 parent 7ad1178 commit aee0cb5

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

client.go

-1
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,6 @@ func (c *Client) R() *Request {
451451
RawPathParams: map[string]string{},
452452
Debug: c.Debug,
453453
AuthScheme: c.AuthScheme,
454-
Token: c.Token,
455454

456455
client: c,
457456
multipartFiles: []*File{},

middleware.go

+2
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ func addCredentials(c *Client, r *Request) error {
301301
// Build the token Auth header
302302
if !IsStringEmpty(r.Token) {
303303
r.RawRequest.Header.Set(c.HeaderAuthorizationKey, strings.TrimSpace(r.AuthScheme+" "+r.Token))
304+
} else if !IsStringEmpty(c.Token) {
305+
r.RawRequest.Header.Set(c.HeaderAuthorizationKey, strings.TrimSpace(r.AuthScheme+" "+c.Token))
304306
}
305307

306308
return nil

request_test.go

+33-1
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ func TestRequestAuthScheme(t *testing.T) {
691691
assertEqual(t, http.StatusOK, resp.StatusCode())
692692
})
693693

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

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

709+
t.Run("empty auth scheme at request level GH954", func(t *testing.T) {
710+
tokenValue := "004DDB79-6801-4587-B976-F093E6AC44FF"
711+
712+
// set client level
713+
c := dc().
714+
SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}).
715+
SetAuthToken(tokenValue)
716+
717+
resp, err := c.R().
718+
SetAuthScheme("").
719+
Get(ts.URL + "/profile")
720+
721+
assertError(t, err)
722+
assertEqual(t, http.StatusOK, resp.StatusCode())
723+
assertEqual(t, tokenValue, resp.Request.Header.Get(hdrAuthorizationKey))
724+
})
725+
726+
t.Run("only client level auth token GH959", func(t *testing.T) {
727+
tokenValue := "004DDB79-6801-4587-B976-F093E6AC44FF"
728+
729+
c := dc().
730+
SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}).
731+
SetAuthToken(tokenValue)
732+
733+
resp, err := c.R().
734+
Get(ts.URL + "/profile")
735+
736+
assertError(t, err)
737+
assertEqual(t, http.StatusOK, resp.StatusCode())
738+
assertEqual(t, "Bearer "+tokenValue, resp.Request.Header.Get(hdrAuthorizationKey))
739+
})
740+
709741
}
710742

711743
func TestRequestDigestAuth(t *testing.T) {

0 commit comments

Comments
 (0)