fix: map Secure field in cookie conversions and add per-cookie error handling
All checks were successful
CI / vet (pull_request) Successful in 1m7s
CI / build (pull_request) Successful in 1m7s
CI / test (pull_request) Successful in 1m10s

The Secure field was dropped in both Playwright<->internal cookie
conversion functions, causing cookies with __Secure-/__Host- prefixes
to be rejected by Chromium. Additionally, batch AddCookies meant one
invalid cookie would fail browser creation entirely.

Changes:
- Map Secure field in cookieToPlaywrightOptionalCookie and
  playwrightCookieToCookie
- Add cookies one-by-one with slog.Warn on failure instead of
  failing the entire batch
- Add unit tests for both conversion functions

Closes #75

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-24 02:13:19 +00:00
parent 01aea52533
commit debf0ee2ed
3 changed files with 145 additions and 6 deletions

View File

@@ -152,12 +152,11 @@ func initBrowser(opt BrowserOptions) (*browserInitResult, error) {
if err != nil {
return nil, fmt.Errorf("error getting cookies from cookie jar: %w", err)
}
pwCookies := make([]playwright.OptionalCookie, len(cookies))
for i, c := range cookies {
pwCookies[i] = cookieToPlaywrightOptionalCookie(c)
}
if err := bctx.AddCookies(pwCookies); err != nil {
return nil, fmt.Errorf("error adding cookies to browser: %w", err)
for _, c := range cookies {
oc := cookieToPlaywrightOptionalCookie(c)
if err := bctx.AddCookies([]playwright.OptionalCookie{oc}); err != nil {
slog.Warn("skipping invalid cookie", "name", c.Name, "host", c.Host, "error", err)
}
}
}