暂时禁用/绕过中间件 [英] Temporarily disable / bypass Middleware
问题描述
在我的应用程序中,我结合自定义实现了OAuth2-Server( oauth2-server-laravel )身份验证软件包( Satanel by Cartalyst ).
In my Application I implemented a OAuth2-Server (oauth2-server-laravel) in combination with a custom Authentication Package (Sentinel by Cartalyst).
在我的route.php中:
In my routes.php:
Route::group(['before' => 'oauth'], function()
{
// ... some routes here
}
因此,请求必须提供授权标头,否则应用程序将退出并带有OAuthException.
So the request must provide an authorization header or the application quits with an OAuthException.
现在我想对控制器进行单元测试.因此,我必须使用OAuth会话为数据库添加种子,并为每个测试访问令牌.
然后覆盖TestCase
的call()
方法,并使用Bearer令牌设置HTTP授权头.
Now I want to unittest my controllers. So I have to seed my database with a OAuth session and access token for every test.
Then overwrite the call()
-method of TestCase
and set the HTTP-Authorization Header with the Bearer Token.
是否有一种方法可以禁用或绕过中间件(在我的情况下仅用于单元测试)?
Is there a way to disable or bypass middleware (in my case just for unit testing)?
在Laravel 4中,它们被称为路由过滤器,并且无论如何它们都在测试环境中被禁用.您也可以使用Route::enableFilters()
手动启用/禁用它们.
In Laravel 4 they were called route filters and they were disabled in the testing environment anyway. You could also manually enable/disable them with Route::enableFilters()
.
推荐答案
显然,随着昨天Laravel 5.1的发布,在TestCase
类中添加了disableMiddleware()
方法,该方法现在可以完全满足我的要求.
Apparently with the release of Laravel 5.1 yesterday a disableMiddleware()
method was added to the TestCase
class, which now does exactly what I wanted.
问题已解决. :)
这篇关于暂时禁用/绕过中间件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!