Skip to content

Commit 236685e

Browse files
authored
feat: add request level SetHeaderAuthorizationKey method (#963)
1 parent 9390194 commit 236685e

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

client.go

+2
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,8 @@ func (c *Client) HeaderAuthorizationKey() string {
529529

530530
// SetHeaderAuthorizationKey method sets the given HTTP header name for Authorization in the client instance.
531531
//
532+
// It can be overridden at the request level; see [Request.SetHeaderAuthorizationKey].
533+
//
532534
// client.SetHeaderAuthorizationKey("X-Custom-Authorization")
533535
func (c *Client) SetHeaderAuthorizationKey(k string) *Client {
534536
c.lock.Lock()

request.go

+10
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,16 @@ func (r *Request) SetAuthScheme(scheme string) *Request {
654654
return r
655655
}
656656

657+
// SetHeaderAuthorizationKey method sets the given HTTP header name for Authorization in the request.
658+
//
659+
// It overrides the `Authorization` header name set by method [Client.SetHeaderAuthorizationKey].
660+
//
661+
// client.R().SetHeaderAuthorizationKey("X-Custom-Authorization")
662+
func (r *Request) SetHeaderAuthorizationKey(k string) *Request {
663+
r.HeaderAuthorizationKey = k
664+
return r
665+
}
666+
657667
// SetOutputFileName method sets the output file for the current HTTP request. The current
658668
// HTTP response will be saved in the given file. It is similar to the `curl -o` flag.
659669
//

request_test.go

+41-4
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ func TestRequestAuthScheme(t *testing.T) {
681681
assertEqual(t, http.StatusOK, resp.StatusCode())
682682
})
683683

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

687687
// set client level
@@ -695,6 +695,38 @@ func TestRequestAuthScheme(t *testing.T) {
695695
assertEqual(t, http.StatusOK, resp.StatusCode())
696696
assertEqual(t, tokenValue, resp.Request.Header.Get(hdrAuthorizationKey))
697697
})
698+
699+
t.Run("empty auth scheme at request level GH954", func(t *testing.T) {
700+
tokenValue := "004DDB79-6801-4587-B976-F093E6AC44FF"
701+
702+
// set client level
703+
c := dcnl().
704+
SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}).
705+
SetAuthToken(tokenValue)
706+
707+
resp, err := c.R().
708+
SetAuthScheme("").
709+
Get(ts.URL + "/profile")
710+
711+
assertError(t, err)
712+
assertEqual(t, http.StatusOK, resp.StatusCode())
713+
assertEqual(t, tokenValue, resp.Request.Header.Get(hdrAuthorizationKey))
714+
})
715+
716+
t.Run("only client level auth token GH959", func(t *testing.T) {
717+
tokenValue := "004DDB79-6801-4587-B976-F093E6AC44FF"
718+
719+
c := dcnl().
720+
SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}).
721+
SetAuthToken(tokenValue)
722+
723+
resp, err := c.R().
724+
Get(ts.URL + "/profile")
725+
726+
assertError(t, err)
727+
assertEqual(t, http.StatusOK, resp.StatusCode())
728+
assertEqual(t, "Bearer "+tokenValue, resp.Request.Header.Get(hdrAuthorizationKey))
729+
})
698730
}
699731

700732
func TestFormData(t *testing.T) {
@@ -2364,6 +2396,11 @@ func TestRequestSettingsCoverage(t *testing.T) {
23642396
r5.EnableRetryDefaultConditions()
23652397
assertEqual(t, true, r5.IsRetryDefaultConditions)
23662398

2399+
r6 := c.R()
2400+
customAuthHeader := "X-Custom-Authorization"
2401+
r6.SetHeaderAuthorizationKey(customAuthHeader)
2402+
assertEqual(t, customAuthHeader, r6.HeaderAuthorizationKey)
2403+
23672404
invalidJsonBytes := []byte(`{\" \": "value here"}`)
23682405
result := jsonIndent(invalidJsonBytes)
23692406
assertEqual(t, string(invalidJsonBytes), string(result))
@@ -2378,10 +2415,10 @@ func TestRequestSettingsCoverage(t *testing.T) {
23782415
}
23792416
}
23802417
}()
2381-
r6 := c.R()
2418+
rc := c.R()
23822419
//lint:ignore SA1012 test case nil check
2383-
r62 := r6.Clone(nil)
2384-
assertEqual(t, nil, r62.ctx)
2420+
rc2 := rc.Clone(nil)
2421+
assertEqual(t, nil, rc2.ctx)
23852422
}
23862423

23872424
func TestRequestDataRace(t *testing.T) {

0 commit comments

Comments
 (0)