Skip to content

Commit 4e891ac

Browse files
committed
feat: enhance convertHeaders function and add comprehensive unit tests
- Improve `convertHeaders` function documentation for clarity - Modify `convertHeaders` function to handle edge cases with empty keys or values - Replace `strings.Split` with `strings.SplitN` to limit splits to 2 parts in `convertHeaders` - Add trimming of whitespace for keys and values in `convertHeaders` - Add new test file `proxy_test.go` with unit tests for `convertHeaders` function Signed-off-by: appleboy <[email protected]>
1 parent eb3d4f4 commit 4e891ac

File tree

2 files changed

+66
-6
lines changed

2 files changed

+66
-6
lines changed

proxy/proxy.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,35 @@ import (
1010
"golang.org/x/net/proxy"
1111
)
1212

13-
// convertHeaders takes a slice of strings representing HTTP headers in the format "key=value"
14-
// and converts them into an http.Header type. If a header string does not contain exactly one "=",
15-
// it is ignored. The resulting http.Header map is returned.
13+
// convertHeaders converts a slice of strings representing HTTP headers
14+
// into an http.Header map. Each string in the input slice should be in
15+
// the format "key=value". If a string cannot be split into exactly two
16+
// parts, or if either the key or value is empty after trimming whitespace,
17+
// that string is ignored.
18+
//
19+
// Parameters:
20+
//
21+
// headers []string - A slice of strings where each string represents an
22+
// HTTP header in the format "key=value".
23+
//
24+
// Returns:
25+
//
26+
// http.Header - A map of HTTP headers where the keys are header names
27+
// and the values are header values.
1628
func convertHeaders(headers []string) http.Header {
1729
h := make(http.Header)
1830
for _, header := range headers {
1931
// split header into key and value with = as delimiter
20-
vals := strings.Split(header, "=")
21-
if len(vals) != 2 {
32+
parts := strings.SplitN(header, "=", 2)
33+
if len(parts) != 2 {
34+
continue
35+
}
36+
key := strings.TrimSpace(parts[0])
37+
value := strings.TrimSpace(parts[1])
38+
if key == "" || value == "" {
2239
continue
2340
}
24-
h.Add(vals[0], vals[1])
41+
h.Add(key, value)
2542
}
2643
return h
2744
}

proxy/proxy_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package proxy
2+
3+
import (
4+
"net/http"
5+
"reflect"
6+
"testing"
7+
)
8+
9+
func TestConvertHeaders(t *testing.T) {
10+
testCases := []struct {
11+
headers []string
12+
expected http.Header
13+
}{
14+
{
15+
headers: []string{"Content-Type=application/json", "Authorization=Bearer token"},
16+
expected: http.Header{
17+
"Content-Type": []string{"application/json"},
18+
"Authorization": []string{"Bearer token"},
19+
},
20+
},
21+
{
22+
headers: []string{"X-Custom-Header=custom_value", "InvalidHeader"},
23+
expected: http.Header{
24+
"X-Custom-Header": []string{"custom_value"},
25+
},
26+
},
27+
{
28+
headers: []string{"KeyOnly=", "=ValueOnly"},
29+
expected: http.Header{},
30+
},
31+
{
32+
headers: []string{},
33+
expected: http.Header{},
34+
},
35+
}
36+
37+
for _, tc := range testCases {
38+
result := convertHeaders(tc.headers)
39+
if !reflect.DeepEqual(result, tc.expected) {
40+
t.Errorf("convertHeaders(%v) = %v, expected %v", tc.headers, result, tc.expected)
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)