Skip to content

Commit

Permalink
feat(#432): Implement Easier Adding of Multiple Value HTTP Header (#452)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhdiiilham committed Sep 12, 2021
1 parent 3f7a6e5 commit ecf7cda
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
16 changes: 16 additions & 0 deletions request.go
Expand Up @@ -117,6 +117,22 @@ func (r *Request) SetHeaders(headers map[string]string) *Request {
return r
}

// SetHeaderMultiValues sets multiple headers fields and its values is list of strings at one go in the current request.
//
// For Example: To set `Accept` as `text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8`
//
// client.R().
// SetHeaderMultiValues(map[string][]string{
// "Accept": []string{"text/html", "application/xhtml+xml", "application/xml;q=0.9", "image/webp", "*/*;q=0.8"},
// })
// Also you can override header value, which was set at client instance level.
func (r *Request) SetHeaderMultiValues(headers map[string][]string) *Request {
for key, values := range headers {
r.SetHeader(key, strings.Join(values, ", "))
}
return r
}

// SetHeaderVerbatim method is to set a single header field and its value verbatim in the current request.
//
// For Example: To set `all_lowercase` and `UPPERCASE` as `available`.
Expand Down
13 changes: 13 additions & 0 deletions request_test.go
Expand Up @@ -1371,6 +1371,19 @@ func TestSetHeaderVerbatim(t *testing.T) {
assertEqual(t, "value_standard", r.Header.Get("Header-Lowercase"))
}

func TestSetHeaderMultipleValue(t *testing.T) {
ts := createPostServer(t)
defer ts.Close()

r := dclr().
SetHeaderMultiValues(map[string][]string{
"Content": []string{"text/*", "text/html", "*"},
"Authorization": []string{"Bearer xyz"},
})
assertEqual(t, "text/*, text/html, *", r.Header.Get("content"))
assertEqual(t, "Bearer xyz", r.Header.Get("authorization"))
}

func TestOutputFileWithBaseDirAndRelativePath(t *testing.T) {
ts := createGetServer(t)
defer ts.Close()
Expand Down

0 comments on commit ecf7cda

Please sign in to comment.