How to make https requests in symfony2 functional test?
Asked Answered
C

2

11

Hi i am using phpunit for testing and Symfony\Bundle\FrameworkBundle\Test\WebTestCase for unit testing. So far there were no problem but now we start to use https and my tests are not working anymore.I start to get 301 response code for my every request in my tests. My question is how do i tell to Symfony\Component\HttpKernel\Client to make requests to https://localhost.com/uri instead of http://localhost.com/uri ?

EDIT

In symfony website http://symfony.com/doc/current/book/testing.html they show how to configure server parameters and there is a code peace like

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

I tried to give HTTPS element as mentioned in http://php.net/manual/en/reserved.variables.server.php changed my code as

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

However, it is still not working?

Clarisclarisa answered 3/11, 2013 at 20:11 Comment(5)
did you try $client->request('GET', 'localhost/uri'); ?Kelantan
He wants an https request not an http request.Elisa
The third argument of $this->createClient() is a server array. What if you set the 'https' element?Thorner
@WouterJ i tried and explained in my editClarisclarisa
@OmerTemel i said third argument of createClient, you used fourth argument of requestThorner
C
17

Thanks to @WouterJ i changed my client creation from :

static::createClient();

to:

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

it solved my problem. It turns out that I cant give HTTP_HOST and HTTPS parameters in client ->request. It should be determined while client creation.

Clarisclarisa answered 4/11, 2013 at 8:11 Comment(1)
A possible alternate solution is calling $client->setServerParameter('HTTPS', true); on an already instantiated client objectDarlenedarline
S
-1

I am using Symfony4 and this how I created my functional test. I have tested following code and it works fine.

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());
    }
}
Saeger answered 15/10, 2018 at 3:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.