-
Notifications
You must be signed in to change notification settings - Fork 166
/
test_helpers.go
164 lines (139 loc) · 4.75 KB
/
test_helpers.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package routes
import (
"bufio"
"bytes"
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/onflow/flow-go/access"
"github.com/onflow/flow-go/access/mock"
"github.com/onflow/flow-go/engine/access/state_stream"
"github.com/onflow/flow-go/engine/access/state_stream/backend"
"github.com/onflow/flow-go/engine/access/subscription"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module/metrics"
"github.com/onflow/flow-go/utils/unittest"
)
const (
ExpandableFieldPayload = "payload"
ExpandableExecutionResult = "execution_result"
sealedHeightQueryParam = "sealed"
finalHeightQueryParam = "final"
startHeightQueryParam = "start_height"
endHeightQueryParam = "end_height"
heightQueryParam = "height"
startBlockIdQueryParam = "start_block_id"
eventTypesQueryParams = "event_types"
addressesQueryParams = "addresses"
contractsQueryParams = "contracts"
heartbeatIntervalQueryParam = "heartbeat_interval"
)
// fakeNetConn implements a mocked ws connection that can be injected in testing logic.
type fakeNetConn struct {
io.Writer
closed chan struct{}
}
var _ net.Conn = (*fakeNetConn)(nil)
// Close closes the fakeNetConn and signals its closure by closing the "closed" channel.
func (c fakeNetConn) Close() error {
select {
case <-c.closed:
default:
close(c.closed)
}
return nil
}
func (c fakeNetConn) LocalAddr() net.Addr { return localAddr }
func (c fakeNetConn) RemoteAddr() net.Addr { return remoteAddr }
func (c fakeNetConn) SetDeadline(t time.Time) error { return nil }
func (c fakeNetConn) SetReadDeadline(t time.Time) error { return nil }
func (c fakeNetConn) SetWriteDeadline(t time.Time) error { return nil }
func (c fakeNetConn) Read(p []byte) (n int, err error) {
<-c.closed
return 0, fmt.Errorf("closed")
}
type fakeAddr int
var (
localAddr = fakeAddr(1)
remoteAddr = fakeAddr(2)
)
func (a fakeAddr) Network() string {
return "net"
}
func (a fakeAddr) String() string {
return "str"
}
// testHijackResponseRecorder is a custom ResponseRecorder that implements the http.Hijacker interface
// for testing WebSocket connections and hijacking.
type testHijackResponseRecorder struct {
*httptest.ResponseRecorder
closed chan struct{}
responseBuff *bytes.Buffer
}
var _ http.Hijacker = (*testHijackResponseRecorder)(nil)
// Hijack implements the http.Hijacker interface by returning a fakeNetConn and a bufio.ReadWriter
// that simulate a hijacked connection.
func (w *testHijackResponseRecorder) Hijack() (net.Conn, *bufio.ReadWriter, error) {
br := bufio.NewReaderSize(strings.NewReader(""), subscription.DefaultSendBufferSize)
bw := bufio.NewWriterSize(&bytes.Buffer{}, subscription.DefaultSendBufferSize)
w.responseBuff = bytes.NewBuffer(make([]byte, 0))
w.closed = make(chan struct{}, 1)
return fakeNetConn{w.responseBuff, w.closed}, bufio.NewReadWriter(br, bw), nil
}
func (w *testHijackResponseRecorder) Close() error {
select {
case <-w.closed:
default:
close(w.closed)
}
return nil
}
// newTestHijackResponseRecorder creates a new instance of testHijackResponseRecorder.
func newTestHijackResponseRecorder() *testHijackResponseRecorder {
return &testHijackResponseRecorder{
ResponseRecorder: httptest.NewRecorder(),
}
}
func executeRequest(req *http.Request, backend access.API) *httptest.ResponseRecorder {
router := NewRouterBuilder(
unittest.Logger(),
metrics.NewNoopCollector(),
).AddRestRoutes(
backend,
flow.Testnet.Chain(),
).Build()
rr := httptest.NewRecorder()
router.ServeHTTP(rr, req)
return rr
}
func executeWsRequest(req *http.Request, stateStreamApi state_stream.API, responseRecorder *testHijackResponseRecorder, chain flow.Chain) {
restCollector := metrics.NewNoopCollector()
config := backend.Config{
EventFilterConfig: state_stream.DefaultEventFilterConfig,
MaxGlobalStreams: subscription.DefaultMaxGlobalStreams,
HeartbeatInterval: subscription.DefaultHeartbeatInterval,
}
router := NewRouterBuilder(unittest.Logger(), restCollector).AddWsRoutes(
stateStreamApi,
chain, config).Build()
router.ServeHTTP(responseRecorder, req)
}
func assertOKResponse(t *testing.T, req *http.Request, expectedRespBody string, backend *mock.API) {
assertResponse(t, req, http.StatusOK, expectedRespBody, backend)
}
func assertResponse(t *testing.T, req *http.Request, status int, expectedRespBody string, backend *mock.API) {
rr := executeRequest(req, backend)
actualResponseBody := rr.Body.String()
require.JSONEq(t,
expectedRespBody,
actualResponseBody,
fmt.Sprintf("Failed Request: %s\nExpected JSON:\n %s \nActual JSON:\n %s\n", req.URL, expectedRespBody, actualResponseBody),
)
require.Equal(t, status, rr.Code)
}