1
16
17 package http
18
19 import (
20 "net/http"
21 "reflect"
22 "testing"
23
24 v1 "k8s.io/api/core/v1"
25 )
26
27 func TestFormatURL(t *testing.T) {
28 testCases := []struct {
29 scheme string
30 host string
31 port int
32 path string
33 result string
34 }{
35 {"http", "localhost", 93, "", "http://localhost:93"},
36 {"https", "localhost", 93, "/path", "https://localhost:93/path"},
37 {"http", "localhost", 93, "?foo", "http://localhost:93?foo"},
38 {"https", "localhost", 93, "/path?bar", "https://localhost:93/path?bar"},
39 }
40 for _, test := range testCases {
41 url := formatURL(test.scheme, test.host, test.port, test.path)
42 if url.String() != test.result {
43 t.Errorf("Expected %s, got %s", test.result, url.String())
44 }
45 }
46 }
47
48 func Test_v1HeaderToHTTPHeader(t *testing.T) {
49 tests := []struct {
50 name string
51 headerList []v1.HTTPHeader
52 want http.Header
53 }{
54 {
55 name: "not empty input",
56 headerList: []v1.HTTPHeader{
57 {Name: "Connection", Value: "Keep-Alive"},
58 {Name: "Content-Type", Value: "text/html"},
59 {Name: "Accept-Ranges", Value: "bytes"},
60 },
61 want: http.Header{
62 "Connection": {"Keep-Alive"},
63 "Content-Type": {"text/html"},
64 "Accept-Ranges": {"bytes"},
65 },
66 },
67 {
68 name: "case insensitive",
69 headerList: []v1.HTTPHeader{
70 {Name: "HOST", Value: "example.com"},
71 {Name: "FOO-bAR", Value: "value"},
72 },
73 want: http.Header{
74 "Host": {"example.com"},
75 "Foo-Bar": {"value"},
76 },
77 },
78 {
79 name: "empty input",
80 headerList: []v1.HTTPHeader{},
81 want: http.Header{},
82 },
83 }
84 for _, tt := range tests {
85 t.Run(tt.name, func(t *testing.T) {
86 if got := v1HeaderToHTTPHeader(tt.headerList); !reflect.DeepEqual(got, tt.want) {
87 t.Errorf("v1HeaderToHTTPHeader() = %v, want %v", got, tt.want)
88 }
89 })
90 }
91 }
92
93 func TestHeaderConversion(t *testing.T) {
94 testCases := []struct {
95 headers []v1.HTTPHeader
96 expected http.Header
97 }{
98 {
99 []v1.HTTPHeader{
100 {
101 Name: "Accept",
102 Value: "application/json",
103 },
104 },
105 http.Header{
106 "Accept": []string{"application/json"},
107 },
108 },
109 {
110 []v1.HTTPHeader{
111 {Name: "accept", Value: "application/json"},
112 },
113 http.Header{
114 "Accept": []string{"application/json"},
115 },
116 },
117 {
118 []v1.HTTPHeader{
119 {Name: "accept", Value: "application/json"},
120 {Name: "Accept", Value: "*/*"},
121 {Name: "pragma", Value: "no-cache"},
122 {Name: "X-forwarded-for", Value: "username"},
123 },
124 http.Header{
125 "Accept": []string{"application/json", "*/*"},
126 "Pragma": []string{"no-cache"},
127 "X-Forwarded-For": []string{"username"},
128 },
129 },
130 }
131 for _, test := range testCases {
132 headers := v1HeaderToHTTPHeader(test.headers)
133 if !reflect.DeepEqual(headers, test.expected) {
134 t.Errorf("Expected %v, got %v", test.expected, headers)
135 }
136 }
137 }
138
View as plain text