Skip to content

Commit

Permalink
support SetCommonHeaderNonCanonical and SetCommonHeadersNonCanonical
Browse files Browse the repository at this point in the history
  • Loading branch information
imroc committed Apr 26, 2022
1 parent 9789245 commit deaf86c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
19 changes: 19 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,25 @@ func (c *Client) SetCommonHeader(key, value string) *Client {
return c
}

// SetCommonHeaderNonCanonical set a header for all requests which key is a
// non-canonical key (keep case unchanged), only valid for HTTP/1.1.
func (c *Client) SetCommonHeaderNonCanonical(key, value string) *Client {
if c.Headers == nil {
c.Headers = make(http.Header)
}
c.Headers[key] = append(c.Headers[key], value)
return c
}

// SetCommonHeadersNonCanonical set headers for all requests which key is a
// non-canonical key (keep case unchanged), only valid for HTTP/1.1.
func (c *Client) SetCommonHeadersNonCanonical(hdrs map[string]string) *Client {
for k, v := range hdrs {
c.SetCommonHeaderNonCanonical(k, v)
}
return c
}

// SetCommonContentType set the `Content-Type` header for all requests.
func (c *Client) SetCommonContentType(ct string) *Client {
c.SetCommonHeader(hdrContentTypeKey, ct)
Expand Down
12 changes: 12 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ func TestSetCommonHeader(t *testing.T) {
assertEqual(t, "my-value", c.Headers.Get("my-header"))
}

func TestSetCommonHeaderNonCanonical(t *testing.T) {
c := tc().SetCommonHeaderNonCanonical("my-Header", "my-value")
assertEqual(t, "my-value", c.Headers["my-Header"][0])
}

func TestSetCommonHeaders(t *testing.T) {
c := tc().SetCommonHeaders(map[string]string{
"header1": "value1",
Expand All @@ -153,6 +158,13 @@ func TestSetCommonHeaders(t *testing.T) {
assertEqual(t, "value2", c.Headers.Get("header2"))
}

func TestSetCommonHeadersNonCanonical(t *testing.T) {
c := tc().SetCommonHeadersNonCanonical(map[string]string{
"my-Header": "my-value",
})
assertEqual(t, "my-value", c.Headers["my-Header"][0])
}

func TestSetCommonBasicAuth(t *testing.T) {
c := tc().SetCommonBasicAuth("imroc", "123456")
assertEqual(t, "Basic aW1yb2M6MTIzNDU2", c.Headers.Get("Authorization"))
Expand Down
8 changes: 5 additions & 3 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,11 @@ func parseRequestHeader(c *Client, r *Request) error {
if r.Headers == nil {
r.Headers = make(http.Header)
}
for k := range c.Headers {
if r.Headers.Get(k) == "" {
r.Headers.Add(k, c.Headers.Get(k))
for k, vs := range c.Headers {
for _, v := range vs {
if len(r.Headers[k]) == 0 {
r.Headers[k] = append(r.Headers[k], v)
}
}
}
return nil
Expand Down
7 changes: 7 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,13 @@ func TestSetHeaderNonCanonical(t *testing.T) {
Get("/header")
assertSuccess(t, resp, err)
assertEqual(t, true, strings.Contains(resp.Dump(), key))

c.SetCommonHeaderNonCanonical(key, "test")
resp, err = c.R().
EnableDumpWithoutResponse().
Get("/header")
assertSuccess(t, resp, err)
assertEqual(t, true, strings.Contains(resp.Dump(), key))
}

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

0 comments on commit deaf86c

Please sign in to comment.