@@ -22,7 +22,6 @@ package ws
22
22
23
23
import (
24
24
"context"
25
- "fmt"
26
25
"io"
27
26
"net"
28
27
"net/http"
@@ -45,32 +44,6 @@ import (
45
44
k6types "go.k6.io/k6/lib/types"
46
45
)
47
46
48
- const (
49
- DummyCDPSessionID = "session_id_0123456789"
50
- DummyCDPTargetID = "target_id_0123456789"
51
- DummyCDPBrowserContextID = "browser_context_id_0123456789"
52
- WebSocketServerURL = "wsbin.local"
53
- )
54
-
55
- var (
56
- TargetAttachedToTargetEvent = fmt .Sprintf (`
57
- {
58
- "sessionId": "%s",
59
- "targetInfo": {
60
- "targetId": "%s",
61
- "type": "page",
62
- "title": "",
63
- "url": "about:blank",
64
- "attached": true,
65
- "browserContextId": "%s"
66
- },
67
- "waitingForDebugger": false
68
- }
69
- ` , DummyCDPSessionID , DummyCDPTargetID , DummyCDPBrowserContextID )
70
-
71
- TargetAttachedToTargetResult = fmt .Sprintf (`{"sessionId":"%s"}` , DummyCDPSessionID )
72
- )
73
-
74
47
// Server can be used as a test alternative to a real CDP compatible browser.
75
48
type Server struct {
76
49
t testing.TB
@@ -104,8 +77,10 @@ func NewServer(t testing.TB, opts ...func(*Server)) *Server {
104
77
KeepAlive : 10 * time .Second ,
105
78
DualStack : true ,
106
79
}, k6netext .NewResolver (net .LookupIP , 0 , k6types .DNSfirst , k6types .DNSpreferIPv4 ))
80
+
81
+ const wsURL = "wsbin.local"
107
82
dialer .Hosts = map [string ]* k6lib.HostAddress {
108
- WebSocketServerURL : domain ,
83
+ wsURL : domain ,
109
84
}
110
85
111
86
// Pre-configure the HTTP client transport with the dialer and TLS config (incl. HTTP2 support)
@@ -279,6 +254,27 @@ func WithCDPHandler(
279
254
280
255
// CDPDefaultHandler is a default handler for the CDP WS server.
281
256
func CDPDefaultHandler (conn * websocket.Conn , msg * cdproto.Message , writeCh chan cdproto.Message , done chan struct {}) {
257
+ const (
258
+ targetAttachedToTargetEvent = `
259
+ {
260
+ "sessionId": "session_id_0123456789",
261
+ "targetInfo": {
262
+ "targetId": "target_id_0123456789",
263
+ "type": "page",
264
+ "title": "",
265
+ "url": "about:blank",
266
+ "attached": true,
267
+ "browserContextId": "browser_context_id_0123456789"
268
+ },
269
+ "waitingForDebugger": false
270
+ }`
271
+
272
+ targetAttachedToTargetResult = `
273
+ {
274
+ "sessionId":"session_id_0123456789"
275
+ }`
276
+ )
277
+
282
278
if msg .SessionID != "" && msg .Method != "" {
283
279
switch msg .Method {
284
280
default :
@@ -292,12 +288,12 @@ func CDPDefaultHandler(conn *websocket.Conn, msg *cdproto.Message, writeCh chan
292
288
case cdproto .MethodType (cdproto .CommandTargetAttachToTarget ):
293
289
writeCh <- cdproto.Message {
294
290
Method : cdproto .EventTargetAttachedToTarget ,
295
- Params : easyjson .RawMessage ([]byte (TargetAttachedToTargetEvent )),
291
+ Params : easyjson .RawMessage ([]byte (targetAttachedToTargetEvent )),
296
292
}
297
293
writeCh <- cdproto.Message {
298
294
ID : msg .ID ,
299
295
SessionID : msg .SessionID ,
300
- Result : easyjson .RawMessage ([]byte (TargetAttachedToTargetResult )),
296
+ Result : easyjson .RawMessage ([]byte (targetAttachedToTargetResult )),
301
297
}
302
298
default :
303
299
writeCh <- cdproto.Message {
0 commit comments