From 1ccb896eca01524274853df36e3669692c212ef0 Mon Sep 17 00:00:00 2001 From: Hubert Siwik Date: Thu, 11 Jul 2024 15:38:10 +0200 Subject: [PATCH] Fix header implementation Fix comment to the case in-sensitive keys Move out test cases to separate methods --- ftwhttp/header.go | 24 +++++++++++----------- ftwhttp/header_test.go | 45 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 52 insertions(+), 17 deletions(-) diff --git a/ftwhttp/header.go b/ftwhttp/header.go index 9599b4eb..3837b1b2 100644 --- a/ftwhttp/header.go +++ b/ftwhttp/header.go @@ -35,9 +35,9 @@ func (w stringWriter) WriteString(s string) (n int, err error) { // Add adds the key, value pair to the header. // It appends to any existing values associated with key. -// The key is case in-sensitive +// The key is case-insensitive func (h Header) Add(key, value string) { - key = canonicalHeaderKey(key) + key = canonicalKey(key) if h.Get(key) == "" { h.Set(key, value) } @@ -46,37 +46,37 @@ func (h Header) Add(key, value string) { // Set sets the header entries associated with key to // the single element value. It replaces any existing // values associated with key. -// The key is case in-sensitive +// The key is case-insensitive func (h Header) Set(key, value string) { - h[canonicalHeaderKey(key)] = value + h[canonicalKey(key)] = value } // Get gets the first value associated with the given key. // If there are no values associated with the key, Get returns "". -// The key is case in-sensitive +// The key is case-insensitive func (h Header) Get(key string) string { if h == nil { return "" } - v := h[canonicalHeaderKey(key)] + v := h[canonicalKey(key)] return v } // Value returns the value associated with the given key. -// The key is case in-sensitive +// The key is case-insensitive func (h Header) Value(key string) string { if h == nil { return "" } - return h[canonicalHeaderKey(key)] + return h[canonicalKey(key)] } // Del deletes the value associated with key. -// The key is case in-sensitive +// The key is case-insensitive func (h Header) Del(key string) { - delete(h, canonicalHeaderKey(key)) + delete(h, canonicalKey(key)) } // Write writes a header in wire format. @@ -144,7 +144,7 @@ func (h Header) getSortedHeadersByName() []string { return keys } -// canonicalHeaderKey transforms given to the canonical form -func canonicalHeaderKey(key string) string { +// canonicalKey transforms given to the canonical form +func canonicalKey(key string) string { return textproto.CanonicalMIMEHeaderKey(key) } diff --git a/ftwhttp/header_test.go b/ftwhttp/header_test.go index 0c1b926f..3a7e9db8 100644 --- a/ftwhttp/header_test.go +++ b/ftwhttp/header_test.go @@ -104,20 +104,54 @@ func (s *headerTestSuite) TestHeaderWriteString() { } } -func (s *headerTestSuite) TestHeaderSetGet() { +func (s *headerTestSuite) TestHeaderAdd() { h := Header{ "Custom": "Value", } h.Add("Other", "Value") - value := h.Get("Other") + value := h["Other"] s.Equalf("Value", value, "got: %s, want: %s\n", value, "Value") +} + +func (s *headerTestSuite) TestHeaderAdd_CaseInsensitiveKey() { + h := Header{} - // Case in-sensitive h.Add("camel-Header", "Value") - value = h.Get("Camel-header") + value := h["Camel-Header"] + s.Equalf("Value", value, "got: %s, want: %s\n", value, "Value") + + h.Add("headerwithouthyphens", "Value2") + value = h["headerwithouthyphens"] + s.Equalf("", value, "got: %s, want: %s\n", value, "") + value = h["Headerwithouthyphens"] + s.Equalf("Value2", value, "got: %s, want: %s\n", value, "Value2") +} + +func (s *headerTestSuite) TestHeaderGet() { + h := Header{ + "Custom": "Value", + } + value := h.Get("Custom") s.Equalf("Value", value, "got: %s, want: %s\n", value, "Value") } +func (s *headerTestSuite) TestHeaderGet_CaseInsensitiveKey() { + h := Header{ + "Custom": "Value", + "Custom-Header": "Value2", + "case-sensitive-Header-Key": "Value3", + } + + value := h.Get("Custom") + s.Equalf("Value", value, "got: %s, want: %s\n", value, "Value") + + value = h.Get("Custom-Header") + s.Equalf("Value2", value, "got: %s, want: %s\n", value, "Value2") + + value = h.Get("case-sensitive-Header-Key") + s.Equalf("", value, "got: %s, want: %s\n", value, "") +} + func (s *headerTestSuite) TestHeaderDel() { for i, test := range headerWriteTests { // we clone it because we are modifying the original @@ -129,8 +163,9 @@ func (s *headerTestSuite) TestHeaderDel() { s.Equalf("", value, "#%d: got: %s, want: %s\n", i, value, "") } } +} - // Case in-sensitive +func (s *headerTestSuite) TestHeaderDel_CaseInsensitiveKey() { h := Header{} h.Add("content-Type", "Value") h.Del("Content-type")