Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

LiamA's avatar
Level 1

Can't turns off cookie encryption while testing the API

Hi,

I'm currently writing a test for an API endpoint involving the use of cookies.

My problem is that when I try to fetch the cookie value via Cookie::get(), I seem to be getting an encrypted value.

I tried adding the cookie name to the $except array in EncryptionCookies.php, but that doesn't seem to have changed anything.

I also tried clearing the cache of course.

Any ideas what's going on there?

0 likes
6 replies
bugsysha's avatar

I wouldn't try to get the value of the cookie in my test. I would only assert that the cookie was added to the response and that is it.

LiamA's avatar
Level 1

But I'm relying on it's value, and want to make an assertion on it

bugsysha's avatar

If this value is generated by your code then test the piece of code that is generating it's value, not the cookie itself. So if you want to test is the Laravel setting the cookie correctly don't do it. First reason is that you shouldn't test what you don't own. Second reason is that Laravel is used by bunch of developers and companies, and if something was broken there it would be noticed pretty fast and fixed even faster.

LiamA's avatar
Level 1

I am testing a cookie I'm setting, not Laravel.

I managed to find a way to get the cookie value:

\Crypt::decrypt((Cookie::get('cookie-name')), false)

This returns a long string made of two sub strings separated by a pipe. The right sub string is the value i set in the cookie. I'm not sure what the left one is yet...

LiamA's avatar
Level 1

But in order to test behaviour of the app that's consuming the API you do need states. Am I wrong?

I mean take for example the actingAs method:

$this->actingAs($user), 'api')

Isn't that what its for?

Please or to participate in this conversation.