///|
test "store a response's cookies and send them back" {
let clock = @sim.FakeClock::new(start_unix_ms=1_000_000L)
let jar = @cookie.CookieJar::new(clock)
jar.store(
@mttp_core.Headers::of([
("Set-Cookie", "sid=abc; Path=/; Secure; HttpOnly"),
("Set-Cookie", "theme=dark; Max-Age=60"),
]),
"https://example.com/login",
)
assert_eq(jar.header_for("https://example.com/"), Some("sid=abc; theme=dark"))
// Secure cookies stay off plain HTTP.
assert_eq(jar.header_for("http://example.com/"), Some("theme=dark"))
clock.advance(61_000)
assert_eq(jar.header_for("https://example.com/"), Some("sid=abc"))
}pub struct CookieJar {
// private fields
}test {
let jar = @cookie.CookieJar::new(@sim.FakeClock::new())
jar.store(
@mttp_core.Headers::of([("Set-Cookie", "sid=1; Path=/; Secure")]),
"https://example.com/login",
)
assert_eq(jar.header_for("https://example.com/"), Some("sid=1"))
assert_eq(jar.header_for("http://example.com/"), None)
}Install
Download zipHTTP for MoonBit without I/O, for libraries such as SDKs: the types, an HTTP/1.1 codec and engines, a client with retries under a budget, deadlines, rate limits, redirects and cookies, SSE, multipart, and typed endpoints with a router and OpenAPI. Apps add gaato/mttp.