package session import ( "context" "crypto/md5" "fmt" "net/http/httptest" "testing" ) func TestPrestashopCookieDomain(t *testing.T) { if got := prestashopCookieDomain("localhost", nil); got != "" { t.Fatalf("prestashopCookieDomain(localhost) = %q, want empty", got) } if got := prestashopCookieDomain("shop.example.com", []string{"shop.example.com"}); got != ".example.com" { t.Fatalf("prestashopCookieDomain(shared) = %q, want %q", got, ".example.com") } if got := prestashopCookieDomain("shop.example.com", nil); got != "shop.example.com" { t.Fatalf("prestashopCookieDomain(single) = %q, want %q", got, "shop.example.com") } } func TestURIMatchesRequest(t *testing.T) { if !uriMatchesRequest("/shop/fr/", "/shop/fr/product/test") { t.Fatalf("expected nested shop URI to match request path") } if uriMatchesRequest("/shop/fr/", "/shop/en/product/test") { t.Fatalf("unexpected match for different shop URI") } } func TestCookieDomainSourcePrefersDatabaseDomain(t *testing.T) { shop := &cookieShopContext{ Domain: "shop.example.com", DomainSSL: "secure.example.com", } if got := cookieDomainSource(shop, "proxy.internal"); got != "secure.example.com" { t.Fatalf("cookieDomainSource() = %q, want %q", got, "secure.example.com") } } func TestCookieDomainSourceKeepsMatchingDatabaseHost(t *testing.T) { shop := &cookieShopContext{ Domain: "shop.example.com", DomainSSL: "secure.example.com", } if got := cookieDomainSource(shop, "shop.example.com"); got != "shop.example.com" { t.Fatalf("cookieDomainSource() = %q, want %q", got, "shop.example.com") } } func TestOverrideCookieHashDomain(t *testing.T) { if got := overrideCookieHashDomain(".Example.com"); got != "example.com" { t.Fatalf("overrideCookieHashDomain() = %q, want %q", got, "example.com") } } func TestResolveCookieNameReturnsExplicitOverride(t *testing.T) { service := NewService(nil, "ps_", "1.7.3", "PrestaShop-fixed", "") got, err := service.ResolveCookieName(context.Background(), httptest.NewRequest("GET", "https://shop.example.com/", nil)) if err != nil { t.Fatalf("ResolveCookieName() error = %v", err) } if got != "PrestaShop-fixed" { t.Fatalf("ResolveCookieName() = %q, want %q", got, "PrestaShop-fixed") } } func TestDomainCookieOverrideParticipatesInHash(t *testing.T) { sum := md5.Sum([]byte("1.7.3" + "ps-s1" + overrideCookieHashDomain(".example.com"))) got := fmt.Sprintf("PrestaShop-%x", sum) want := "PrestaShop-1e5aa4f42a55532134a4e84017cdf643" if got != want { t.Fatalf("derived cookie name = %q, want %q", got, want) } } func TestNormalizeCookiePath(t *testing.T) { if got := normalizeCookiePath(""); got != "/" { t.Fatalf("normalizeCookiePath(\"\") = %q, want %q", got, "/") } if got := normalizeCookiePath("shop"); got != "/shop/" { t.Fatalf("normalizeCookiePath(\"shop\") = %q, want %q", got, "/shop/") } }