Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a consistent cookie API #1030

Merged
merged 9 commits into from
Sep 6, 2023
Merged
19 changes: 10 additions & 9 deletions api/browser_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

// BrowserContext is the public interface of a CDP browser context.
type BrowserContext interface {
AddCookies(cookies goja.Value) error
AddCookies(cookies []*Cookie) error
AddInitScript(script goja.Value, arg goja.Value) error
Browser() Browser
ClearCookies() error
Expand Down Expand Up @@ -41,14 +41,15 @@ type BrowserContext interface {
//
// https://datatracker.ietf.org/doc/html/rfc6265.
type Cookie struct {
Name string `json:"name"` // Cookie name.
Value string `json:"value"` // Cookie value.
Domain string `json:"domain"` // Cookie domain.
Path string `json:"path"` // Cookie path.
Expires int64 `json:"expires"` // Cookie expiration date as the number of seconds since the UNIX epoch.
HTTPOnly bool `json:"httpOnly"` // True if cookie is http-only.
Secure bool `json:"secure"` // True if cookie is secure.
SameSite CookieSameSite `json:"sameSite"` // Cookie SameSite type.
Name string `json:"name"` // Cookie name.
Value string `json:"value"` // Cookie value.
Domain string `json:"domain"` // Cookie domain.
Path string `json:"path"` // Cookie path.
Expires int64 `json:"expires"` // Cookie expiration date as the number of seconds since the UNIX epoch.
HTTPOnly bool `json:"httpOnly"` // True if cookie is http-only.
Secure bool `json:"secure"` // True if cookie is secure.
SameSite CookieSameSite `json:"sameSite"` // Cookie SameSite type.
URL string `json:"url,omitempty"` // Cookie URL.
}

// CookieSameSite represents the cookie's 'SameSite' status.
Expand Down
31 changes: 18 additions & 13 deletions common/browser_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,21 +443,16 @@ func (b *BrowserContext) getSession(id target.SessionID) *Session {

// AddCookies adds cookies into this browser context.
// All pages within this context will have these cookies installed.
func (b *BrowserContext) AddCookies(cookies goja.Value) error {
func (b *BrowserContext) AddCookies(cookies []*api.Cookie) error {
ankur22 marked this conversation as resolved.
Show resolved Hide resolved
b.logger.Debugf("BrowserContext:AddCookies", "bctxid:%v", b.id)

var cookieParams []network.CookieParam
if !gojaValueExists(cookies) {
return Error("cookies argument must be set")
// skip work if no cookies provided.
if len(cookies) == 0 {
return fmt.Errorf("no cookies provided")
}
if err := b.vu.Runtime().ExportTo(cookies, &cookieParams); err != nil {
return fmt.Errorf("cannot recognize cookie values: %w", err)
}

coookiesToSet := make([]*network.CookieParam, len(cookieParams))
for i, cookie := range cookieParams {
c := cookie

cookiesToSet := make([]*network.CookieParam, 0, len(cookies))
for _, c := range cookies {
if c.Name == "" {
return fmt.Errorf("cookie name must be set: %#v", c)
}
Expand All @@ -469,11 +464,21 @@ func (b *BrowserContext) AddCookies(cookies goja.Value) error {
const msg = "if cookie URL is not provided, both domain and path must be specified: %#v"
return fmt.Errorf(msg, c)
}
coookiesToSet[i] = &c
cookiesToSet = append(cookiesToSet, &network.CookieParam{
Name: c.Name,
Value: c.Value,
Domain: c.Domain,
Path: c.Path,
URL: c.URL,
Expires: nil, // TODO: fix this
HTTPOnly: c.HTTPOnly,
Secure: c.Secure,
SameSite: network.CookieSameSite(c.SameSite),
})
}

setCookies := storage.
SetCookies(coookiesToSet).
SetCookies(cookiesToSet).
WithBrowserContextID(b.id)
if err := setCookies.Do(cdp.WithExecutor(b.ctx, b.browser.conn)); err != nil {
return fmt.Errorf("cannot set cookies: %w", err)
Expand Down