Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.
Merged
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
6 changes: 3 additions & 3 deletions browser/browser_context_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolin
}), nil
},
"setHTTPCredentials": func(httpCredentials sobek.Value) (*sobek.Promise, error) {
pc := common.NewCredentials()
if err := pc.Parse(vu.Context(), httpCredentials); err != nil {
creds, err := exportTo[common.Credentials](rt, httpCredentials)
if err != nil {
return nil, fmt.Errorf("parsing HTTP credentials: %w", err)
}
return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, bc.SetHTTPCredentials(pc) //nolint:staticcheck
return nil, bc.SetHTTPCredentials(creds) //nolint:staticcheck
}), nil
},
"setOffline": func(offline bool) *sobek.Promise {
Expand Down
6 changes: 3 additions & 3 deletions browser/browser_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@ func parseBrowserContextOptions(ctx context.Context, opts sobek.Value) (*common.
case "hasTouch":
b.HasTouch = o.Get(k).ToBoolean()
case "httpCredentials":
credentials := common.NewCredentials()
if err := credentials.Parse(ctx, o.Get(k).ToObject(rt)); err != nil {
var err error
b.HTTPCredentials, err = exportTo[common.Credentials](rt, o.Get(k))
if err != nil {
return nil, fmt.Errorf("parsing HTTP credential options: %w", err)
}
b.HttpCredentials = credentials
case "ignoreHTTPSErrors":
b.IgnoreHTTPSErrors = o.Get(k).ToBoolean()
case "isMobile":
Expand Down
2 changes: 1 addition & 1 deletion browser/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ type browserContextAPI interface {
SetDefaultNavigationTimeout(timeout int64)
SetDefaultTimeout(timeout int64)
SetGeolocation(geolocation *common.Geolocation) error
SetHTTPCredentials(httpCredentials *common.Credentials) error
SetHTTPCredentials(httpCredentials common.Credentials) error
SetOffline(offline bool) error
WaitForEvent(event string, optsOrPredicate sobek.Value) (any, error)
}
Expand Down
4 changes: 2 additions & 2 deletions common/browser_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,12 @@ func (b *BrowserContext) SetGeolocation(g *Geolocation) error {
// See for details:
// - https://github.com/microsoft/playwright/issues/2196#issuecomment-627134837
// - https://github.com/microsoft/playwright/pull/2763
func (b *BrowserContext) SetHTTPCredentials(hc *Credentials) error {
func (b *BrowserContext) SetHTTPCredentials(hc Credentials) error {
b.logger.Warnf("setHTTPCredentials", "setHTTPCredentials is deprecated."+
" Create a new BrowserContext with httpCredentials instead.")
b.logger.Debugf("BrowserContext:SetHTTPCredentials", "bctxid:%v", b.id)

b.opts.HttpCredentials = hc
b.opts.HTTPCredentials = hc
for _, p := range b.browser.getPages() {
if err := p.updateHTTPCredentials(); err != nil {
return fmt.Errorf("setting HTTP credentials in target ID %s: %w", p.targetID, err)
Expand Down
2 changes: 1 addition & 1 deletion common/browser_context_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type BrowserContextOptions struct {
ExtraHTTPHeaders map[string]string `js:"extraHTTPHeaders"`
Geolocation *Geolocation `js:"geolocation"`
HasTouch bool `js:"hasTouch"`
HttpCredentials *Credentials `js:"httpCredentials"`
HTTPCredentials Credentials `js:"httpCredentials"`
IgnoreHTTPSErrors bool `js:"ignoreHTTPSErrors"`
IsMobile bool `js:"isMobile"`
JavaScriptEnabled bool `js:"javaScriptEnabled"`
Expand Down
4 changes: 2 additions & 2 deletions common/frame_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1148,8 +1148,8 @@ func (fs *FrameSession) updateGeolocation(initial bool) error {
func (fs *FrameSession) updateHTTPCredentials(initial bool) error {
fs.logger.Debugf("NewFrameSession:updateHttpCredentials", "sid:%v tid:%v", fs.session.ID(), fs.targetID)

credentials := fs.page.browserCtx.opts.HttpCredentials
if !initial || credentials != nil {
credentials := fs.page.browserCtx.opts.HTTPCredentials
if !initial || !credentials.IsEmpty() {
return fs.networkManager.Authenticate(credentials)
}

Expand Down
35 changes: 11 additions & 24 deletions common/network_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net"
"net/url"
"strconv"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -34,27 +35,13 @@ type Credentials struct {
Password string `js:"password"`
}

// NewCredentials return a new Credentials.
func NewCredentials() *Credentials {
return &Credentials{}
}

// Parse credentials details from a given sobek credentials value.
func (c *Credentials) Parse(ctx context.Context, credentials sobek.Value) error {
if !sobekValueExists(credentials) {
return errors.New("credentials are required")
}
o := credentials.ToObject(k6ext.Runtime(ctx))
for _, k := range o.Keys() {
switch k {
case "username":
c.Username = o.Get(k).String()
case "password":
c.Password = o.Get(k).String()
}
// IsEmpty returns true if the credentials are empty.
func (c Credentials) IsEmpty() bool {
c = Credentials{
Username: strings.TrimSpace(c.Username),
Password: strings.TrimSpace(c.Password),
}

return nil
return c == (Credentials{})
}

type metricInterceptor interface {
Expand All @@ -70,7 +57,7 @@ type NetworkManager struct {
session session
parent *NetworkManager
frameManager *FrameManager
credentials *Credentials
credentials Credentials
resolver k6netext.Resolver
vu k6modules.VU
customMetrics *k6ext.CustomMetrics
Expand Down Expand Up @@ -627,7 +614,7 @@ func (m *NetworkManager) onAuthRequired(event *fetch.EventAuthRequired) {
case m.attemptedAuth[rid]:
delete(m.attemptedAuth, rid)
res = fetch.AuthChallengeResponseResponseCancelAuth
case m.credentials != nil:
case !m.credentials.IsEmpty():
// TODO: remove requests from attemptedAuth when:
// - request is redirected
// - loading finished
Expand Down Expand Up @@ -732,9 +719,9 @@ func (m *NetworkManager) updateProtocolRequestInterception() error {
}

// Authenticate sets HTTP authentication credentials to use.
func (m *NetworkManager) Authenticate(credentials *Credentials) error {
func (m *NetworkManager) Authenticate(credentials Credentials) error {
m.credentials = credentials
if credentials != nil {
if !credentials.IsEmpty() {
m.userReqInterceptionEnabled = true
}
if err := m.updateProtocolRequestInterception(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion tests/browser_context_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestBrowserContextOptionsDefaultValues(t *testing.T) {
assert.Empty(t, opts.ExtraHTTPHeaders)
assert.Nil(t, opts.Geolocation)
assert.False(t, opts.HasTouch)
assert.Nil(t, opts.HttpCredentials)
assert.True(t, opts.HTTPCredentials.IsEmpty())
assert.False(t, opts.IgnoreHTTPSErrors)
assert.False(t, opts.IsMobile)
assert.True(t, opts.JavaScriptEnabled)
Expand Down
2 changes: 1 addition & 1 deletion tests/network_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func TestBasicAuth(t *testing.T) {
browser := newTestBrowser(t, withHTTPServer())

bcopts := common.NewBrowserContextOptions()
bcopts.HttpCredentials = &common.Credentials{
bcopts.HTTPCredentials = common.Credentials{
Username: validUser,
Password: validPassword,
}
Expand Down
Loading