forked from googollee/go-socket.io
-
Notifications
You must be signed in to change notification settings - Fork 4
/
handler_test.go
108 lines (83 loc) · 2.65 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
106
107
108
package socketio
import (
"testing"
"github.com/googollee/go-engine.io"
. "github.com/smartystreets/goconvey/convey"
"io"
"net/http"
)
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
}
func (f *FakeBroadcastAdaptor) Len(room string) int {
return 0
}
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)
})
}