Files
go-extractor/cookiejar_test.go
Steve Dudenhoeffer 963696cd62
All checks were successful
CI / vet (pull_request) Successful in 40s
CI / build (pull_request) Successful in 1m22s
CI / test (pull_request) Successful in 1m28s
enhance: thread-safe CookieJar, SameSite cookie attr, dynamic Google countries
- Wrap staticCookieJar in struct with sync.RWMutex for thread safety
- Add SameSite field to Cookie struct with Strict/Lax/None constants
- Update Playwright cookie conversion functions for SameSite
- Replace hardcoded 4-country switch with dynamic country code generation

Closes #20, #22, #23
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-15 16:34:54 +00:00

267 lines
7.1 KiB
Go

package extractor
import (
"testing"
)
func TestCookie_IsTargetMatch_ExactHost(t *testing.T) {
c := Cookie{Host: "example.com", Path: "/"}
match, err := c.IsTargetMatch("https://example.com/page")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !match {
t.Error("expected match for exact host")
}
}
func TestCookie_IsTargetMatch_DotPrefix(t *testing.T) {
c := Cookie{Host: ".example.com", Path: "/"}
match, err := c.IsTargetMatch("https://sub.example.com/page")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !match {
t.Error("expected match for .example.com against sub.example.com")
}
}
func TestCookie_IsTargetMatch_DotPrefix_NoFalsePositive(t *testing.T) {
c := Cookie{Host: ".example.com", Path: "/"}
match, err := c.IsTargetMatch("https://notexample.com/page")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if match {
t.Error("did not expect .example.com to match notexample.com")
}
}
func TestCookie_IsTargetMatch_PathExact(t *testing.T) {
c := Cookie{Host: "example.com", Path: "/foo"}
match, err := c.IsTargetMatch("https://example.com/foo")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !match {
t.Error("expected match for exact path /foo")
}
}
func TestCookie_IsTargetMatch_PathPrefix(t *testing.T) {
c := Cookie{Host: "example.com", Path: "/foo"}
match, err := c.IsTargetMatch("https://example.com/foo/bar")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !match {
t.Error("expected match for /foo prefix with /foo/bar")
}
}
func TestCookie_IsTargetMatch_PathBoundary(t *testing.T) {
c := Cookie{Host: "example.com", Path: "/foo"}
match, err := c.IsTargetMatch("https://example.com/foosball")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if match {
t.Error("did not expect /foo to match /foosball")
}
}
func TestCookie_IsTargetMatch_EmptyPath(t *testing.T) {
c := Cookie{Host: "example.com", Path: ""}
match, err := c.IsTargetMatch("https://example.com/anything")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !match {
t.Error("expected empty path cookie to match any path")
}
}
func TestCookie_IsTargetMatch_NoMatch(t *testing.T) {
c := Cookie{Host: "other.com", Path: "/"}
match, err := c.IsTargetMatch("https://example.com/page")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if match {
t.Error("did not expect other.com to match example.com")
}
}
func TestCookie_IsTargetMatch_InvalidURL(t *testing.T) {
c := Cookie{Host: "example.com", Path: "/"}
_, err := c.IsTargetMatch("://invalid")
if err == nil {
t.Error("expected error for invalid URL")
}
}
func TestStaticCookieJar_GetAll(t *testing.T) {
jar := &staticCookieJar{cookies: []Cookie{
{Host: "a.com", Name: "a", Value: "1"},
{Host: "b.com", Name: "b", Value: "2"},
}}
cookies, err := jar.GetAll()
if err != nil {
t.Fatalf("GetAll() error: %v", err)
}
if len(cookies) != 2 {
t.Errorf("GetAll() returned %d cookies, want 2", len(cookies))
}
}
func TestStaticCookieJar_Get(t *testing.T) {
jar := &staticCookieJar{cookies: []Cookie{
{Host: "example.com", Path: "/", Name: "a", Value: "1"},
{Host: "other.com", Path: "/", Name: "b", Value: "2"},
}}
cookies, err := jar.Get("https://example.com/page")
if err != nil {
t.Fatalf("Get() error: %v", err)
}
if len(cookies) != 1 {
t.Fatalf("Get() returned %d cookies, want 1", len(cookies))
}
if cookies[0].Name != "a" {
t.Errorf("Get() cookie name = %q, want %q", cookies[0].Name, "a")
}
}
func TestStaticCookieJar_Set_New(t *testing.T) {
jar := &staticCookieJar{}
err := jar.Set(Cookie{Host: "example.com", Path: "/", Name: "a", Value: "1"})
if err != nil {
t.Fatalf("Set() error: %v", err)
}
cookies, _ := jar.GetAll()
if len(cookies) != 1 {
t.Fatalf("after Set, GetAll() returned %d cookies, want 1", len(cookies))
}
if cookies[0].Value != "1" {
t.Errorf("cookie value = %q, want %q", cookies[0].Value, "1")
}
}
func TestStaticCookieJar_Set_Update(t *testing.T) {
jar := &staticCookieJar{cookies: []Cookie{
{Host: "example.com", Path: "/", Name: "a", Value: "1"},
}}
err := jar.Set(Cookie{Host: "example.com", Path: "/", Name: "a", Value: "2"})
if err != nil {
t.Fatalf("Set() error: %v", err)
}
cookies, _ := jar.GetAll()
if len(cookies) != 1 {
t.Fatalf("after update Set, GetAll() returned %d cookies, want 1", len(cookies))
}
if cookies[0].Value != "2" {
t.Errorf("cookie value = %q, want %q", cookies[0].Value, "2")
}
}
func TestStaticCookieJar_Delete(t *testing.T) {
jar := &staticCookieJar{cookies: []Cookie{
{Host: "example.com", Path: "/", Name: "a", Value: "1"},
{Host: "other.com", Path: "/", Name: "b", Value: "2"},
}}
err := jar.Delete(Cookie{Host: "example.com", Path: "/", Name: "a"})
if err != nil {
t.Fatalf("Delete() error: %v", err)
}
cookies, _ := jar.GetAll()
if len(cookies) != 1 {
t.Fatalf("after Delete, GetAll() returned %d cookies, want 1", len(cookies))
}
if cookies[0].Name != "b" {
t.Errorf("remaining cookie name = %q, want %q", cookies[0].Name, "b")
}
}
func TestStaticCookieJar_Delete_NotFound(t *testing.T) {
jar := &staticCookieJar{cookies: []Cookie{
{Host: "example.com", Path: "/", Name: "a", Value: "1"},
}}
err := jar.Delete(Cookie{Host: "nonexistent.com", Path: "/", Name: "x"})
if err != nil {
t.Fatalf("Delete() error: %v", err)
}
cookies, _ := jar.GetAll()
if len(cookies) != 1 {
t.Fatalf("after no-op Delete, GetAll() returned %d cookies, want 1", len(cookies))
}
}
func TestReadOnlyCookieJar_SetIsNoop(t *testing.T) {
inner := &staticCookieJar{cookies: []Cookie{
{Host: "example.com", Path: "/", Name: "a", Value: "1"},
}}
ro := ReadOnlyCookieJar{Jar: inner}
err := ro.Set(Cookie{Host: "example.com", Path: "/", Name: "new", Value: "val"})
if err != nil {
t.Fatalf("Set() error: %v", err)
}
cookies, _ := inner.GetAll()
if len(cookies) != 1 {
t.Errorf("ReadOnlyCookieJar.Set should be noop, but inner jar has %d cookies", len(cookies))
}
}
func TestReadOnlyCookieJar_DeleteIsNoop(t *testing.T) {
inner := &staticCookieJar{cookies: []Cookie{
{Host: "example.com", Path: "/", Name: "a", Value: "1"},
}}
ro := ReadOnlyCookieJar{Jar: inner}
err := ro.Delete(Cookie{Host: "example.com", Path: "/", Name: "a"})
if err != nil {
t.Fatalf("Delete() error: %v", err)
}
cookies, _ := inner.GetAll()
if len(cookies) != 1 {
t.Errorf("ReadOnlyCookieJar.Delete should be noop, but inner jar has %d cookies", len(cookies))
}
}
func TestReadOnlyCookieJar_GetAll(t *testing.T) {
inner := &staticCookieJar{cookies: []Cookie{
{Host: "example.com", Path: "/", Name: "a", Value: "1"},
}}
ro := ReadOnlyCookieJar{Jar: inner}
cookies, err := ro.GetAll()
if err != nil {
t.Fatalf("GetAll() error: %v", err)
}
if len(cookies) != 1 {
t.Errorf("ReadOnlyCookieJar.GetAll() returned %d cookies, want 1", len(cookies))
}
}
func TestReadOnlyCookieJar_Get(t *testing.T) {
inner := &staticCookieJar{cookies: []Cookie{
{Host: "example.com", Path: "/", Name: "a", Value: "1"},
}}
ro := ReadOnlyCookieJar{Jar: inner}
cookies, err := ro.Get("https://example.com/page")
if err != nil {
t.Fatalf("Get() error: %v", err)
}
if len(cookies) != 1 {
t.Errorf("ReadOnlyCookieJar.Get() returned %d cookies, want 1", len(cookies))
}
}