Core: Change in symfony/http-client 4.3.4 breaks headers in Test Client

Created on 3 Sep 2019  路  4Comments  路  Source: api-platform/core

Hi friends!

We're using the new testing Client/Response, etc in our SymfonyCasts tutorial and we've had really nice success. However, one part stopped working with symfony/http-client 4.3.4. The problematic change is this one: https://github.com/symfony/symfony/pull/32823/files#diff-1ab846e0995199d7db8d975a9225e818

Basically, this changes the format of the $options['headers'] key in HttpClient::prepareRequest(), which the Client class calls

For example, suppose this request:

class CheeseListingResourceTest extends ApiTestCase
{
    public function testCreateCheeseListing()
    {
        $client = self::createClient();
        $client->request('POST', '/api/cheeses', [
            'headers' => ['Content-Type' => 'application/json'],
            'json' => [],
        ]);
        $this->assertResponseStatusCodeSame(401);
    }
}

Before 4.3.4, $options (if you dump right after https://github.com/api-platform/core/blob/6e9ccf7418bf973d273b125d55ccc521b89afb06/src/Bridge/Symfony/Bundle/Test/Client.php#L90) looks like this:

array:7 [
  "headers" => array:2 [
    "content-type" => array:1 [
      0 => "application/json"
    ]
    "accept" => array:1 [
      0 => "application/ld+json"
    ]
  ]
...

With symfony/http-client 4.3.4, it now looks like:

array:7 [
  "headers" => array:2 [
    0 => "Content-Type: application/json"
    1 => "accept: application/ld+json"
  ]
  "normalized_headers" => array:2 [
    "content-type" => array:1 [
      0 => "Content-Type: application/json"
    ]
    "accept" => array:1 [
      0 => "accept: application/ld+json"
    ]
  ]

This causes the Client class from the new testing tools to incorrectly read those headers in this block: https://github.com/api-platform/core/blob/6e9ccf7418bf973d273b125d55ccc521b89afb06/src/Bridge/Symfony/Bundle/Test/Client.php#L95-L104

It's probably an easy fix - sorry I don't have time at this moment to make a PR. But I hope it will be an easy pick :).

Cheers!

Hacktoberfest bug

Most helpful comment

Thanks for reporting this @weaverryan, we came to the same conclusion with @teohhanhui.
That's why this PR is red: https://github.com/api-platform/core/pull/3038.
It seems it's a BC break from Symfony (if HttpClient is no more experimental): https://symfony.com/doc/current/contributing/code/bc.html#using-our-traits
This method has changed: https://github.com/symfony/http-client/blob/v4.3.4/HttpClientTrait.php#L190

All 4 comments

Thanks for reporting this @weaverryan, we came to the same conclusion with @teohhanhui.
That's why this PR is red: https://github.com/api-platform/core/pull/3038.
It seems it's a BC break from Symfony (if HttpClient is no more experimental): https://symfony.com/doc/current/contributing/code/bc.html#using-our-traits
This method has changed: https://github.com/symfony/http-client/blob/v4.3.4/HttpClientTrait.php#L190

IMO, @experimental should mean "can break in the next minor release", not "can break in the next patch release".

What is Symfony's actual policy on that?

It seems it's a BC break from Symfony (if HttpClient is no more experimental): https://symfony.com/doc/current/contributing/code/bc.html#using-our-traits

It IS still experimental (won't be in 4.4)

IMO, @experimental should mean "can break in the next minor release", not "can break in the next patch release".
What is Symfony's actual policy on that?

BC breaks are allowed in a minor, but not in a patch. I've opened an issue :) https://github.com/symfony/symfony/issues/33450

Fixed by #3038

Was this page helpful?
0 / 5 - 0 ratings