Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit 693d6c0

Browse files
committed
Rename websocket test server identifiers
Resolves #166 (comment)
1 parent 4f3f0c0 commit 693d6c0

File tree

3 files changed

+28
-26
lines changed

3 files changed

+28
-26
lines changed

common/connection_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import (
3838
)
3939

4040
func TestConnection(t *testing.T) {
41-
server := ws.NewWSServerWithEcho(t)
41+
server := ws.NewServerWithEcho(t)
4242
defer server.Cleanup()
4343

4444
t.Run("connect", func(t *testing.T) {
@@ -53,7 +53,7 @@ func TestConnection(t *testing.T) {
5353
}
5454

5555
func TestConnectionClosureAbnormal(t *testing.T) {
56-
server := ws.NewWSServerWithClosureAbnormal(t)
56+
server := ws.NewServerWithClosureAbnormal(t)
5757
defer server.Cleanup()
5858

5959
t.Run("closure abnormal", func(t *testing.T) {
@@ -71,7 +71,7 @@ func TestConnectionClosureAbnormal(t *testing.T) {
7171
}
7272

7373
func TestConnectionSendRecv(t *testing.T) {
74-
server := ws.NewWSServerWithCDPHandler(t, ws.CDPDefaultHandler, nil)
74+
server := ws.NewServerWithCDPHandler(t, ws.CDPDefaultHandler, nil)
7575
defer server.Cleanup()
7676

7777
t.Run("send command with empty reply", func(t *testing.T) {
@@ -135,7 +135,7 @@ func TestConnectionCreateSession(t *testing.T) {
135135
}
136136
}
137137

138-
server := ws.NewWSServerWithCDPHandler(t, handler, &cmdsReceived)
138+
server := ws.NewServerWithCDPHandler(t, handler, &cmdsReceived)
139139
defer server.Cleanup()
140140

141141
t.Run("create session for target", func(t *testing.T) {

common/session_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,18 @@ func TestSessionCreateSession(t *testing.T) {
6161
case cdproto.MethodType(cdproto.CommandTargetAttachToTarget):
6262
writeCh <- cdproto.Message{
6363
Method: cdproto.EventTargetAttachedToTarget,
64-
Params: easyjson.RawMessage([]byte(ws.CDPTargetAttachedToTargetRequest)),
64+
Params: easyjson.RawMessage([]byte(ws.TargetAttachedToTargetEvent)),
6565
}
6666
writeCh <- cdproto.Message{
6767
ID: msg.ID,
6868
SessionID: msg.SessionID,
69-
Result: easyjson.RawMessage([]byte(ws.CDPTargetAttachedToTargetResponse)),
69+
Result: easyjson.RawMessage([]byte(ws.TargetAttachedToTargetResult)),
7070
}
7171
}
7272
}
7373
}
7474

75-
server := ws.NewWSServerWithCDPHandler(t, handler, &cmdsReceived)
75+
server := ws.NewServerWithCDPHandler(t, handler, &cmdsReceived)
7676
defer server.Cleanup()
7777

7878
t.Run("send and recv session commands", func(t *testing.T) {

tests/ws/server.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const (
5353
)
5454

5555
var (
56-
CDPTargetAttachedToTargetRequest = fmt.Sprintf(`
56+
TargetAttachedToTargetEvent = fmt.Sprintf(`
5757
{
5858
"sessionId": "%s",
5959
"targetInfo": {
@@ -68,19 +68,19 @@ var (
6868
}
6969
`, DummyCDPSessionID, DummyCDPTargetID, DummyCDPBrowserContextID)
7070

71-
CDPTargetAttachedToTargetResponse = fmt.Sprintf(`{"sessionId":"%s"}`, DummyCDPSessionID)
71+
TargetAttachedToTargetResult = fmt.Sprintf(`{"sessionId":"%s"}`, DummyCDPSessionID)
7272
)
7373

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(
7676
t testing.TB,
7777
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))
8080
}
8181

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 {
8484
Mux *http.ServeMux
8585
ServerHTTP *httptest.Server
8686
Dialer *k6netext.Dialer
@@ -89,18 +89,18 @@ type WSTestServer struct {
8989
Cleanup func()
9090
}
9191

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())
9595
}
9696

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())
100100
}
101101

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 {
104104
t.Helper()
105105

106106
// 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
135135
require.NoError(t, http2.ConfigureTransport(transport))
136136

137137
ctx, ctxCancel := context.WithCancel(context.Background())
138-
return &WSTestServer{
138+
return &Server{
139139
Mux: mux,
140140
ServerHTTP: httpSrv,
141141
Dialer: dialer,
@@ -148,6 +148,8 @@ func NewWSServer(t testing.TB, path string, handler http.Handler) *WSTestServer
148148
}
149149
}
150150

151+
// TODO: make a websocket.Conn wrapper for CDPxxx methods
152+
151153
// CDPDefaultCDPHandler is a default handler for the CDP WS server
152154
func CDPDefaultHandler(conn *websocket.Conn, msg *cdproto.Message, writeCh chan cdproto.Message, done chan struct{}) {
153155
if msg.SessionID != "" && msg.Method != "" {
@@ -163,12 +165,12 @@ func CDPDefaultHandler(conn *websocket.Conn, msg *cdproto.Message, writeCh chan
163165
case cdproto.MethodType(cdproto.CommandTargetAttachToTarget):
164166
writeCh <- cdproto.Message{
165167
Method: cdproto.EventTargetAttachedToTarget,
166-
Params: easyjson.RawMessage([]byte(CDPTargetAttachedToTargetRequest)),
168+
Params: easyjson.RawMessage([]byte(TargetAttachedToTargetEvent)),
167169
}
168170
writeCh <- cdproto.Message{
169171
ID: msg.ID,
170172
SessionID: msg.SessionID,
171-
Result: easyjson.RawMessage([]byte(CDPTargetAttachedToTargetResponse)),
173+
Result: easyjson.RawMessage([]byte(TargetAttachedToTargetResult)),
172174
}
173175
default:
174176
writeCh <- cdproto.Message{

0 commit comments

Comments
 (0)