Skip to content

Commit

Permalink
support SetHeaderNonCanonical and SetHeadersNonCanonical
Browse files Browse the repository at this point in the history
  • Loading branch information
imroc committed Apr 26, 2022
1 parent c9ce64a commit 9789245
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 38 deletions.
25 changes: 8 additions & 17 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,31 +355,22 @@ func (r *Request) SetHeader(key, value string) *Request {
return r
}

// SetHeadersMap set headers from a map for the request.
// To use non-canonical keys, assign to the map directly.
func (r *Request) SetHeadersMap(hdrs map[string]string) *Request {
// SetHeadersNonCanonical set headers from a map for the request which key is a
// non-canonical key (keep case unchanged), only valid for HTTP/1.1.
func (r *Request) SetHeadersNonCanonical(hdrs map[string]string) *Request {
for k, v := range hdrs {
r.SetHeaderMap(k, v)
r.SetHeaderNonCanonical(k, v)
}
return r
}

// SetHeaderMap set a header for the request.
// To use non-canonical keys, assign to the map directly.
func (r *Request) SetHeaderMap(key, value string) *Request {
// SetHeaderNonCanonical set a header for the request which key is a
// non-canonical key (keep case unchanged), only valid for HTTP/1.1.
func (r *Request) SetHeaderNonCanonical(key, value string) *Request {
if r.Headers == nil {
r.Headers = make(http.Header)
}
r.Headers[key] = []string{value}
return r
}

// SetHeaderMaps set headers from a map for the request.
// To use non-canonical keys, assign to the map directly.
func (r *Request) SetHeaderMaps(hdrs map[string][]string) *Request {
if r.Headers == nil {
r.Headers = hdrs
}
r.Headers[key] = append(r.Headers[key], value)
return r
}

Expand Down
35 changes: 14 additions & 21 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net/url"
"os"
"strconv"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -510,31 +511,23 @@ func testHeader(t *testing.T, c *Client) {
assertEqual(t, "value3", headers.Get("header3"))
}

func TestSetHeaderMaps(t *testing.T) {
func TestSetHeaderNonCanonical(t *testing.T) {
// set headers
headers := map[string][]string{
"header1": {"value1"},
"header2": {"value2", "value3"},
}
resp, err := tc().R().
SetHeaderMaps(headers).
Get("/headersMaps")
key := "spring.cloud.function.Routing-expression"
c := tc().EnableForceHTTP1()
resp, err := c.R().EnableDumpWithoutResponse().
SetHeadersNonCanonical(map[string]string{
key: "test",
}).Get("/header")
assertSuccess(t, resp, err)
assertEqual(t, true, strings.Contains(resp.Dump(), key))

}

func TestSetHeadersMap(t *testing.T) {
// set headers
headers := map[string]string{
"header1": "value1",
"header2": "value2",
}

resp, err := tc().R().
SetHeadersMap(headers).
Get("/headersMap")
resp, err = c.R().
EnableDumpWithoutResponse().
SetHeaderNonCanonical(key, "test").
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 9789245

Please sign in to comment.