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

Commit 1908389

Browse files
committed
Remove globals from ws test server
Resolves #166 (comment)
1 parent af825f4 commit 1908389

File tree

2 files changed

+55
-34
lines changed

2 files changed

+55
-34
lines changed

common/session_test.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,31 @@ import (
3939
)
4040

4141
func TestSessionCreateSession(t *testing.T) {
42+
const (
43+
cdpTargetID = "target_id_0123456789"
44+
cdpBrowserContextID = "browser_context_id_0123456789"
45+
46+
targetAttachedToTargetEvent = `
47+
{
48+
"sessionId": "session_id_0123456789",
49+
"targetInfo": {
50+
"targetId": "target_id_0123456789",
51+
"type": "page",
52+
"title": "",
53+
"url": "about:blank",
54+
"attached": true,
55+
"browserContextId": "browser_context_id_0123456789"
56+
},
57+
"waitingForDebugger": false
58+
}`
59+
60+
targetAttachedToTargetResult = `
61+
{
62+
"sessionId":"session_id_0123456789"
63+
}
64+
`
65+
)
66+
4267
cmdsReceived := make([]cdproto.MethodType, 0)
4368
handler := func(conn *websocket.Conn, msg *cdproto.Message, writeCh chan cdproto.Message, done chan struct{}) {
4469
if msg.SessionID != "" && msg.Method != "" {
@@ -61,12 +86,12 @@ func TestSessionCreateSession(t *testing.T) {
6186
case cdproto.MethodType(cdproto.CommandTargetAttachToTarget):
6287
writeCh <- cdproto.Message{
6388
Method: cdproto.EventTargetAttachedToTarget,
64-
Params: easyjson.RawMessage([]byte(ws.TargetAttachedToTargetEvent)),
89+
Params: easyjson.RawMessage([]byte(targetAttachedToTargetEvent)),
6590
}
6691
writeCh <- cdproto.Message{
6792
ID: msg.ID,
6893
SessionID: msg.SessionID,
69-
Result: easyjson.RawMessage([]byte(ws.TargetAttachedToTargetResult)),
94+
Result: easyjson.RawMessage([]byte(targetAttachedToTargetResult)),
7095
}
7196
}
7297
}
@@ -82,9 +107,9 @@ func TestSessionCreateSession(t *testing.T) {
82107

83108
if assert.NoError(t, err) {
84109
session, err := conn.createSession(&target.Info{
85-
TargetID: ws.DummyCDPTargetID,
86110
Type: "page",
87-
BrowserContextID: ws.DummyCDPBrowserContextID,
111+
TargetID: cdpTargetID,
112+
BrowserContextID: cdpBrowserContextID,
88113
})
89114

90115
if assert.NoError(t, err) {

tests/ws/server.go

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ package ws
2222

2323
import (
2424
"context"
25-
"fmt"
2625
"io"
2726
"net"
2827
"net/http"
@@ -45,32 +44,6 @@ import (
4544
k6types "go.k6.io/k6/lib/types"
4645
)
4746

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-
7447
// Server can be used as a test alternative to a real CDP compatible browser.
7548
type Server struct {
7649
t testing.TB
@@ -104,8 +77,10 @@ func NewServer(t testing.TB, opts ...func(*Server)) *Server {
10477
KeepAlive: 10 * time.Second,
10578
DualStack: true,
10679
}, k6netext.NewResolver(net.LookupIP, 0, k6types.DNSfirst, k6types.DNSpreferIPv4))
80+
81+
const wsURL = "wsbin.local"
10782
dialer.Hosts = map[string]*k6lib.HostAddress{
108-
WebSocketServerURL: domain,
83+
wsURL: domain,
10984
}
11085

11186
// Pre-configure the HTTP client transport with the dialer and TLS config (incl. HTTP2 support)
@@ -279,6 +254,27 @@ func WithCDPHandler(
279254

280255
// CDPDefaultHandler is a default handler for the CDP WS server.
281256
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+
282278
if msg.SessionID != "" && msg.Method != "" {
283279
switch msg.Method {
284280
default:
@@ -292,12 +288,12 @@ func CDPDefaultHandler(conn *websocket.Conn, msg *cdproto.Message, writeCh chan
292288
case cdproto.MethodType(cdproto.CommandTargetAttachToTarget):
293289
writeCh <- cdproto.Message{
294290
Method: cdproto.EventTargetAttachedToTarget,
295-
Params: easyjson.RawMessage([]byte(TargetAttachedToTargetEvent)),
291+
Params: easyjson.RawMessage([]byte(targetAttachedToTargetEvent)),
296292
}
297293
writeCh <- cdproto.Message{
298294
ID: msg.ID,
299295
SessionID: msg.SessionID,
300-
Result: easyjson.RawMessage([]byte(TargetAttachedToTargetResult)),
296+
Result: easyjson.RawMessage([]byte(targetAttachedToTargetResult)),
301297
}
302298
default:
303299
writeCh <- cdproto.Message{

0 commit comments

Comments
 (0)