Skip to content

feat: add request level SetHeaderAuthorizationKey method #963

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

Merged
merged 1 commit into from
Jan 24, 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
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
Loading