-
Notifications
You must be signed in to change notification settings - Fork 10
/
fake_guacd_test.go
58 lines (51 loc) · 1.03 KB
/
fake_guacd_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
package bring
import (
"net"
"github.com/deluan/bring/protocol"
)
const disconnectOpcode = "disconnect"
type fakeServer struct {
replies map[string]string
messagesReceived []string
opcodesReceived []string
}
func (s *fakeServer) start() string {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
panic(err)
}
go func() {
for {
conn, err := ln.Accept()
if err != nil {
panic(err)
}
s.handleRequest(conn)
}
}()
return ln.Addr().String()
}
func (s *fakeServer) handleRequest(conn net.Conn) {
defer conn.Close()
io := protocol.NewInstructionIO(conn)
for {
recv, err := io.Read()
if err != nil {
return
}
opcode := recv.Opcode
_, err = io.WriteRaw([]byte(s.replies[opcode]))
if err != nil {
panic(err)
}
if opcode == disconnectOpcode {
break
}
s.messagesReceived = append(s.messagesReceived, recv.String())
s.opcodesReceived = append(s.opcodesReceived, opcode)
}
_, err := io.Write(protocol.NewInstruction(disconnectOpcode))
if err != nil {
panic(err)
}
}