/
mocks.go
139 lines (110 loc) · 3.52 KB
/
mocks.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Package mocks implements mocks.
package mocks
import (
"bytes"
"errors"
"io"
"net/http"
"time"
"github.com/gorilla/websocket"
)
var ErrReadFailed = errors.New("Read() failed")
// Conn is a mockable websocket.Conn
type Conn struct {
// CloseResult is the result of Conn.Close
CloseResult error
// MessageByteArray is the byte array returned by conn.ReadMessage and
// conn.NextReader
MessageByteArray []byte
// NextReaderMessageType is the type returned by conn.NextReader
NextReaderMessageType int
// NextReaderResult is the result returned by conn.NextReader
NextReaderResult error
// NextReaderMustFail determines if the Reader's Read() method must fail
NextReaderMustFail bool
// ReadMessageResult is the result returned by conn.ReadMessage
ReadMessageResult error
// ReadMessageType is the type returned by conn.ReadMessage
ReadMessageType int
// SetReadDeadlineResult is the result returned by conn.SetReadDeadline
SetReadDeadlineResult error
// SetWriteDeadlineResult is the result returned by conn.SetWriteDeadline
SetWriteDeadlineResult error
// WritePreparedMessageResult is the result returned by conn.WritePreparedMessage
WritePreparedMessageResult error
}
// Close closes the mocked connection
func (c *Conn) Close() error {
return c.CloseResult
}
// ReadMessage reads a message from the mocked connection
func (c *Conn) ReadMessage() (messageType int, p []byte, err error) {
return c.ReadMessageType, c.MessageByteArray, c.ReadMessageResult
}
func (c *Conn) NextReader() (messageType int, r io.Reader, err error) {
if c.NextReaderMustFail {
return c.NextReaderMessageType, &FailingReader{}, c.NextReaderResult
}
return c.NextReaderMessageType, bytes.NewReader(c.MessageByteArray),
c.NextReaderResult
}
// SetReadLimit sets the read limit of the mocked connection
func (*Conn) SetReadLimit(limit int64) {}
// SetReadDeadline sets the read deadline of the mocked connection
func (c *Conn) SetReadDeadline(t time.Time) error {
return c.SetReadDeadlineResult
}
// SetWriteDeadline sets the write deadline of the mocked connection
func (c *Conn) SetWriteDeadline(t time.Time) error {
return c.SetWriteDeadlineResult
}
// WritePreparedMessage writes a prepared message on the mocked connection
func (c *Conn) WritePreparedMessage(pm *websocket.PreparedMessage) error {
return c.WritePreparedMessageResult
}
// reponseBody is a fake HTTP response body.
type reponseBody struct {
reader io.Reader
}
// newResponseBody creates a new response body.
func newResponseBody(data []byte) io.ReadCloser {
return &reponseBody{
reader: bytes.NewReader(data),
}
}
// Read reads the response body.
func (r *reponseBody) Read(p []byte) (n int, err error) {
return r.reader.Read(p)
}
// Close closes the response body.
func (r *reponseBody) Close() error {
return nil
}
type httpTransport struct {
Response *http.Response
Error error
}
// NewHTTPClient returns a mocked *http.Client.
func NewHTTPClient(code int, body []byte, err error) *http.Client {
return &http.Client{
Transport: &httpTransport{
Error: err,
Response: &http.Response{
Body: newResponseBody(body),
StatusCode: code,
},
},
}
}
func (r *httpTransport) RoundTrip(req *http.Request) (*http.Response, error) {
// Cannot be more concise than this (i.e. `return r.Error, r.Response`) because
// http.Client.Do warns if both Error and Response are non nil
if r.Error != nil {
return nil, r.Error
}
return r.Response, nil
}
type FailingReader struct{}
func (r *FailingReader) Read(b []byte) (int, error) {
return 0, ErrReadFailed
}