Skip to content

[5.1] [Proposal] Add WithoutCsrf trait for testing #9354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed

[5.1] [Proposal] Add WithoutCsrf trait for testing #9354

wants to merge 4 commits into from

Conversation

laurencei
Copy link
Contributor

This allows you to disable just the CSRF middleware check on your tests, without having to disable all middleware.

It is just a stripped down version of withoutMiddleware that already exists.

This can be useful when testing form submissions which may require aspects of your middleware to be used - but without having to include a csrf token in each of your tests.

If this is accepted, I'll submit a PR for the Docs.

@GrahamCampbell
Copy link
Member

👎 I think people should just unset the middleware.

@laurencei
Copy link
Contributor Author

The problem is if your form processing does something with the session - then Laravel starts complaining of errors since you're not setting the session stuff in the middleware.

So disabling middleware is not always an option - which is why I wrote this...

@GrahamCampbell
Copy link
Member

So disabling middleware is not always an option

I was meaning people just just remove a single middleware from the stack.

@laurencei
Copy link
Contributor Author

Oh... ok - that makes sense?

How do you do that? Perhaps we add an example of that to the test Docs? Then the solution is available for everyone who needs to disable CSRF for testing...

if you want to post the code I'll do up the Doc PR - or you can do the Docs PR yourself if you have time.

@GrahamCampbell
Copy link
Member

We don't seem to actually have a way of removing middlewares without you doing reflection on the router object. Maybe we need to add that?

Another alternative could be for you to have a custom kernel class that you override the middleware on. I've seen people do that for testing packages before.

@lucasmichot
Copy link
Contributor

Seems related to #9311

@taylorotwell
Copy link
Member

Maybe it would be better to have a helper to easily set the CSRF token rather than have methods to remove individual middleware?

@laurencei laurencei deleted the patch-2 branch June 23, 2015 03:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants