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

Commit 85893f4

Browse files
authored
Merge pull request #166 from grafana/refactor/move-ws-test-server
Move websocket test server to its own package
2 parents e6db54c + bf0a22b commit 85893f4

File tree

3 files changed

+238
-219
lines changed

3 files changed

+238
-219
lines changed

common/connection_test.go

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ import (
3333
"github.com/mailru/easyjson"
3434
"github.com/stretchr/testify/assert"
3535
"github.com/stretchr/testify/require"
36+
37+
"github.com/grafana/xk6-browser/tests/ws"
3638
)
3739

3840
func TestConnection(t *testing.T) {
39-
server := NewWSServerWithEcho(t)
40-
defer server.Cleanup()
41+
server := ws.NewServer(t, ws.WithEchoHandler("/echo"))
4142

4243
t.Run("connect", func(t *testing.T) {
4344
ctx := context.Background()
@@ -51,8 +52,7 @@ func TestConnection(t *testing.T) {
5152
}
5253

5354
func TestConnectionClosureAbnormal(t *testing.T) {
54-
server := NewWSServerWithClosureAbnormal(t)
55-
defer server.Cleanup()
55+
server := ws.NewServer(t, ws.WithClosureAbnormalHandler("/closure-abnormal"))
5656

5757
t.Run("closure abnormal", func(t *testing.T) {
5858
ctx := context.Background()
@@ -69,8 +69,7 @@ func TestConnectionClosureAbnormal(t *testing.T) {
6969
}
7070

7171
func TestConnectionSendRecv(t *testing.T) {
72-
server := NewWSServerWithCDPHandler(t, CDPDefaultHandler, nil)
73-
defer server.Cleanup()
72+
server := ws.NewServer(t, ws.WithCDPHandler("/cdp", ws.CDPDefaultHandler, nil))
7473

7574
t.Run("send command with empty reply", func(t *testing.T) {
7675
ctx := context.Background()
@@ -109,19 +108,19 @@ func TestConnectionCreateSession(t *testing.T) {
109108
writeCh <- cdproto.Message{
110109
Method: cdproto.EventTargetAttachedToTarget,
111110
Params: easyjson.RawMessage([]byte(`
112-
{
113-
"sessionId": "0123456789",
114-
"targetInfo": {
115-
"targetId": "abcdef0123456789",
116-
"type": "page",
117-
"title": "",
118-
"url": "about:blank",
119-
"attached": true,
120-
"browserContextId": "0123456789876543210"
121-
},
122-
"waitingForDebugger": false
123-
}
124-
`)),
111+
{
112+
"sessionId": "0123456789",
113+
"targetInfo": {
114+
"targetId": "abcdef0123456789",
115+
"type": "page",
116+
"title": "",
117+
"url": "about:blank",
118+
"attached": true,
119+
"browserContextId": "0123456789876543210"
120+
},
121+
"waitingForDebugger": false
122+
}
123+
`)),
125124
}
126125
writeCh <- cdproto.Message{
127126
ID: msg.ID,
@@ -133,8 +132,7 @@ func TestConnectionCreateSession(t *testing.T) {
133132
}
134133
}
135134

136-
server := NewWSServerWithCDPHandler(t, handler, &cmdsReceived)
137-
defer server.Cleanup()
135+
server := ws.NewServer(t, ws.WithCDPHandler("/cdp", handler, &cmdsReceived))
138136

139137
t.Run("create session for target", func(t *testing.T) {
140138
ctx := context.Background()

common/session_test.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,36 @@ import (
3434
"github.com/mailru/easyjson"
3535
"github.com/stretchr/testify/assert"
3636
"github.com/stretchr/testify/require"
37+
38+
"github.com/grafana/xk6-browser/tests/ws"
3739
)
3840

3941
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+
4067
cmdsReceived := make([]cdproto.MethodType, 0)
4168
handler := func(conn *websocket.Conn, msg *cdproto.Message, writeCh chan cdproto.Message, done chan struct{}) {
4269
if msg.SessionID != "" && msg.Method != "" {
@@ -59,19 +86,18 @@ func TestSessionCreateSession(t *testing.T) {
5986
case cdproto.MethodType(cdproto.CommandTargetAttachToTarget):
6087
writeCh <- cdproto.Message{
6188
Method: cdproto.EventTargetAttachedToTarget,
62-
Params: easyjson.RawMessage([]byte(DEFAULT_CDP_TARGET_ATTACHED_TO_TARGET_MSG)),
89+
Params: easyjson.RawMessage([]byte(targetAttachedToTargetEvent)),
6390
}
6491
writeCh <- cdproto.Message{
6592
ID: msg.ID,
6693
SessionID: msg.SessionID,
67-
Result: easyjson.RawMessage([]byte(DEFAULT_CDP_TARGET_ATTACH_TO_TARGET_RESPONSE)),
94+
Result: easyjson.RawMessage([]byte(targetAttachedToTargetResult)),
6895
}
6996
}
7097
}
7198
}
7299

73-
server := NewWSServerWithCDPHandler(t, handler, &cmdsReceived)
74-
defer server.Cleanup()
100+
server := ws.NewServer(t, ws.WithCDPHandler("/cdp", handler, &cmdsReceived))
75101

76102
t.Run("send and recv session commands", func(t *testing.T) {
77103
ctx := context.Background()
@@ -81,9 +107,9 @@ func TestSessionCreateSession(t *testing.T) {
81107

82108
if assert.NoError(t, err) {
83109
session, err := conn.createSession(&target.Info{
84-
TargetID: DEFAULT_CDP_TARGET_ID,
85110
Type: "page",
86-
BrowserContextID: DEFAULT_CDP_BROWSER_CONTEXT_ID,
111+
TargetID: cdpTargetID,
112+
BrowserContextID: cdpBrowserContextID,
87113
})
88114

89115
if assert.NoError(t, err) {

0 commit comments

Comments
 (0)