Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Clean up config.Header to http.Header conversion
It was pointed out by @dswarbrick that the current implementation is
unnecessary as http.Header has an Add method. Header.Add canonicalizes
header names, so the test needs to be adjusted, too.

Signed-off-by: Marcelo E. Magallon <[email protected]>
  • Loading branch information
mem committed Dec 17, 2022
commit 15df3218a4ce4e3e3a11a688df7bf5ea6f09dae8
17 changes: 10 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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