Skip to content

Commit

Permalink
feat: add request level SetHeaderAuthorizationKey method
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Jan 24, 2025
1 parent 9390194 commit 7f26295
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
2 changes: 2 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,8 @@ func (c *Client) HeaderAuthorizationKey() string {

// SetHeaderAuthorizationKey method sets the given HTTP header name for Authorization in the client instance.
//
// It can be overridden at the request level; see [Request.SetHeaderAuthorizationKey].
//
// client.SetHeaderAuthorizationKey("X-Custom-Authorization")
func (c *Client) SetHeaderAuthorizationKey(k string) *Client {
c.lock.Lock()
Expand Down
10 changes: 10 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,16 @@ func (r *Request) SetAuthScheme(scheme string) *Request {
return r
}

// SetHeaderAuthorizationKey method sets the given HTTP header name for Authorization in the request.
//
// It overrides the `Authorization` header name set by method [Client.SetHeaderAuthorizationKey].
//
// client.R().SetHeaderAuthorizationKey("X-Custom-Authorization")
func (r *Request) SetHeaderAuthorizationKey(k string) *Request {
r.HeaderAuthorizationKey = k
return r
}

// SetOutputFileName method sets the output file for the current HTTP request. The current
// HTTP response will be saved in the given file. It is similar to the `curl -o` flag.
//
Expand Down
45 changes: 41 additions & 4 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,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 @@ -695,6 +695,38 @@ func TestRequestAuthScheme(t *testing.T) {
assertEqual(t, http.StatusOK, resp.StatusCode())
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 := dcnl().
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 := dcnl().
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 TestFormData(t *testing.T) {
Expand Down Expand Up @@ -2364,6 +2396,11 @@ func TestRequestSettingsCoverage(t *testing.T) {
r5.EnableRetryDefaultConditions()
assertEqual(t, true, r5.IsRetryDefaultConditions)

r6 := c.R()
customAuthHeader := "X-Custom-Authorization"
r6.SetHeaderAuthorizationKey(customAuthHeader)
assertEqual(t, customAuthHeader, r6.HeaderAuthorizationKey)

invalidJsonBytes := []byte(`{\" \": "value here"}`)
result := jsonIndent(invalidJsonBytes)
assertEqual(t, string(invalidJsonBytes), string(result))
Expand All @@ -2378,10 +2415,10 @@ func TestRequestSettingsCoverage(t *testing.T) {
}
}
}()
r6 := c.R()
rc := c.R()
//lint:ignore SA1012 test case nil check
r62 := r6.Clone(nil)
assertEqual(t, nil, r62.ctx)
rc2 := rc.Clone(nil)
assertEqual(t, nil, rc2.ctx)
}

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

0 comments on commit 7f26295

Please sign in to comment.