Skip to content

Commit 46d22f8

Browse files
authored
Add unit test for pkg/util/http.go (#4770)
Signed-off-by: zouyu <[email protected]>
1 parent 8773416 commit 46d22f8

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed

pkg/util/http_test.go

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package util
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
httpPb "github.com/linkerd/linkerd2-proxy-api/go/http_types"
8+
)
9+
10+
func TestParseScheme(t *testing.T) {
11+
cases := []struct {
12+
scheme string
13+
expected *httpPb.Scheme
14+
}{
15+
{
16+
scheme: "http",
17+
expected: &httpPb.Scheme{
18+
Type: &httpPb.Scheme_Registered_{
19+
Registered: 0,
20+
},
21+
},
22+
},
23+
{
24+
scheme: "https",
25+
expected: &httpPb.Scheme{
26+
Type: &httpPb.Scheme_Registered_{
27+
Registered: 1,
28+
},
29+
},
30+
},
31+
{
32+
scheme: "unknown",
33+
expected: &httpPb.Scheme{
34+
Type: &httpPb.Scheme_Unregistered{
35+
Unregistered: "UNKNOWN",
36+
},
37+
},
38+
},
39+
}
40+
41+
for _, c := range cases {
42+
c := c
43+
t.Run(c.scheme, func(t *testing.T) {
44+
got := ParseScheme(c.scheme)
45+
if !reflect.DeepEqual(c.expected, got) {
46+
t.Errorf("expected: %v, got: %v", c.expected, got)
47+
}
48+
})
49+
}
50+
}
51+
52+
func TestParseMethod(t *testing.T) {
53+
cases := []struct {
54+
method string
55+
expected *httpPb.HttpMethod
56+
}{
57+
{
58+
method: "get",
59+
expected: &httpPb.HttpMethod{
60+
Type: &httpPb.HttpMethod_Registered_{
61+
Registered: 0,
62+
},
63+
},
64+
},
65+
{
66+
method: "post",
67+
expected: &httpPb.HttpMethod{
68+
Type: &httpPb.HttpMethod_Registered_{
69+
Registered: 1,
70+
},
71+
},
72+
},
73+
{
74+
method: "put",
75+
expected: &httpPb.HttpMethod{
76+
Type: &httpPb.HttpMethod_Registered_{
77+
Registered: 2,
78+
},
79+
},
80+
},
81+
{
82+
method: "delete",
83+
expected: &httpPb.HttpMethod{
84+
Type: &httpPb.HttpMethod_Registered_{
85+
Registered: 3,
86+
},
87+
},
88+
},
89+
{
90+
method: "patch",
91+
expected: &httpPb.HttpMethod{
92+
Type: &httpPb.HttpMethod_Registered_{
93+
Registered: 4,
94+
},
95+
},
96+
},
97+
{
98+
method: "options",
99+
expected: &httpPb.HttpMethod{
100+
Type: &httpPb.HttpMethod_Registered_{
101+
Registered: 5,
102+
},
103+
},
104+
},
105+
{
106+
method: "connect",
107+
expected: &httpPb.HttpMethod{
108+
Type: &httpPb.HttpMethod_Registered_{
109+
Registered: 6,
110+
},
111+
},
112+
},
113+
{
114+
method: "head",
115+
expected: &httpPb.HttpMethod{
116+
Type: &httpPb.HttpMethod_Registered_{
117+
Registered: 7,
118+
},
119+
},
120+
},
121+
{
122+
method: "trace",
123+
expected: &httpPb.HttpMethod{
124+
Type: &httpPb.HttpMethod_Registered_{
125+
Registered: 8,
126+
},
127+
},
128+
},
129+
{
130+
method: "unknown",
131+
expected: &httpPb.HttpMethod{
132+
Type: &httpPb.HttpMethod_Unregistered{
133+
Unregistered: "UNKNOWN",
134+
},
135+
},
136+
},
137+
}
138+
139+
for _, c := range cases {
140+
c := c
141+
t.Run(c.method, func(t *testing.T) {
142+
got := ParseMethod(c.method)
143+
if !reflect.DeepEqual(c.expected, got) {
144+
t.Errorf("expected: %v, got: %v", c.expected, got)
145+
}
146+
})
147+
}
148+
}

0 commit comments

Comments
 (0)