@@ -4,14 +4,13 @@ import (
44 "math"
55 "strings"
66 "testing"
7+
8+ "github.com/google/go-cmp/cmp"
79)
810
911func TestCloseError (t * testing.T ) {
1012 t .Parallel ()
1113
12- // Other parts of close error are tested by websocket_test.go right now
13- // with the autobahn tests.
14-
1514 testCases := []struct {
1615 name string
1716 ce CloseError
@@ -50,7 +49,108 @@ func TestCloseError(t *testing.T) {
5049
5150 _ , err := tc .ce .bytes ()
5251 if (err == nil ) != tc .success {
53- t .Fatalf ("unexpected error value: %v" , err )
52+ t .Fatalf ("unexpected error value: %+v" , err )
53+ }
54+ })
55+ }
56+ }
57+
58+ func Test_parseClosePayload (t * testing.T ) {
59+ t .Parallel ()
60+
61+ testCases := []struct {
62+ name string
63+ p []byte
64+ success bool
65+ ce CloseError
66+ }{
67+ {
68+ name : "normal" ,
69+ p : append ([]byte {0x3 , 0xE8 }, []byte ("hello" )... ),
70+ success : true ,
71+ ce : CloseError {
72+ Code : StatusNormalClosure ,
73+ Reason : "hello" ,
74+ },
75+ },
76+ {
77+ name : "nothing" ,
78+ success : true ,
79+ ce : CloseError {
80+ Code : StatusNoStatusRcvd ,
81+ },
82+ },
83+ {
84+ name : "oneByte" ,
85+ p : []byte {0 },
86+ success : false ,
87+ },
88+ {
89+ name : "badStatusCode" ,
90+ p : []byte {0x17 , 0x70 },
91+ success : false ,
92+ },
93+ }
94+
95+ for _ , tc := range testCases {
96+ tc := tc
97+ t .Run (tc .name , func (t * testing.T ) {
98+ t .Parallel ()
99+
100+ ce , err := parseClosePayload (tc .p )
101+ if (err == nil ) != tc .success {
102+ t .Fatalf ("unexpected expected error value: %+v" , err )
103+ }
104+
105+ if tc .success && tc .ce != ce {
106+ t .Fatalf ("unexpected close error: %v" , cmp .Diff (tc .ce , ce ))
107+ }
108+ })
109+ }
110+ }
111+
112+ func Test_validWireCloseCode (t * testing.T ) {
113+ t .Parallel ()
114+
115+ testCases := []struct {
116+ name string
117+ code StatusCode
118+ valid bool
119+ }{
120+ {
121+ name : "normal" ,
122+ code : StatusNormalClosure ,
123+ valid : true ,
124+ },
125+ {
126+ name : "noStatus" ,
127+ code : StatusNoStatusRcvd ,
128+ valid : false ,
129+ },
130+ {
131+ name : "3000" ,
132+ code : 3000 ,
133+ valid : true ,
134+ },
135+ {
136+ name : "4999" ,
137+ code : 4999 ,
138+ valid : true ,
139+ },
140+ {
141+ name : "unknown" ,
142+ code : 5000 ,
143+ valid : false ,
144+ },
145+ }
146+
147+ for _ , tc := range testCases {
148+ tc := tc
149+ t .Run (tc .name , func (t * testing.T ) {
150+ t .Parallel ()
151+
152+ if valid := validWireCloseCode (tc .code ); tc .valid != valid {
153+ t .Fatalf ("expected %v for %v but got %v" , tc .valid , tc .code , valid )
54154 }
55155 })
56156 }
0 commit comments