Skip to content

Commit

Permalink
Refactor AddCookies to return error
Browse files Browse the repository at this point in the history
  • Loading branch information
inancgumus committed Aug 22, 2023
1 parent 7ae14a9 commit 0a83c65
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 27 deletions.
2 changes: 1 addition & 1 deletion 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)
AddCookies(cookies goja.Value) error
AddInitScript(script goja.Value, arg goja.Value) error
Browser() Browser
ClearCookies()
Expand Down
9 changes: 1 addition & 8 deletions common/browser_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,16 +439,9 @@ 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) {
func (b *BrowserContext) AddCookies(cookies goja.Value) error {
b.logger.Debugf("BrowserContext:AddCookies", "bctxid:%v", b.id)

err := b.addCookies(cookies)
if err != nil {
k6ext.Panic(b.ctx, "adding cookies: %w", err)
}
}

func (b *BrowserContext) addCookies(cookies goja.Value) error {
var cookieParams []network.CookieParam
if !gojaValueExists(cookies) {
return Error("cookies argument must be set")
Expand Down
35 changes: 17 additions & 18 deletions tests/browser_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,22 @@ func TestBrowserContextAddCookies(t *testing.T) {
errorTests := []struct {
description string
cookiesCmd string
shouldPanic bool
shouldFail bool
}{
{
description: "nil_cookies",
cookiesCmd: "",
shouldPanic: true,
shouldFail: true,
},
{
description: "goja_null_cookies",
cookiesCmd: "null;",
shouldPanic: true,
shouldFail: true,
},
{
description: "goja_undefined_cookies",
cookiesCmd: "undefined;",
shouldPanic: true,
shouldFail: true,
},
{
description: "goja_cookies_object",
Expand All @@ -81,12 +81,12 @@ func TestBrowserContextAddCookies(t *testing.T) {
url: "http://test.go",
});
`,
shouldPanic: true,
shouldFail: true,
},
{
description: "goja_cookies_string",
cookiesCmd: `"test_cookie_name=test_cookie_value"`,
shouldPanic: true,
shouldFail: true,
},
{
description: "cookie_missing_name",
Expand All @@ -96,7 +96,7 @@ func TestBrowserContextAddCookies(t *testing.T) {
url: "http://test.go",
}
];`,
shouldPanic: true,
shouldFail: true,
},
{
description: "cookie_missing_value",
Expand All @@ -106,7 +106,7 @@ func TestBrowserContextAddCookies(t *testing.T) {
url: "http://test.go",
}
];`,
shouldPanic: true,
shouldFail: true,
},
{
description: "cookie_missing_url",
Expand All @@ -116,7 +116,7 @@ func TestBrowserContextAddCookies(t *testing.T) {
value: "test_cookie_value",
}
];`,
shouldPanic: true,
shouldFail: true,
},
{
description: "cookies_missing_path",
Expand All @@ -127,7 +127,7 @@ func TestBrowserContextAddCookies(t *testing.T) {
domain: "http://test.go",
}
];`,
shouldPanic: true,
shouldFail: true,
},
{
description: "cookies_missing_domain",
Expand All @@ -138,7 +138,7 @@ func TestBrowserContextAddCookies(t *testing.T) {
path: "/to/page",
}
];`,
shouldPanic: true,
shouldFail: true,
},
{
description: "cookie_with_url",
Expand All @@ -149,7 +149,7 @@ func TestBrowserContextAddCookies(t *testing.T) {
url: "http://test.go",
}
];`,
shouldPanic: false,
shouldFail: false,
},
{
description: "cookie_with_domain_and_path",
Expand All @@ -161,10 +161,9 @@ func TestBrowserContextAddCookies(t *testing.T) {
path: "/to/page",
}
];`,
shouldPanic: false,
shouldFail: false,
},
}

for _, tt := range errorTests {
tt := tt
t.Run(tt.description, func(t *testing.T) {
Expand All @@ -182,11 +181,11 @@ func TestBrowserContextAddCookies(t *testing.T) {
bc, err := tb.NewContext(nil)
require.NoError(t, err)

if tt.shouldPanic {
assert.Panics(t, func() { bc.AddCookies(cookies) })
} else {
assert.NotPanics(t, func() { bc.AddCookies(cookies) })
if tt.shouldFail {
require.Error(t, bc.AddCookies(cookies))
return
}
require.NoError(t, bc.AddCookies(cookies))
})
}
}

0 comments on commit 0a83c65

Please sign in to comment.