Skip to content

Commit

Permalink
Fix header implementation
Browse files Browse the repository at this point in the history
Fix comment to the case in-sensitive keys
Move out test cases to separate methods
  • Loading branch information
huberts90 committed Jul 11, 2024
1 parent 0d6d312 commit 1ccb896
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 17 deletions.
24 changes: 12 additions & 12 deletions ftwhttp/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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.
Expand Down Expand Up @@ -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)
}
45 changes: 40 additions & 5 deletions ftwhttp/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down

0 comments on commit 1ccb896

Please sign in to comment.