Cookies
Send cookies by name and value, or build them with apitest.NewCookie.
apitest.Handler(handler).
Get("/hello").
Cookie("session", "12345").
Cookies(
apitest.NewCookie("theme").Value("dark"),
).
Expect(t).
Status(http.StatusOK).
End()| Method | Behaviour |
|---|---|
Cookie(name, value) | Adds a cookie. |
Cookies(...*apitest.Cookie) | Adds each cookie. The function is variadic. |
Building cookies
apitest.NewCookie(name) returns a builder with the methods Value, Path, Domain, Expires, MaxAge, Secure and HttpOnly. A request only carries the name and value of a cookie to the server, so the other attributes matter mainly when asserting on response cookies.
Every field of apitest.Cookie is a pointer, so a field that was never set is distinguishable from its zero value. Assertions use this to compare only the fields you set.
Converting an http.Cookie
apitest.FromHTTPCookie converts a *http.Cookie, for example one returned by a login step:
sessionCookie := &http.Cookie{Name: "session", Value: "12345"}
apitest.Handler(handler).
Get("/hello").
Cookies(apitest.FromHTTPCookie(sessionCookie)).
Expect(t).
Status(http.StatusOK).
End()ToHttpCookie converts the other way.
Sessions across requests
Each apitest test sends one request. To carry cookies from one request to the next, such as a session cookie set at login, run against a real server with a cookie jar. See end-to-end tests.