dillonkearns/elm-pages/12.3.0

Test.PagesProgram.CookieJar

A cookie jar used to seed the initial request of a test and to track cookies across subsequent requests. Supports:

  • Setting/getting individual cookies
  • Seeding a signed session cookie via setSession
  • Parsing Set-Cookie response headers to capture cookies and their attributes (Path, Domain, Expires, Max-Age, Secure, HttpOnly, SameSite) across requests
  • Applying the jar to a Test.BackendTask.TestSetup via withCookies

A collection of cookies maintained across HTTP requests.

type alias CookieEntry =
{ value : String
, path : Maybe String
, domain : Maybe String
, expires : Maybe String
, maxAge : Maybe Int
, secure : Bool
, httpOnly : Bool
, sameSite : Maybe String
}

A cookie with its value and attributes as parsed from Set-Cookie.

Attributes default to unset/false when a cookie is added via set or setSession. withSetCookieHeaders populates attributes from response headers.

expires is preserved as the raw header string (date formats vary). sameSite is the raw attribute value ("Strict", "Lax", or "None").

Building

An empty cookie jar, ready for set and setSession.

set : String -> String -> CookieJar -> CookieJar

Set a cookie in the jar with the given name and value. Attributes default to unset/false; use withSetCookieHeaders to capture attributes from a response.

{ name : String
, secret : String
, session : Session
}

Set a signed session cookie in the jar. The Session value is encoded and signed the same way Server.Session encodes its payload at runtime, so routes reading the cookie get the seeded values back out.

import Test.PagesProgram.CookieJar as CookieJar
import Test.PagesProgram.Session as Session


CookieJar.init
    |> CookieJar.setSession
        { name = "mysession"
        , secret = "test-secret"
        , session =
            Session.init
                |> Session.withValue "userId" "42"
        }

The secret is recorded alongside the signed cookie so the visual test runner can surface which secret produced each signed cookie. The test mock does not use real HMAC, so secret may be any non-empty string without . characters.

get : String -> CookieJar -> Maybe String

Get a cookie's value from the jar.

getEntry : String -> CookieJar -> Maybe CookieEntry

Get a cookie's full entry (value + attributes) from the jar.

toDict : CookieJar -> Dict String String

Convert the cookie jar to a Dict of name/value pairs for use in request construction. Attributes are dropped — requests only need name/value.

entries : CookieJar -> List ( String, CookieEntry )

List every cookie in the jar with its full entry.

Capturing from responses

Apply a list of Set-Cookie header values to the cookie jar. Parses each header to extract the cookie name, value, and attributes (Path, Domain, Expires, Max-Age, Secure, HttpOnly, SameSite). Unknown attributes are ignored.

CookieJar.init
    |> CookieJar.withSetCookieHeaders
        [ "session=abc123; Path=/; HttpOnly"
        , "theme=dark"
        ]
    |> CookieJar.get "session"
    -- Just "abc123"

Seeding a test

withCookies : CookieJar -> TestSetup -> TestSetup

Apply the jar to a Test.BackendTask.TestSetup, seeding every cookie in the jar on the initial request.

import Test.BackendTask as BackendTaskTest
import Test.PagesProgram.CookieJar as CookieJar


BackendTaskTest.init
    |> CookieJar.withCookies
        (CookieJar.init
            |> CookieJar.set "theme" "dark"
        )