Skip to content

CSRF token mismatch while testing #40467

Answered by SjorsO
maan56 asked this question in Q&A
Jan 18, 2022 · 2 comments · 1 reply
Discussion options

You must be logged in to vote

CSRF protection is skipped when running tests. That happens in the VerivyCsrfToken middleware. Laravel knows you're running tests if your APP_ENV is set to testing (which is set automatically in phpunit.xml). When you cache your config file, your APP_ENV is hardcoded to whatever is in your .envfile at the time (probably local). Then when you run your tests, Laravel thinks you're not running tests, and will check the CSRF token.

As a general tip, never run artisan optimize or config:cache in development. Only use them in production.

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@maan56
Comment options

Answer selected by maan56
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants