大家早上好,我將我的 symfony 2.8 應用程式完全遷移到了 5.4 版。我現在處于單元測驗階段。我將測驗復制到我的新專案中,但是我在 API 身份驗證方面遇到了一些困難。我使用 rest bundle 的經典配置啟動了未修改的單元測驗。在我的測驗中,我測驗了上游用戶的身份驗證,以便恢復測驗不同端點所需的權限。當我想使用 _username 和 _password 進行身份驗證時,我的回應內容中出現以下錯誤:
<!-- Invalid JSON. (400 Bad Request) -->
這是我的抽象類的 authUser 函式的內容,它允許對用戶進行身份驗證:
<?php
namespace WORD\UserBundle\Tests\Model;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Bundle\FrameworkBundle\Client;
abstract class AbstractAuthTestCase extends WebTestCase
{
/**
* @var Client
*/
protected $client = null;
/**
* ContainerInterface
*/
//protected $container;
// public function setUp(): void
// {
// self::ensureKernelShutdown();
// $this->client = static::createClient();
// // $this->container = $this->client->getContainer();
// $this->client->setServerParameter('HTTPS', true);
// $this->client->setServerParameter('HTTP_HOST', self::$container->getParameter('api_host'));
// $this->client->setServerParameter('HTTP_CONTENT_TYPE', 'application/json');
// }
public function authUser($username='stack.fr', $password='overflow', $referer='https://stack.over.local/')
{
$this->newClient();
$this->client->setServerParameter('HTTP_REFERER', $referer);
$this->client->request('POST', '/api/login_check', array(
'_username' => $username,
'_password' => $password,
));
$response = json_decode($this->client->getResponse()->getContent(), true);
if (isset($response['token'])) {
$this->client->setServerParameter('HTTP_AUTHORIZATION', 'Bearer ' . $response['token']);
}
return $response;
}
private function newClient()
{
$this->client = static::createClient();
//$this->container = $this->client->getContainer();
$this->client->setServerParameter('HTTPS', true);
$this->client->setServerParameter('HTTP_HOST', self::$container->getParameter('api_host'));
$this->client->setServerParameter('HTTP_CONTENT_TYPE', 'application/json');
}
}
如果我忘記了什么,你能告訴我嗎?我正在使用 FOS/RESTBundle 3.4 開發 Symfony 5.4
根據檔案,這個測驗方法在 symfony 上仍然有效。5 版本。謝謝您的幫助
uj5u.com熱心網友回復:
我終于找到了解決我的問題的方法。我不得不更改查詢的格式:
$this->client->request(
'POST',
'/api/login_check',
[],
[],
['CONTENT_TYPE' => 'application/json'],
'{"_username":"'.$username.'","_password": "'.$password.'" }'
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/527635.html
標籤:php交响乐森林捆绑
下一篇:升級到TYPO39.5,得到SymfonyRuntimeException...命令控制器類“TYPO3\CMS\Lang\Command\LanguageCommandController”
