apitest
Assertions

Cookies

Assert on cookie values and attributes, and on whether a cookie was set.

apitest.Handler(handler).
	Get("/user/1234").
	Expect(t).
	Status(http.StatusOK).
	Cookie("theme", "dark").
	Cookies(
		apitest.NewCookie("session").
			Value("12345").
			Path("/").
			HttpOnly(true),
	).
	CookiePresent("csrf").
	CookieNotPresent("legacy").
	End()
MethodPasses when
Cookie(name, value)A cookie with the name was set, with that value.
Cookies(...*apitest.Cookie)Each cookie was set, and matches every field you set on it.
CookiePresent(name)A cookie with the name was set, whatever its value.
CookieNotPresent(name)No cookie with the name was set.

Only the fields you set are compared

apitest.NewCookie(name) builds the expected cookie. Fields you do not set are ignored, so NewCookie("session").Value("12345") passes whatever the cookie's path, expiry or flags.

Set more fields to check them too:

apitest.Handler(handler).
	Post("/login").
	Expect(t).
	Cookies(
		apitest.NewCookie("session").
			Value("12345").
			Path("/").
			Domain("example.com").
			Expires(expires).
			MaxAge(3600).
			Secure(true).
			HttpOnly(true),
	).
	End()

The fields that can be checked are Value, Path, Domain, Expires, MaxAge, Secure and HttpOnly.

Cookies with changing values

Session IDs and CSRF tokens change on every run. Check that they were set with CookiePresent, and check their attributes with Cookies while leaving out Value.

On this page