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

Commit

Permalink
add cookie type
Browse files Browse the repository at this point in the history
  • Loading branch information
inancgumus committed Aug 21, 2023
1 parent 7ac87c5 commit 0f9f610
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 25 deletions.
3 changes: 1 addition & 2 deletions api/browser_context.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package api

import (
"github.com/chromedp/cdproto/network"
"github.com/dop251/goja"
)

Expand All @@ -13,7 +12,7 @@ type BrowserContext interface {
ClearCookies()
ClearPermissions()
Close()
Cookies() ([]*network.Cookie, error)
Cookies() ([]any, error)
ExposeBinding(name string, callback goja.Callable, opts goja.Value)
ExposeFunction(name string, callback goja.Callable)
GrantPermissions(permissions []string, opts goja.Value)
Expand Down
3 changes: 1 addition & 2 deletions browser/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"

"github.com/chromedp/cdproto/network"
"github.com/dop251/goja"

"github.com/grafana/xk6-browser/api"
Expand Down Expand Up @@ -620,7 +619,7 @@ func mapBrowserContext(vu moduleVU, bc api.BrowserContext) mapping {
"clearCookies": bc.ClearCookies,
"clearPermissions": bc.ClearPermissions,
"close": bc.Close,
"cookies": func() ([]*network.Cookie, error) {
"cookies": func() ([]any, error) {
cc, err := bc.Cookies()
ctx := vu.Context()
panicIfFatalError(ctx, err)
Expand Down
18 changes: 15 additions & 3 deletions common/browser_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (b *BrowserContext) Close() {
}

// Cookies returns all cookies.
func (b *BrowserContext) Cookies() ([]*network.Cookie, error) {
func (b *BrowserContext) Cookies() ([]any, error) {
b.logger.Debugf("BrowserContext:Cookies", "bctxid:%v", b.id)

getCookies := storage.
Expand All @@ -192,9 +192,21 @@ func (b *BrowserContext) Cookies() ([]*network.Cookie, error) {
return nil, fmt.Errorf("unable to get cookies: %w", err)
}

// TODO: copy networkCookies?
cookies := make([]any, len(networkCookies))
for i, c := range networkCookies {
cookies[i] = &Cookie{
Name: c.Name,
Value: c.Value,
Domain: c.Domain,
Path: c.Path,
Expires: int64(c.Expires),
HTTPOnly: c.HTTPOnly,
Secure: c.Secure,
SameSite: CookieSameSite(c.SameSite),
}
}

return networkCookies, nil
return cookies, nil
}

// ExposeBinding is not implemented.
Expand Down
34 changes: 34 additions & 0 deletions common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,3 +575,37 @@ func (c *Credentials) Parse(ctx context.Context, credentials goja.Value) error {
}
return nil
}

// Cookie represents a browser cookie.
//
// 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.
}

// CookieSameSite represents the cookie's 'SameSite' status.
//
// https://tools.ietf.org/html/draft-west-first-party-cookies.
type CookieSameSite string

const (
// CookieSameSiteStrict sets the cookie to be sent only in a first-party
// context and not be sent along with requests initiated by third party
// websites.
CookieSameSiteStrict CookieSameSite = "Strict"

// CookieSameSiteLax sets the cookie to be sent along with "same-site"
// requests, and with "cross-site" top-level navigations.
CookieSameSiteLax CookieSameSite = "Lax"

// CookieSameSiteNone sets the cookie to be sent in all contexts, i.e
// potentially insecure third-party requests.
CookieSameSiteNone CookieSameSite = "None"
)
32 changes: 14 additions & 18 deletions tests/browser_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"net/http"
"testing"

"github.com/chromedp/cdproto/network"
"github.com/dop251/goja"
"github.com/grafana/xk6-browser/common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -260,22 +260,15 @@ func TestBrowserContextCookies2(t *testing.T) {
cookies, err := p.Context().Cookies()
require.NoError(t, err)
require.Len(t, cookies, 1)

assert.Equal(t, &network.Cookie{
Name: "c",
Value: "v",
Domain: "127.0.0.1",
Expires: -1,
HTTPOnly: false,
Path: "/" + testBrowserStaticDir,
Priority: network.CookiePriorityMedium,
SameParty: false,
SameSite: "",
Secure: false,
Session: true,
Size: 2,
SourcePort: int64(tb.port()),
SourceScheme: network.CookieSourceSchemeNonSecure,
assert.Equal(t, &common.Cookie{
Name: "c",
Value: "v",
Domain: "127.0.0.1",
Expires: -1,
HTTPOnly: false,
Path: "/" + testBrowserStaticDir,
SameSite: "",
Secure: false,
}, cookies[0])
})
t.Run("http_only", func(t *testing.T) {
Expand All @@ -296,6 +289,9 @@ func TestBrowserContextCookies2(t *testing.T) {
cookies, err := p.Context().Cookies()
require.NoError(t, err)
require.Len(t, cookies, 1)
assert.True(t, cookies[0].HTTPOnly)

c, ok := cookies[0].(*common.Cookie)
require.True(t, ok)
assert.True(t, c.HTTPOnly)
})
}

0 comments on commit 0f9f610

Please sign in to comment.