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

Clean up config.Header to http.Header conversion #426

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
17 changes: 10 additions & 7 deletions config/config.go
Expand Up @@ -59,14 +59,17 @@ func (h *Header) HTTPHeader() http.Header {
header := make(http.Header)

for name, values := range *h {
var s []string
if values != nil {
s = make([]string, 0, len(values))
for _, value := range values {
s = append(s, string(value))
}
// HTTP allows for empty headers. The best representation of a
// header with a nil or empty set of values is a single empty
// string.
if len(values) == 0 {
header.Set(name, "")
continue
}

for _, value := range values {
header.Add(name, string(value))
}
header[name] = s
}

return header
Expand Down
11 changes: 7 additions & 4 deletions config/config_test.go
Expand Up @@ -70,10 +70,13 @@ func TestHeaderHTTPHeader(t *testing.T) {
"nil": nil,
},
expected: http.Header{
"single": []string{"v1"},
"multi": []string{"v1", "v2"},
"empty": []string{},
"nil": nil,
// note that the conversion from config.header
// to http.Header will use the canonical form
// of the header names.
"Single": []string{"v1"},
"Multi": []string{"v1", "v2"},
"Empty": []string{""},
"Nil": []string{""},
},
},
"nil": {
Expand Down