Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cdp_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions cmd/cdpgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,16 +329,23 @@ func (g *Generator) CdpClient(domains []proto.Domain) {
// invoke methods or listen to events in every CDP domain. The Client consumes
// a rpcc connection, used to invoke the methods.
type Client struct {
conn *rpcc.Conn
%s
}

// NewClient returns a new Client that uses conn
// for communication with the debugging target.
func NewClient(conn *rpcc.Conn) *Client {
return &Client{
conn: conn,
%s
}
}

// Conn returns the underlying rpcc.Conn used by this Client.
func (c *Client) Conn() *rpcc.Conn {
return c.conn
}
`, fields.buf.Bytes(), newFields.buf.Bytes())
}

Expand Down
45 changes: 44 additions & 1 deletion internal/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package testutil

import (
"context"
"encoding/json"
"io"
"testing"
"time"

Expand All @@ -11,6 +13,41 @@ import (
"github.com/mafredri/cdp/rpcc"
)

// logCodec wraps an rpcc.Codec and logs all requests and responses.
type logCodec struct {
t *testing.T
enc *json.Encoder
dec *json.Decoder
}

func (c *logCodec) WriteRequest(r *rpcc.Request) error {
c.t.Helper()
data, _ := json.Marshal(r)
c.t.Logf("SEND: %s", data)
return c.enc.Encode(r)
}

func (c *logCodec) ReadResponse(r *rpcc.Response) error {
c.t.Helper()
if err := c.dec.Decode(r); err != nil {
return err
}
data, _ := json.Marshal(r)
c.t.Logf("RECV: %s", data)
return nil
}

// NewLogCodec returns a new rpcc codec that logs all requests and responses.
func NewLogCodec(t *testing.T) rpcc.DialOption {
return rpcc.WithCodec(func(conn io.ReadWriter) rpcc.Codec {
return &logCodec{
t: t,
enc: json.NewEncoder(conn),
dec: json.NewDecoder(conn),
}
})
}

// Client represents a test client.
type Client struct {
t *testing.T
Expand All @@ -32,7 +69,13 @@ func NewClient(ctx context.Context, t *testing.T) *Client {
if err != nil {
t.Fatal(err)
}
conn, err := rpcc.DialContext(ctx, v.WebSocketDebuggerURL)

var opts []rpcc.DialOption
if testing.Verbose() {
opts = append(opts, NewLogCodec(t))
}

conn, err := rpcc.DialContext(ctx, v.WebSocketDebuggerURL, opts...)
if err != nil {
t.Fatal(err)
}
Expand Down
Loading