Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BrowserKit]Cannot set Custom Header #5074

Closed
windix opened this issue Jul 27, 2012 · 16 comments
Closed

[BrowserKit]Cannot set Custom Header #5074

windix opened this issue Jul 27, 2012 · 16 comments

Comments

@windix
Copy link

windix commented Jul 27, 2012

Hi,

I noticed the Client::request() function doesn't support pass custom header. It can only set $_SERVER, but not headers directly.

It is exactly the same problem as mentioned here: http://stackoverflow.com/questions/11549672/symfony-functional-test-custom-headers-not-passing-through

Thanks,
Wei

@stof
Copy link
Member

stof commented Jul 27, 2012

headers are also part of $_SERVER, by prefixing them with HTTP_

@windix
Copy link
Author

windix commented Jul 30, 2012

Thanks stof. This can be closed.

In my case, I am using a custom header "X-Session-Token". In the testing code, it can be send as "HTTP_X_SESSION_TOKEN":

$this->client->request($method,
    $uri,
    array(),    // parameters
    array(),    // files
    array('HTTP_X_SESSION_TOKEN' => $session_token), // server
    json_encode($content));

And the following code works fine on server side:

$request->headers->get('x-session-token')

@windix windix closed this as completed Jul 30, 2012
@samsullivan
Copy link

I still consider this an issue. Why don't you call apache_request_headers() if it exists and fallback to the headers set in $_SERVER? This way custom headers do not need to be prefaced with HTTP_.

@migueleliasweb
Copy link

I still consider this an issue too. There should be an parameter for the header array.

@gnat42
Copy link
Contributor

gnat42 commented Aug 8, 2014

I just spent 3 hours trying to figure out why the headers I set weren't making it to the request object. Quite annoying.

@robhogan
Copy link

+1. I've been setting X-Forwarded-Proto in behat/mink using Behat\Mink\Session::setRequestHeader(). This worked fine with the Goutte driver but not the Symfony2 Kernel driver and it took a long while for me to figure out this was the problem. It doesn't seem like it should be the job of my test context to worry about this inconsistency.

@rayrutjes
Copy link

I also think this is still an issue, is this documented anywhere except here?

@xabbuh
Copy link
Member

xabbuh commented Aug 2, 2015

@rayrutjes Please open an issue in the docs repository if you think that we can improve the documentation in a way.

@gideonfell
Copy link

+1

1 similar comment
@BillBrower
Copy link

+1

@Kansukey
Copy link

The custom headers do not need to be prefaced with HTTP_

I do agree. Moreover, this is not documented.

@themark147
Copy link

I spent a couple of hours to find out why request didn't contain my token ... oh

@tomtomklima
Copy link

Same story - one hour researching why headers are not used... prefixed with HTTP_ did the trick.

javiereguiluz added a commit to symfony/symfony-docs that referenced this issue Apr 6, 2021
…nt must have HTTP_ prefix (Ferror)

This PR was submitted for the 5.x branch but it was merged into the 4.4 branch instead.

Discussion
----------

[Testing] Every custom header in functional testing client must have HTTP_ prefix

<!--

If your pull request fixes a BUG, use the oldest maintained branch that contains
the bug (see https://symfony.com/releases for the list of maintained branches).

If your pull request documents a NEW FEATURE, use the same Symfony branch where
the feature was introduced (and `5.x` for features of unreleased versions).

-->
Due to symfony/symfony#5074 issue, I would propose to add a simple caution block with information, that the `HTTP_` prefix is mandatory - it will save some hours for others.

Commits
-------

8e5d9bc Every custom header in testing client must have HTTP prefix
@fahimbinkhair
Copy link

Thanks stof. This can be closed.

In my case, I am using a custom header "X-Session-Token". In the testing code, it can be send as "HTTP_X_SESSION_TOKEN":

$this->client->request($method,
    $uri,
    array(),    // parameters
    array(),    // files
    array('HTTP_X_SESSION_TOKEN' => $session_token), // server
    json_encode($content));

And the following code works fine on server side:

$request->headers->get('x-session-token')

only worked for me after adding HTTP_ as prefix

@klobastov
Copy link

My case with KernelBrowser API auth in codeception functional test (before setServerParamter with key jwt):

$client->request(
            'GET',
            $uri,
            [],
            [],
            [
                'CONTENT_TYPE' => 'application/json',
                'HTTP_AUTHORIZATION' => 'Bearer ' . $client->getServerParameter('jwt')
            ],
        );

ps
$client->loginUser($user) not work for api request in functional tests

@allan-simon
Copy link

For those wondering why "Authorization" is more "custom" than Content_type, actually it's more that EVERY header except Content-* headers need to be prefixed by HTTP-

https://github.com/symfony/symfony/blob/6.4/src/Symfony/Component/BrowserKit/HttpBrowser.php#L118

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests