-
Notifications
You must be signed in to change notification settings - Fork 42
/
server.go
287 lines (258 loc) · 6.78 KB
/
server.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Package ws provides a test WebSocket server.
package ws
import (
"context"
"io"
"net"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
k6netext "go.k6.io/k6/lib/netext"
k6types "go.k6.io/k6/lib/types"
"github.com/chromedp/cdproto"
"github.com/gorilla/websocket"
"github.com/mailru/easyjson"
"github.com/mailru/easyjson/jlexer"
"github.com/mailru/easyjson/jwriter"
"github.com/mccutchen/go-httpbin/httpbin"
"github.com/stretchr/testify/require"
"golang.org/x/net/http2"
)
// Server can be used as a test alternative to a real CDP compatible browser.
type Server struct {
t testing.TB
Mux *http.ServeMux
ServerHTTP *httptest.Server
Dialer *k6netext.Dialer
HTTPTransport *http.Transport
Context context.Context
}
// NewServer returns a fully configured and running WS test server.
func NewServer(t testing.TB, opts ...func(*Server)) *Server {
t.Helper()
// Create a http.ServeMux and set the httpbin handler as the default
mux := http.NewServeMux()
mux.Handle("/", httpbin.New().Handler())
// Initialize the HTTP server and get its details
server := httptest.NewServer(mux)
url, err := url.Parse(server.URL)
require.NoError(t, err)
ip := net.ParseIP(url.Hostname())
require.NotNil(t, ip)
domain, err := k6types.NewHost(ip, "")
require.NoError(t, err)
// Set up the dialer with shorter timeouts and the custom domains
dialer := k6netext.NewDialer(net.Dialer{
Timeout: 2 * time.Second,
KeepAlive: 10 * time.Second,
DualStack: true,
}, k6netext.NewResolver(net.LookupIP, 0, k6types.DNSfirst, k6types.DNSpreferIPv4))
const wsURL = "wsbin.local"
dialer.Hosts, err = k6types.NewHosts(map[string]k6types.Host{
wsURL: *domain,
})
require.NoError(t, err, "failed to set up dialer hosts")
// Pre-configure the HTTP client transport with the dialer and TLS config (incl. HTTP2 support)
transport := &http.Transport{
DialContext: dialer.DialContext,
}
require.NoError(t, http2.ConfigureTransport(transport))
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(func() {
server.Close()
cancel()
})
s := &Server{
t: t,
Mux: mux,
ServerHTTP: server,
Dialer: dialer,
HTTPTransport: transport,
Context: ctx,
}
for _, opt := range opts {
opt(s)
}
return s
}
// WithClosureAbnormalHandler attaches an abnormal closure behavior to Server.
func WithClosureAbnormalHandler(path string) func(*Server) {
handler := func(w http.ResponseWriter, req *http.Request) {
conn, err := (&websocket.Upgrader{}).Upgrade(w, req, w.Header())
if err != nil {
// TODO: log
return
}
err = conn.Close() // This forces a connection closure without a proper WS close message exchange
if err != nil {
// TODO: log
return
}
}
return func(s *Server) {
s.Mux.Handle(path, http.HandlerFunc(handler))
}
}
// WithEchoHandler attaches an echo handler to Server.
func WithEchoHandler(path string) func(*Server) {
handler := func(w http.ResponseWriter, req *http.Request) {
conn, err := (&websocket.Upgrader{}).Upgrade(w, req, w.Header())
if err != nil {
return
}
messageType, r, e := conn.NextReader()
if e != nil {
return
}
var wc io.WriteCloser
wc, err = conn.NextWriter(messageType)
if err != nil {
return
}
if _, err = io.Copy(wc, r); err != nil {
return
}
if err = wc.Close(); err != nil {
return
}
err = conn.WriteControl(websocket.CloseMessage,
websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""),
time.Now().Add(10*time.Second),
)
if err != nil {
return
}
}
return func(s *Server) {
s.Mux.Handle(path, http.HandlerFunc(handler))
}
}
// WithCDPHandler attaches a custom CDP handler function to Server.
func WithCDPHandler(
path string,
fn func(conn *websocket.Conn, msg *cdproto.Message, writeCh chan cdproto.Message, done chan struct{}),
cmdsReceived *[]cdproto.MethodType,
) func(*Server) {
handler := func(w http.ResponseWriter, req *http.Request) {
conn, err := (&websocket.Upgrader{}).Upgrade(w, req, w.Header())
if err != nil {
return
}
done := make(chan struct{})
writeCh := make(chan cdproto.Message)
go func() {
read := func(conn *websocket.Conn) (*cdproto.Message, error) {
_, buf, err := conn.ReadMessage()
if err != nil {
return nil, err
}
var msg cdproto.Message
decoder := jlexer.Lexer{Data: buf}
msg.UnmarshalEasyJSON(&decoder)
if err := decoder.Error(); err != nil {
return nil, err
}
return &msg, nil
}
for {
select {
case <-done:
return
default:
}
msg, err := read(conn)
if err != nil {
close(done)
return
}
if msg.Method != "" && cmdsReceived != nil {
*cmdsReceived = append(*cmdsReceived, msg.Method)
}
fn(conn, msg, writeCh, done)
}
}()
go func() {
write := func(conn *websocket.Conn, msg *cdproto.Message) {
encoder := jwriter.Writer{}
msg.MarshalEasyJSON(&encoder)
if err := encoder.Error; err != nil {
return
}
writer, err := conn.NextWriter(websocket.TextMessage)
if err != nil {
return
}
if _, err := encoder.DumpTo(writer); err != nil {
return
}
if err := writer.Close(); err != nil {
return
}
}
for {
select {
case msg := <-writeCh:
write(conn, &msg)
case <-done:
return
}
}
}()
<-done // Wait for done channel to be closed before closing connection
}
return func(s *Server) {
s.Mux.Handle(path, http.HandlerFunc(handler))
}
}
// CDPDefaultHandler is a default handler for the CDP WS server.
func CDPDefaultHandler(conn *websocket.Conn, msg *cdproto.Message, writeCh chan cdproto.Message, done chan struct{}) {
const (
targetAttachedToTargetEvent = `
{
"sessionId": "session_id_0123456789",
"targetInfo": {
"targetId": "target_id_0123456789",
"type": "page",
"title": "",
"url": "about:blank",
"attached": true,
"browserContextId": "browser_context_id_0123456789"
},
"waitingForDebugger": false
}`
targetAttachedToTargetResult = `
{
"sessionId":"session_id_0123456789"
}`
)
if msg.SessionID != "" && msg.Method != "" {
switch msg.Method {
default:
writeCh <- cdproto.Message{
ID: msg.ID,
SessionID: msg.SessionID,
}
}
} else if msg.Method != "" {
switch msg.Method {
case cdproto.MethodType(cdproto.CommandTargetAttachToTarget):
writeCh <- cdproto.Message{
Method: cdproto.EventTargetAttachedToTarget,
Params: easyjson.RawMessage([]byte(targetAttachedToTargetEvent)),
}
writeCh <- cdproto.Message{
ID: msg.ID,
SessionID: msg.SessionID,
Result: easyjson.RawMessage([]byte(targetAttachedToTargetResult)),
}
default:
writeCh <- cdproto.Message{
ID: msg.ID,
SessionID: msg.SessionID,
Result: easyjson.RawMessage([]byte("{}")),
}
}
}
}