1
0
mirror of https://github.com/golang/go synced 2024-11-26 23:01:23 -07:00

net/http: return nil from Header.Clone if the receiver is nil

Fixes #33141

Change-Id: I84a8b3496fc9396fd1c09ba9505697c34bdf7105
Reviewed-on: https://go-review.googlesource.com/c/go/+/188022
Run-TryBot: Andrew Bonventre <andybons@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
This commit is contained in:
Andrew Bonventre 2019-07-30 17:03:16 -04:00
parent 39d4178735
commit 8dddf7556e
2 changed files with 13 additions and 1 deletions

View File

@ -78,8 +78,12 @@ func (h Header) write(w io.Writer, trace *httptrace.ClientTrace) error {
return h.writeSubset(w, nil, trace)
}
// Clone returns a copy of h.
// Clone returns a copy of h or nil if h is nil.
func (h Header) Clone() Header {
if h == nil {
return nil
}
// Find total number of values.
nv := 0
for _, vv := range h {

View File

@ -176,6 +176,14 @@ func TestHasToken(t *testing.T) {
}
}
func TestNilHeaderClone(t *testing.T) {
t1 := Header(nil)
t2 := t1.Clone()
if t2 != nil {
t.Errorf("cloned header does not match original: got: %+v; want: %+v", t2, nil)
}
}
var testHeader = Header{
"Content-Length": {"123"},
"Content-Type": {"text/plain"},