httpraw: fix overrwrite/empty val bugs in Header.Set/SetBytes (#159)

This commit is contained in:
Pat Whittingslow
2026-07-17 11:07:59 -03:00
committed by GitHub
parent ab1a0c735a
commit 766e03e90e
2 changed files with 35 additions and 4 deletions
+32
View File
@@ -208,3 +208,35 @@ func TestCopyNormalizedHeaderValue(t *testing.T) {
}
}
}
func TestHeaderSetOverwrite(t *testing.T) {
var h Header
h.Reset(nil)
h.SetMethod("GET")
h.SetRequestURI("/")
h.SetProtocol("HTTP/1.1")
h.Set("Host", "first.example.com")
h.Set("Host", "second.example.com")
got := string(h.Get("Host"))
if got != "second.example.com" {
t.Errorf("want Host %q, got %q", "second.example.com", got)
}
req, err := h.AppendRequest(nil)
if err != nil {
t.Fatal(err)
}
if n := strings.Count(string(req), "Host:"); n != 1 {
t.Errorf("want 1 Host field in request, got %d:\n%s", n, req)
}
}
func TestHeaderSetBytesEmptyValue(t *testing.T) {
var h Header
h.Reset(nil)
h.SetBytes("X-Empty", nil)
if got := h.Get("X-Empty"); len(got) != 0 {
t.Errorf("want empty value, got %q", got)
}
}