Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

加单元测试代码 #22

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package quickws

import (
"net/http"
"reflect"
"testing"
)

Expand All @@ -41,3 +43,110 @@ func Test_getHttpErrMsg(t *testing.T) {
}
})
}

func TestStringToBytes(t *testing.T) {
type args struct {
s string
}
tests := []struct {
name string
args args
wantB []byte
}{
{
name: "test1",
args: args{s: "test1"},
wantB: []byte("test1"),
},
{
name: "test2",
args: args{s: "test2"},
wantB: []byte("test2"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotB := StringToBytes(tt.args.s); !reflect.DeepEqual(gotB, tt.wantB) {
t.Errorf("StringToBytes() = %v, want %v", gotB, tt.wantB)
}
})
}
}

func Test_secWebSocketAccept(t *testing.T) {
tests := []struct {
name string
want string
}{
{name: ">0"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := secWebSocketAccept(); len(got) == 0 {
t.Errorf("secWebSocketAccept() = %v, want %v", got, tt.want)
}
})
}
}

func Test_secWebSocketAcceptVal(t *testing.T) {
type args struct {
val string
}
tests := []struct {
name string
args args
want string
}{
{name: "test1", args: args{val: "test1"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := secWebSocketAcceptVal(tt.args.val); len(got) == 0 {
t.Errorf("secWebSocketAcceptVal() = %v, want %v", got, tt.want)
}
})
}
}

func Test_needDecompression(t *testing.T) {
type args struct {
header http.Header
}
tests := []struct {
name string
args args
want bool
}{
{name: "test1", args: args{header: http.Header{"Sec-Websocket-Extensions": {"permessage-deflate; server_no_context_takeover; client_no_context_takeover"}}}, want: true},
{name: "test2", args: args{header: http.Header{"Sec-Websocket-Extensions": {"xx"}}}, want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := needDecompression(tt.args.header); got != tt.want {
t.Errorf("needDecompression() = %v, want %v", got, tt.want)
}
})
}
}

func Test_maybeCompressionDecompression(t *testing.T) {
type args struct {
header http.Header
}
tests := []struct {
name string
args args
want bool
}{
{name: "test1", args: args{header: http.Header{"Sec-Websocket-Extensions": {"permessage-deflate; server_no_context_takeover; client_no_context_takeover"}}}, want: true},
{name: "test2", args: args{header: http.Header{"Sec-Websocket-Extensions": {"xx"}}}, want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := maybeCompressionDecompression(tt.args.header); got != tt.want {
t.Errorf("maybeCompressionDecompression() = %v, want %v", got, tt.want)
}
})
}
}
Loading