Skip to content

Commit

Permalink
Remove globals from ws test server
Browse files Browse the repository at this point in the history
  • Loading branch information
inancgumus committed Dec 23, 2021
1 parent af825f4 commit 1908389
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 34 deletions.
33 changes: 29 additions & 4 deletions common/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,31 @@ import (
)

func TestSessionCreateSession(t *testing.T) {
const (
cdpTargetID = "target_id_0123456789"
cdpBrowserContextID = "browser_context_id_0123456789"

targetAttachedToTargetEvent = `
{
"sessionId": "session_id_0123456789",
"targetInfo": {
"targetId": "target_id_0123456789",
"type": "page",
"title": "",
"url": "about:blank",
"attached": true,
"browserContextId": "browser_context_id_0123456789"
},
"waitingForDebugger": false
}`

targetAttachedToTargetResult = `
{
"sessionId":"session_id_0123456789"
}
`
)

cmdsReceived := make([]cdproto.MethodType, 0)
handler := func(conn *websocket.Conn, msg *cdproto.Message, writeCh chan cdproto.Message, done chan struct{}) {
if msg.SessionID != "" && msg.Method != "" {
Expand All @@ -61,12 +86,12 @@ func TestSessionCreateSession(t *testing.T) {
case cdproto.MethodType(cdproto.CommandTargetAttachToTarget):
writeCh <- cdproto.Message{
Method: cdproto.EventTargetAttachedToTarget,
Params: easyjson.RawMessage([]byte(ws.TargetAttachedToTargetEvent)),
Params: easyjson.RawMessage([]byte(targetAttachedToTargetEvent)),
}
writeCh <- cdproto.Message{
ID: msg.ID,
SessionID: msg.SessionID,
Result: easyjson.RawMessage([]byte(ws.TargetAttachedToTargetResult)),
Result: easyjson.RawMessage([]byte(targetAttachedToTargetResult)),
}
}
}
Expand All @@ -82,9 +107,9 @@ func TestSessionCreateSession(t *testing.T) {

if assert.NoError(t, err) {
session, err := conn.createSession(&target.Info{
TargetID: ws.DummyCDPTargetID,
Type: "page",
BrowserContextID: ws.DummyCDPBrowserContextID,
TargetID: cdpTargetID,
BrowserContextID: cdpBrowserContextID,
})

if assert.NoError(t, err) {
Expand Down
56 changes: 26 additions & 30 deletions tests/ws/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package ws

import (
"context"
"fmt"
"io"
"net"
"net/http"
Expand All @@ -45,32 +44,6 @@ import (
k6types "go.k6.io/k6/lib/types"
)

const (
DummyCDPSessionID = "session_id_0123456789"
DummyCDPTargetID = "target_id_0123456789"
DummyCDPBrowserContextID = "browser_context_id_0123456789"
WebSocketServerURL = "wsbin.local"
)

var (
TargetAttachedToTargetEvent = fmt.Sprintf(`
{
"sessionId": "%s",
"targetInfo": {
"targetId": "%s",
"type": "page",
"title": "",
"url": "about:blank",
"attached": true,
"browserContextId": "%s"
},
"waitingForDebugger": false
}
`, DummyCDPSessionID, DummyCDPTargetID, DummyCDPBrowserContextID)

TargetAttachedToTargetResult = fmt.Sprintf(`{"sessionId":"%s"}`, DummyCDPSessionID)
)

// Server can be used as a test alternative to a real CDP compatible browser.
type Server struct {
t testing.TB
Expand Down Expand Up @@ -104,8 +77,10 @@ func NewServer(t testing.TB, opts ...func(*Server)) *Server {
KeepAlive: 10 * time.Second,
DualStack: true,
}, k6netext.NewResolver(net.LookupIP, 0, k6types.DNSfirst, k6types.DNSpreferIPv4))

const wsURL = "wsbin.local"
dialer.Hosts = map[string]*k6lib.HostAddress{
WebSocketServerURL: domain,
wsURL: domain,
}

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

// CDPDefaultHandler is a default handler for the CDP WS server.
func CDPDefaultHandler(conn *websocket.Conn, msg *cdproto.Message, writeCh chan cdproto.Message, done chan struct{}) {
const (
targetAttachedToTargetEvent = `
{
"sessionId": "session_id_0123456789",
"targetInfo": {
"targetId": "target_id_0123456789",
"type": "page",
"title": "",
"url": "about:blank",
"attached": true,
"browserContextId": "browser_context_id_0123456789"
},
"waitingForDebugger": false
}`

targetAttachedToTargetResult = `
{
"sessionId":"session_id_0123456789"
}`
)

if msg.SessionID != "" && msg.Method != "" {
switch msg.Method {
default:
Expand All @@ -292,12 +288,12 @@ func CDPDefaultHandler(conn *websocket.Conn, msg *cdproto.Message, writeCh chan
case cdproto.MethodType(cdproto.CommandTargetAttachToTarget):
writeCh <- cdproto.Message{
Method: cdproto.EventTargetAttachedToTarget,
Params: easyjson.RawMessage([]byte(TargetAttachedToTargetEvent)),
Params: easyjson.RawMessage([]byte(targetAttachedToTargetEvent)),
}
writeCh <- cdproto.Message{
ID: msg.ID,
SessionID: msg.SessionID,
Result: easyjson.RawMessage([]byte(TargetAttachedToTargetResult)),
Result: easyjson.RawMessage([]byte(targetAttachedToTargetResult)),
}
default:
writeCh <- cdproto.Message{
Expand Down

0 comments on commit 1908389

Please sign in to comment.