lubart's avatar

csrf_token in PHPUnit test for Laravel 5.2 project

Hi,

I'm trying to upgrade Laravel from 5.1 to 5.2 in my project. I have done all instructions from the official manual and project works normally, but some PHPUnit tests are failed.

In all failed tests POST call like following is used:

\Session::start(); $this->call("POST", "/myroute", [ '_token' => csrf_token(), 'param' => $param ]);

Previously in Laravel 5.1 this worked well, but in new Laravel 5.2.45 version, request is refused in the controller. I believe it happens due to some changes with csrf verifications, but I do not know how to fix this issue...

0 likes
6 replies
bobbybouwmann's avatar
Level 88

First of all you don't have to do session::start because Laravel does that for you ;)

Anyway, what error do you get? You must get some fail message right?

shez1983's avatar

[ '_token' => csrf_token() is incorrect.. as it is an api (lumen) i dont think it cares about csrf?

shez1983's avatar

so he is.. i swear i thought he was talking about lumen... :s

lubart's avatar

@bobbybouwmann Thank you for answer! Indeed, test passes even without \Session::start();

Now I see that my question was not fully correct, but in any case thank you so much for answer that gave me a chance to review my problem! The problem was not in csrf verification, but in using Symfony\Component\HttpFoundation\File\UploadedFile class for attachment in this request instead of Illuminate\Http\UploadedFile.

bobbybouwmann's avatar

Aah yes! You indeed need to use the correct namespace ;) Glad you fixed it!

1 like

Please or to participate in this conversation.