forked from googollee/go-socket.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_test.go
105 lines (80 loc) · 2.58 KB
/
handler_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package socketio
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
"net/http"
"io"
"github.com/googollee/go-engine.io"
)
type FakeBroadcastAdaptor struct {}
func (f *FakeBroadcastAdaptor) Join(room string, socket Socket) error {
return nil
}
func (f *FakeBroadcastAdaptor) Leave(room string, socket Socket) error {
return nil
}
func (f *FakeBroadcastAdaptor) Send(ignore Socket, room, event string, args ...interface{}) error {
return nil
}
type FakeReadCloser struct {}
func (fr *FakeReadCloser) Read(p []byte) (n int, err error) {
p = append(p, byte(128))
return 1, nil
}
func (fr *FakeReadCloser) Close() error {
return nil
}
type FakeWriteCloser struct {}
func (fr *FakeWriteCloser) Write(p []byte) (n int, err error) {
return len(p), nil
}
func (fr *FakeWriteCloser) Close() error {
return nil
}
type FakeSockConnection struct {}
func (f *FakeSockConnection) Id() string {
return "test1"
}
func (f *FakeSockConnection) Request() *http.Request {
return &http.Request{}
}
func (f *FakeSockConnection) Close() error {
return nil
}
func (f *FakeSockConnection) NextReader() (engineio.MessageType, io.ReadCloser, error) {
return engineio.MessageText, &FakeReadCloser{}, nil
}
func (f *FakeSockConnection) NextWriter(messageType engineio.MessageType) (io.WriteCloser, error) {
return &FakeWriteCloser{}, nil
}
func TestHandler(t *testing.T) {
//BugFix missed
//Method: handler.onPacket
//Reason: missed fallthrough after case _ACK:
//
// case _ACK:
// fallthrough <---- fixed problem
//
Convey("Call ACK handler by ACK id received from client", t, func() {
saver := &FrameSaver{}
var handlerCalled bool
baseHandlerInstance := newBaseHandler("some:event", &FakeBroadcastAdaptor{})
socketInstance := newSocket(&FakeSockConnection{}, baseHandlerInstance)
c, _ := newCaller(func () {handlerCalled = true})
socketInstance.acks[0] = c
socketInstance.onPacket(newDecoder(saver), &packet{Type:_ACK, Id:0, Data:"[]", NSP:"/"})
So(len(socketInstance.acks), ShouldEqual, 0)
So(handlerCalled, ShouldBeTrue)
})
Convey("Call BINARY ACK handler by BINARY ACK id received from client", t, func() {
saver := &FrameSaver{}
var handlerCalled bool
baseHandlerInstance := newBaseHandler("some:event", &FakeBroadcastAdaptor{})
socketInstance := newSocket(&FakeSockConnection{}, baseHandlerInstance)
c, _ := newCaller(func () {handlerCalled = true})
socketInstance.acks[0] = c
socketInstance.onPacket(newDecoder(saver), &packet{Type:_BINARY_ACK, Id:0, Data:"[]", NSP:"/"})
So(len(socketInstance.acks), ShouldEqual, 0)
So(handlerCalled, ShouldBeTrue)
})
}