【问题标题】:How to make https requests in symfony2 functional test?如何在 symfony2 功能测试中发出 https 请求?
【发布时间】:2013-11-14 11:16:25
【问题描述】:

您好,我使用 phpunit 进行测试,使用 Symfony\Bundle\FrameworkBundle\Test\WebTestCase 进行单元测试。到目前为止没有问题,但现在我们开始使用 https,我的测试不再工作了。我开始为我的测试中的每个请求获取 301 响应代码。我的问题是我如何告诉 Symfony\Component\HttpKernel\Client 向 https://localhost.com/uri 而不是 http://localhost.com/uri ?

编辑

在 symfony 网站http://symfony.com/doc/current/book/testing.html 他们展示了如何配置服务器参数并且有一个代码和平之类的

$client->request(
 'GET',
 '/demo/hello/Fabien',
 array(),
 array(),
 array(
     'CONTENT_TYPE'          => 'application/json',
     'HTTP_REFERER'          => '/foo/bar',
     'HTTP_X-Requested-With' => 'XMLHttpRequest',
 )
);

我尝试将 http://php.net/manual/en/reserved.variables.server.php 中提到的 HTTPS 元素更改为我的代码

$client->request('GET',
         '/'.$version.'/agencies/'.$agencyId,
         array(), 
         array(),
         array('HTTPS' => 'on')
         );

但是还是不行?

【问题讨论】:

  • 你试过 $client->request('GET', 'localhost/uri'); ?
  • 他想要一个 https 请求而不是 http 请求。
  • $this->createClient() 的第三个参数是一个服务器数组。如果设置“https”元素会怎样?
  • @WouterJ 我在编辑中尝试并解释了
  • @OmerTemel 我说的是 createClient 的第三个参数,你使用了 request 的第四个参数

标签: php symfony https http-status-code-301 functional-testing


【解决方案1】:

我正在使用 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());
    }
}

【讨论】:

    【解决方案2】:

    感谢@WouterJ,我将我的客户端创建从:

    static::createClient();
    

    到:

    static::createClient(array(),array('HTTPS' => true));
    

    它解决了我的问题。 事实证明,我不能在客户端->请求中给出 HTTP_HOST 和 HTTPS 参数。应在创建客户端时确定。

    【讨论】:

    • 一种可能的替代解决方案是在已经实例化的客户端对象上调用$client->setServerParameter('HTTPS', true);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 2015-09-24
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多