Skip to content

Commit cc8b1bd

Browse files
authored
加单元测试代码 (#22)
1 parent de68b09 commit cc8b1bd

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

utils_test.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
package quickws
1616

1717
import (
18+
"net/http"
19+
"reflect"
1820
"testing"
1921
)
2022

@@ -41,3 +43,110 @@ func Test_getHttpErrMsg(t *testing.T) {
4143
}
4244
})
4345
}
46+
47+
func TestStringToBytes(t *testing.T) {
48+
type args struct {
49+
s string
50+
}
51+
tests := []struct {
52+
name string
53+
args args
54+
wantB []byte
55+
}{
56+
{
57+
name: "test1",
58+
args: args{s: "test1"},
59+
wantB: []byte("test1"),
60+
},
61+
{
62+
name: "test2",
63+
args: args{s: "test2"},
64+
wantB: []byte("test2"),
65+
},
66+
}
67+
for _, tt := range tests {
68+
t.Run(tt.name, func(t *testing.T) {
69+
if gotB := StringToBytes(tt.args.s); !reflect.DeepEqual(gotB, tt.wantB) {
70+
t.Errorf("StringToBytes() = %v, want %v", gotB, tt.wantB)
71+
}
72+
})
73+
}
74+
}
75+
76+
func Test_secWebSocketAccept(t *testing.T) {
77+
tests := []struct {
78+
name string
79+
want string
80+
}{
81+
{name: ">0"},
82+
}
83+
for _, tt := range tests {
84+
t.Run(tt.name, func(t *testing.T) {
85+
if got := secWebSocketAccept(); len(got) == 0 {
86+
t.Errorf("secWebSocketAccept() = %v, want %v", got, tt.want)
87+
}
88+
})
89+
}
90+
}
91+
92+
func Test_secWebSocketAcceptVal(t *testing.T) {
93+
type args struct {
94+
val string
95+
}
96+
tests := []struct {
97+
name string
98+
args args
99+
want string
100+
}{
101+
{name: "test1", args: args{val: "test1"}},
102+
}
103+
for _, tt := range tests {
104+
t.Run(tt.name, func(t *testing.T) {
105+
if got := secWebSocketAcceptVal(tt.args.val); len(got) == 0 {
106+
t.Errorf("secWebSocketAcceptVal() = %v, want %v", got, tt.want)
107+
}
108+
})
109+
}
110+
}
111+
112+
func Test_needDecompression(t *testing.T) {
113+
type args struct {
114+
header http.Header
115+
}
116+
tests := []struct {
117+
name string
118+
args args
119+
want bool
120+
}{
121+
{name: "test1", args: args{header: http.Header{"Sec-Websocket-Extensions": {"permessage-deflate; server_no_context_takeover; client_no_context_takeover"}}}, want: true},
122+
{name: "test2", args: args{header: http.Header{"Sec-Websocket-Extensions": {"xx"}}}, want: false},
123+
}
124+
for _, tt := range tests {
125+
t.Run(tt.name, func(t *testing.T) {
126+
if got := needDecompression(tt.args.header); got != tt.want {
127+
t.Errorf("needDecompression() = %v, want %v", got, tt.want)
128+
}
129+
})
130+
}
131+
}
132+
133+
func Test_maybeCompressionDecompression(t *testing.T) {
134+
type args struct {
135+
header http.Header
136+
}
137+
tests := []struct {
138+
name string
139+
args args
140+
want bool
141+
}{
142+
{name: "test1", args: args{header: http.Header{"Sec-Websocket-Extensions": {"permessage-deflate; server_no_context_takeover; client_no_context_takeover"}}}, want: true},
143+
{name: "test2", args: args{header: http.Header{"Sec-Websocket-Extensions": {"xx"}}}, want: false},
144+
}
145+
for _, tt := range tests {
146+
t.Run(tt.name, func(t *testing.T) {
147+
if got := maybeCompressionDecompression(tt.args.header); got != tt.want {
148+
t.Errorf("maybeCompressionDecompression() = %v, want %v", got, tt.want)
149+
}
150+
})
151+
}
152+
}

0 commit comments

Comments
 (0)