Php 如何在symfony2功能测试中发出https请求?
您好,我使用phpunit进行测试,使用Symfony\Bundle\FrameworkBundle\Test\WebTestCase进行单元测试。到目前为止还没有问题,但现在我们开始使用https,我的测试不再有效。我开始为测试中的每个请求获取301响应代码。我的问题是如何告诉Symfony\Component\HttpKernel\Client向 https://localhost.com/uri 而不是http://localhost.com/uri ? 编辑 在symfony网站上,他们展示了如何配置服务器参数,还有一个类似于peace的代码Php 如何在symfony2功能测试中发出https请求?,php,symfony,https,http-status-code-301,functional-testing,Php,Symfony,Https,Http Status Code 301,Functional Testing,您好,我使用phpunit进行测试,使用Symfony\Bundle\FrameworkBundle\Test\WebTestCase进行单元测试。到目前为止还没有问题,但现在我们开始使用https,我的测试不再有效。我开始为测试中的每个请求获取301响应代码。我的问题是如何告诉Symfony\Component\HttpKernel\Client向 https://localhost.com/uri 而不是http://localhost.com/uri ? 编辑 在symfony网站上
$client->request(
'GET',
'/demo/hello/Fabien',
array(),
array(),
array(
'CONTENT_TYPE' => 'application/json',
'HTTP_REFERER' => '/foo/bar',
'HTTP_X-Requested-With' => 'XMLHttpRequest',
)
);
我试着给HTTPS元素,就像我的代码中提到的一样
$client->request('GET',
'/'.$version.'/agencies/'.$agencyId,
array(),
array(),
array('HTTPS' => 'on')
);
但是,它仍然不起作用?多亏了@WouterJ,我将我的客户端创建更改为:
static::createClient();
致:
它解决了我的问题。
事实证明,我不能在client->request中提供HTTP_主机和HTTPS参数。它应该在客户端创建时确定。我正在使用Symfony4,这就是我创建功能测试的方式。我已经测试了以下代码,它运行良好
namespace App\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class CategoryControllerTest extends WebTestCase
{
public function testList()
{
$client = static::createClient();
$client->request('GET', '/category/list');
$this->assertEquals(200, $client->getResponse()->getStatusCode());
}
}
您是否尝试过$client->request('GET',')?他想要的是https请求而不是http请求。$this->createClient()的第三个参数是服务器数组。如果你设置了“https”元素呢?@WouterJ我试着在我的edit@OmerTemel我说了createClient的第三个参数,您使用了Request的第四个参数一个可能的替代解决方案是调用
$client->setServerParameter('HTTPS',true)已实例化的客户端对象上的代码>
namespace App\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class CategoryControllerTest extends WebTestCase
{
public function testList()
{
$client = static::createClient();
$client->request('GET', '/category/list');
$this->assertEquals(200, $client->getResponse()->getStatusCode());
}
}