fix: bug fixes, test coverage, and CI workflow
- Fix Nodes.First() panic on empty slice (return nil) - Fix ticker leak in archive.go (create once, defer Stop) - Fix cookie path matching for empty and root paths - Fix lost query params in google.go (u.Query().Set was discarded) - Fix type assertion panic in useragents.go - Fix dropped date parse error in powerball.go - Remove unreachable dead code in megamillions.go and powerball.go - Simplify document.go WaitForNetworkIdle, remove unused root field - Remove debug fmt.Println calls across codebase - Replace panic(err) with stderr+exit in all cmd/ programs - Fix duckduckgo cmd: remove useless defer, return error on bad safesearch - Fix archive cmd: ToConfig returns error instead of panicking - Add 39+ unit tests across 6 new test files - Add Gitea Actions CI workflow (build, test, vet in parallel) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
266
cookiejar_test.go
Normal file
266
cookiejar_test.go
Normal file
@@ -0,0 +1,266 @@
|
||||
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{
|
||||
Cookie{Host: "a.com", Name: "a", Value: "1"},
|
||||
Cookie{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{
|
||||
Cookie{Host: "example.com", Path: "/", Name: "a", Value: "1"},
|
||||
Cookie{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{
|
||||
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{
|
||||
Cookie{Host: "example.com", Path: "/", Name: "a", Value: "1"},
|
||||
Cookie{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{
|
||||
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{
|
||||
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{
|
||||
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{
|
||||
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{
|
||||
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))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user