withSession method

DoxTestClient withSession()

A copy of this client that works like a browser with a session:

  • it keeps the cookies of every response (Set-Cookie) and sends them back. A cookie with an empty value, Max-Age=0 or an old Expires date is removed.
  • on POST, PUT, PATCH and DELETE it sends the XSRF-TOKEN cookie in the X-XSRF-TOKEN header, like axios and Inertia. So CsrfMiddleware lets the request in.

Each call gives a new browser with no cookies. Copies made from it (eg. with withHeaders) share its cookies.

DoxTestClient browser = client.withSession();
await browser.get('/login'); // gets the session and XSRF-TOKEN cookies
TestResponse res = await browser.post('/login', body: form);
res.expectStatus(302).expectHeader('location', '/dashboard');
(await browser.get('/dashboard')).expectStatus(200);

Implementation

DoxTestClient withSession() {
  return DoxTestClient._copy(baseUri, headers, _http, <String, String>{});
}