mardi 26 février 2019

Laravel 5.7 CSRF Middleware

According to Laravel 5.7 Docs

"The CSRF middleware is automatically disabled when running tests."

But I have the following Test

public function testUserLoginSuccesfully()
{
    $data = ['email' => 'test@user.com' , 'password' => bcrypt('test12345')];
    $csrf = csrf_token();

    $response = $this->withHeaders(['_token' => $csrf])
    ->post('/login',$data);

    $response->assertStatus(302)->assertRedirect('/home');

}

In order to work properly I have to disable csrf protection in the VerifyCsrfToken.php:

protected $except = [
    //
    '/login','/register'
];

If don't modify this property I'm getting a http 419 error. I don't know what I'm missing or how can I disable csrf only for testing.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire