我想用基本身份验证测试一个小型API。
路线如下:
Route::group(array('prefix' => 'api/v1', 'before' => 'auth.basic'), function()
{
Route::get('/', function()
{
return 'Hello';
});
});
测试看起来是这样的:
class APITest extends TestCase {
public function testHTTPAuthFailed()
{
$response = $this->call('GET', 'api/v1');
fwrite(STDERR, print_r($response->getContent(), TRUE));
}
}
当我使用phpunit运行测试时,我可以在控制台中看到"Hello"。这不是我所期望的,我想我会看到"身份验证失败"。但是,如果我尝试用cURL(在终端上)调用我的API,我可以看到验证失败(所以没关系,这是我的例外):
$ curl -i host/api/v1
HTTP/1.1 401 Unauthorized
那么为什么laravel(4.2)中的phpunit会跳过(?)基本身份验证呢。如何测试无效凭据?
我刚刚在另一个问题中找到了答案。我需要在我的测试函数中添加:Route::enableFilters();
,这没关系。