Skip to content
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

feat(#432): Implement Easier Adding of Multiple Value HTTP Header #452

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}

// SetMultiValueHeaders 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().
// SetMultiValueHeaders(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) SetMultiValueHeaders(headers map[string][]string) *Request {
mhdiiilham marked this conversation as resolved.
Show resolved Hide resolved
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().
SetMultiValueHeaders(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