diff --git a/client.go b/client.go index 419f3c0..e160b92 100644 --- a/client.go +++ b/client.go @@ -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() diff --git a/request.go b/request.go index 0f1b10b..a1b4ec4 100644 --- a/request.go +++ b/request.go @@ -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. // diff --git a/request_test.go b/request_test.go index 7320d25..d988c0a 100644 --- a/request_test.go +++ b/request_test.go @@ -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 @@ -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) { @@ -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)) @@ -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) {