Thanks to visit codestin.com
Credit goes to github.com

Skip to content

No way to override the Content-Type header in Browserkit Request #20042

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

Closed
zio-mitch opened this issue Sep 24, 2016 · 11 comments
Closed

No way to override the Content-Type header in Browserkit Request #20042

zio-mitch opened this issue Sep 24, 2016 · 11 comments

Comments

@zio-mitch
Copy link

zio-mitch commented Sep 24, 2016

Making a request with the method request() will always override the content-type header, with application/x-www-form-urlencoded.

Example to reproduce the problem

 $client = static::createClient();
 $headers['content-type'] = 'some.other valid IANA';
 $client->request('POST', '/api/test', [], [], $headers, 'HELO');

the request sent to the controller will contain application/x--www-form-urlencoded

Also trying with:

$headers['HTTP_content-type'] = 'some.other valid IANA' same result
@zio-mitch zio-mitch changed the title No way to override the Content-Type header Browserkit Request: No way to override the Content-Type header Sep 24, 2016
@ro0NL
Copy link
Contributor

ro0NL commented Sep 25, 2016

@Pherserk can you try CONTENT_TYPE or HTTP_CONTENT_TYPE... FYI you're passing in server parameters, not headers. Maybe

$contentHeaders = array('CONTENT_LENGTH' => true, 'CONTENT_MD5' => true, 'CONTENT_TYPE' => true);
kicks in some how..

However at first sight your version should work too based on

$key = str_replace('_', '-', strtolower($key));

Can you debug where stuff gets lost?

@zio-mitch
Copy link
Author

@ro0NL

Just added a failing test on this fork at line 167
https://github.com/Pherserk/browser-kit/blob/master/Tests/ClientTest.php

But due to the nature of the mock TestClient, I think it is not reliable.

@zio-mitch
Copy link
Author

zio-mitch commented Sep 25, 2016

@ro0NL
Probably I'm messing up things, some headers passed in the ServerBag are translated to headers.
I should use the headers bag, so what should be the correct way to call the client method I'm using to send the request?

Just to be more precise, I'm developing a Json Api user management util.

I use to validate Json Api request with this validator:
https://github.com/Pherserk/symfony2_demo_article_feed/blob/e409b71541f36decc0e1d804b6fa3a49bcb31498/src/AppBundle/Service/JsonApi/Validator/JsonRequestValidator.php

And I'm testing controllers using this util:
https://github.com/Pherserk/symfony2_demo_article_feed/blob/e409b71541f36decc0e1d804b6fa3a49bcb31498/src/AppBundle/Test/ApiWebTestCase.php

This is an example usage of this test util at line 37
https://github.com/Pherserk/symfony2_demo_article_feed/blob/0f07fa57f51dbb16cf63d374999b570e5c693ad6/src/AppBundle/Tests/Controller/Api/UserGroupControllerTest.php

Many, many thanks.

@ro0NL
Copy link
Contributor

ro0NL commented Sep 25, 2016

From your issue i kinda assumed you were using WebTestCase::createClient So perphaps compare to what Symfony\Component\HttpKernel\Client does, ie filtering the request correctly, here's where the magic happens.

Next your TestClient::doRequest is foobar.. it doesnt use $request at all.. so yes it's not reliable.

Im lost.. what exactly is the issue? WebTestCase, TestClient?

@zio-mitch
Copy link
Author

zio-mitch commented Sep 25, 2016

@ro0NL
Looking at the Client class in Browserkit component:

$this->internalRequest = new Request($uri, $method, $parameters, $files, $this->cookieJar->allValues($uri), $server, $content);

$this->request = $this->filterRequest($this->internalRequest);

if I dump the internalRequest, it preserves the content-type header, while request, does override it with the "application/x--www-form-urlencoded" value.
But I can't understand how this happens.

@ro0NL
Copy link
Contributor

ro0NL commented Sep 25, 2016

This is guessing.. i can have a look tonight maybe. But by default the request is not filtered.. ie $this->internalRequest === $this->request. So something else's happening down the chain.. cant you search for application/x--www-form-urlencoded and go from there?

@zio-mitch
Copy link
Author

@xabbuh i think we can close this issue,
I have opened this pull request, that is related to this, that is not a bug, but a sometimes difficult to spot behaviour.

symfony/http-foundation#32

@javiereguiluz javiereguiluz changed the title Browserkit Request: No way to override the Content-Type header No way to override the Content-Type header in Browserkit Request Sep 29, 2016
@Klamius
Copy link

Klamius commented Aug 30, 2017

@xabbuh @javiereguiluz @ro0NL
according to this line https://github.com/symfony/http-foundation/blob/master/Request.php#L369
If you set
$client->setServerParameter('CONTENT_TYPE' ,'ANY OTHER FORMAT');
OR

$client->request(
            'POST',
            'URL',
            [],
            [],
            ['CONTENT_TYPE' => 'ANY OTHER FORMAT'],
            ''
        );

this should works

@javiereguiluz
Copy link
Member

Closing because this should work as explained by @petertanath. @ro0NL thanks for your help here too.

@blaues0cke
Copy link

blaues0cke commented Jun 18, 2018

Can confirm that this works as expected:

$client->request(
    'POST',
    '/api/devices',
    [],
    [],
    [
        'CONTENT_TYPE' => 'application/ld+json',
    ],
    json_encode([
        'country'    => $country['@id'],
        'deviceId'   => 'randomDeviceId',
        'deviceType' => 'IOS',
    ])
);

@yapro
Copy link
Contributor

yapro commented Apr 4, 2020

Maybe this will help us symfony/browser-kit#10

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

7 participants