41

For some reason when i sent a new $client->request the headers I specify get lost:

public function testGetClientsAction()
{
    $client = static::createClient();

    $cookie = new Cookie('locale2', 'en', time() + 3600 * 24 * 7, '/', null, false, false);
    $client->getCookieJar()->set($cookie);

    // Visit user login page and login
    $crawler = $client->request('GET', '/login');
    $form = $crawler->selectButton('login')->form();
    $crawler = $client->submit($form, array('_username' => 'greg', '_password' => 'greg'));

    $client->request(
       'GET', 
       '/clients', 
        array(), 
        array(), 
        array('X-Requested-With' => 'XMLHttpRequest', 'accept' => 'application/json')
    );

    print_r($client->getResponse());
    die();

}

In the method that is being tested I have this on the first line:

print_r($request->headers->all());

The response is as follows:

Array
(
    [host] => Array
        (
            [0] => localhost
        )

    [user-agent] => Array
        (
            [0] => Symfony2 BrowserKit
        )

    [accept] => Array
        (
            [0] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
        )

    [accept-language] => Array
        (
            [0] => en-us,en;q=0.5
        )

    [accept-charset] => Array
        (
            [0] => ISO-8859-1,utf-8;q=0.7,*;q=0.7
        )

    [referer] => Array
        (
            [0] => http://localhost/login_check
        )

    [x-php-ob-level] => Array
        (
            [0] => 1
        )

)
greg
  • 6,853
  • 15
  • 58
  • 71

2 Answers2

73

I have the same issue and after a little dig through I think it is a feature that BrowserKit currently doesn't support.

I have logged an issue for it: https://github.com/symfony/symfony/issues/5074

Update: this is not an issue -- see the comments below

Sample Code

Sample request:

$client->request(
    'GET',
    $url,
    array(),
    array(),
    array(
        'HTTP_X_CUSTOM_VAR' => $var
    )
);

Fetching the data:

$request->headers->get('x-custom-var');
kosinix
  • 1,779
  • 1
  • 18
  • 22
Windix
  • 891
  • 7
  • 5
  • 24
    Update: from a comment I got from stof in the issue, just pass a variation of your custom header by prefixing it with "HTTP_" and it will works. See the above issue for an working example in my case. – Windix Jul 30 '12 at 02:12
  • Good stuff. Thanks for your help! – greg Jul 31 '12 at 17:35
  • 3
    This is not the case anymore (sym2.5) - header without a prefix is fine! Check http://symfony.com/doc/current/book/testing.html for example – gregor Jul 11 '14 at 08:40
  • 4
    @gregor is only partially right. custom headers will work only with "HTTP_" prefix while standard header keys will work without. – Udan Jul 27 '15 at 09:45
  • 3
    Still a valid response after all this time. What's the rationale behind this decision? Seems absurd. – Anton Babushkin Sep 15 '17 at 01:10
  • notice the difference you pass _ but then you read with - – max4ever Feb 16 '22 at 14:23
22

If you check the definition from Client.php, at method request, you will see in the docblock a very useful information:

  • @param array $server The server parameters (HTTP headers are referenced with a HTTP_ prefix as PHP does)

This means that you should add a HTTP_ prefix to the header you want to send. For example if you want to pass header X-HTTP-Method-Override you specify it like so:

    $client->request(
        Request::METHOD_POST,
        '/api/v1/something',
        $body,
        [],
        ['HTTP_X-HTTP-Method-Override' => Request::METHOD_GET]
    );
Adrian C.
  • 742
  • 6
  • 14