Guys, anybody?
Oct 31, 2014
14
Level 1
[5.0] PHPUnit testing the POST request with csrf
Hi guys.
I'm having a problem testing the POST request in Laravel 5.
This snippet tries to check if the user can subscribe to the newsletter:
$this->call('GET', '/landing/subscribe', [
'email' => $this->faker->email,
'_token' => Session::token(),
]);
When I run phpunit I get this error:
There was 1 error:
1) LandingControllerTest::it_submits_an_email_to_the_landing_page
Illuminate\Session\TokenMismatchException:
/home/vagrant/www/healthpro/app/Http/Middleware/VerifyCsrfToken.php:25
Here's the method from the middleware that tries to compare the token from the input:
protected function tokensMatch($request)
{
return $request->session()->token() == $request->input('_token');
}
The problem is that when I make the request the application creates a new session with the new token and I get this Exception.
So here's my question: How can I test the POST requests with csrf?
PS: I call the $this->startSession() in the setUp method to create the session for the testing.
Please or to participate in this conversation.