@@ -53,7 +53,7 @@ const (
53
53
)
54
54
55
55
var (
56
- CDPTargetAttachedToTargetRequest = fmt .Sprintf (`
56
+ TargetAttachedToTargetEvent = fmt .Sprintf (`
57
57
{
58
58
"sessionId": "%s",
59
59
"targetInfo": {
@@ -68,19 +68,19 @@ var (
68
68
}
69
69
` , DummyCDPSessionID , DummyCDPTargetID , DummyCDPBrowserContextID )
70
70
71
- CDPTargetAttachedToTargetResponse = fmt .Sprintf (`{"sessionId":"%s"}` , DummyCDPSessionID )
71
+ TargetAttachedToTargetResult = fmt .Sprintf (`{"sessionId":"%s"}` , DummyCDPSessionID )
72
72
)
73
73
74
- // NewWSServerWithCDPHandler creates a WS test server with a custom CDP handler function
75
- func NewWSServerWithCDPHandler (
74
+ // NewServerWithCDPHandler creates a WS test server with a custom CDP handler function
75
+ func NewServerWithCDPHandler (
76
76
t testing.TB ,
77
77
fn func (conn * websocket.Conn , msg * cdproto.Message , writeCh chan cdproto.Message , done chan struct {}),
78
- cmdsReceived * []cdproto.MethodType ) * WSTestServer {
79
- return NewWSServer (t , "/cdp" , getWebsocketHandlerCDP (fn , cmdsReceived ))
78
+ cmdsReceived * []cdproto.MethodType ) * Server {
79
+ return NewServer (t , "/cdp" , getWebsocketHandlerCDP (fn , cmdsReceived ))
80
80
}
81
81
82
- // WSTestServer can be used as a test alternative to a real CDP compatible browser.
83
- type WSTestServer struct {
82
+ // Server can be used as a test alternative to a real CDP compatible browser.
83
+ type Server struct {
84
84
Mux * http.ServeMux
85
85
ServerHTTP * httptest.Server
86
86
Dialer * k6netext.Dialer
@@ -89,18 +89,18 @@ type WSTestServer struct {
89
89
Cleanup func ()
90
90
}
91
91
92
- // NewWSServerWithClosureAbnormal creates a WS test server with abnormal closure behavior
93
- func NewWSServerWithClosureAbnormal (t testing.TB ) * WSTestServer {
94
- return NewWSServer (t , "/closure-abnormal" , getWebsocketHandlerAbnormalClosure ())
92
+ // NewServerWithClosureAbnormal creates a WS test server with abnormal closure behavior
93
+ func NewServerWithClosureAbnormal (t testing.TB ) * Server {
94
+ return NewServer (t , "/closure-abnormal" , getWebsocketHandlerAbnormalClosure ())
95
95
}
96
96
97
- // NewWSServerWithEcho creates a WS test server with an echo handler
98
- func NewWSServerWithEcho (t testing.TB ) * WSTestServer {
99
- return NewWSServer (t , "/echo" , getWebsocketHandlerEcho ())
97
+ // NewServerWithEcho creates a WS test server with an echo handler
98
+ func NewServerWithEcho (t testing.TB ) * Server {
99
+ return NewServer (t , "/echo" , getWebsocketHandlerEcho ())
100
100
}
101
101
102
- // NewWSServer returns a fully configured and running WS test server
103
- func NewWSServer (t testing.TB , path string , handler http.Handler ) * WSTestServer {
102
+ // NewServer returns a fully configured and running WS test server
103
+ func NewServer (t testing.TB , path string , handler http.Handler ) * Server {
104
104
t .Helper ()
105
105
106
106
// Create a http.ServeMux and set the httpbin handler as the default
@@ -135,7 +135,7 @@ func NewWSServer(t testing.TB, path string, handler http.Handler) *WSTestServer
135
135
require .NoError (t , http2 .ConfigureTransport (transport ))
136
136
137
137
ctx , ctxCancel := context .WithCancel (context .Background ())
138
- return & WSTestServer {
138
+ return & Server {
139
139
Mux : mux ,
140
140
ServerHTTP : httpSrv ,
141
141
Dialer : dialer ,
@@ -148,6 +148,8 @@ func NewWSServer(t testing.TB, path string, handler http.Handler) *WSTestServer
148
148
}
149
149
}
150
150
151
+ // TODO: make a websocket.Conn wrapper for CDPxxx methods
152
+
151
153
// CDPDefaultCDPHandler is a default handler for the CDP WS server
152
154
func CDPDefaultHandler (conn * websocket.Conn , msg * cdproto.Message , writeCh chan cdproto.Message , done chan struct {}) {
153
155
if msg .SessionID != "" && msg .Method != "" {
@@ -163,12 +165,12 @@ func CDPDefaultHandler(conn *websocket.Conn, msg *cdproto.Message, writeCh chan
163
165
case cdproto .MethodType (cdproto .CommandTargetAttachToTarget ):
164
166
writeCh <- cdproto.Message {
165
167
Method : cdproto .EventTargetAttachedToTarget ,
166
- Params : easyjson .RawMessage ([]byte (CDPTargetAttachedToTargetRequest )),
168
+ Params : easyjson .RawMessage ([]byte (TargetAttachedToTargetEvent )),
167
169
}
168
170
writeCh <- cdproto.Message {
169
171
ID : msg .ID ,
170
172
SessionID : msg .SessionID ,
171
- Result : easyjson .RawMessage ([]byte (CDPTargetAttachedToTargetResponse )),
173
+ Result : easyjson .RawMessage ([]byte (TargetAttachedToTargetResult )),
172
174
}
173
175
default :
174
176
writeCh <- cdproto.Message {
0 commit comments