-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock.go
73 lines (57 loc) · 1.42 KB
/
mock.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
package websockets
import (
"fmt"
"github.com/mitchellh/mapstructure"
)
// WebsocketConnMock mock struct for websocket connections
// You can set `DoError` to let any method that can return an error return errors
type WebsocketConnMock struct {
Read chan interface{}
Write chan interface{}
LocalData map[string]interface{}
DoError bool
}
// NewWebsocketConnMock constructor
func NewWebsocketConnMock() *WebsocketConnMock {
return &WebsocketConnMock{
Read: make(chan interface{}),
Write: make(chan interface{}),
LocalData: make(map[string]interface{}),
DoError: false,
}
}
// Locals access local data
func (c *WebsocketConnMock) Locals(key string) interface{} {
return c.LocalData[key]
}
// ReadJSON receives data from the Write chan
func (c *WebsocketConnMock) ReadJSON(v interface{}) error {
data := <-c.Write
if c.DoError {
return fmt.Errorf("error")
}
return mapstructure.Decode(data, v)
}
// WriteJSON writes data to the Read chan
func (c *WebsocketConnMock) WriteJSON(v interface{}) error {
if c.DoError {
return fmt.Errorf("error")
}
c.Read <- v
return nil
}
// Close the connection
func (c *WebsocketConnMock) Close() error {
if c.DoError {
return fmt.Errorf("error")
}
return nil
}
// WriteMessage to the connection
func (c *WebsocketConnMock) WriteMessage(messageType int, data []byte) error {
if c.DoError {
return fmt.Errorf("error")
}
c.Read <- data
return nil
}