Temporarily disable / bypass middleware

In my application, I implemented OAuth2-Server ( oauth2-server-laravel ) in combination with a custom authentication package ( Cartente Sentinel ).

In my .php routes:

Route::group(['before' => 'oauth'], function() { // ... some routes here } 

Therefore, the request must contain an authorization header, or the application terminates with an OAuthException.

Now I want to disable my controllers. Therefore, I have to seed my database with an OAuth session and an access token for each test. Then rewrite the call() method of TestCase and set the HTTP authorization header with a token carrier.

Is there a way to disable or bypass middleware (in my case, only for unit testing)?

In Laravel 4, they were called route filters, and in any case, they were disabled in the test environment. You can also manually enable / disable them using Route::enableFilters() .

+5
source share
3 answers

Apparently, with the release of Laravel 5.1, the disableMiddleware() method was added to the TestCase class yesterday, which now does exactly what I wanted.

The problem is resolved. :)

+5
source

The only answer I could come up with was to put a workaround in the middleware itself. For instance:

 public function handle($request, Closure $next) { // Don't validate authentication when testing. if (env('APP_ENV') === 'testing') { return $next($request); } // ... continue on to process the request } 

I don't like the idea of ​​making middleware dependent on the application environment, but I don't see any other options.

+2
source

Here is the package I was working on after it had the same problem.

https://github.com/moon0326/FakeMiddleware

0
source

Source: https://habr.com/ru/post/1216526/


All Articles