Skip to content

[HttpClient] Behaviour change when mocking HttpClient #62693

@adpeyre

Description

@adpeyre

Symfony version(s) affected

7.4.1

Description

Hello,

Since upgrading to 7.4.1, I've been encountering an issue when mocking a scoped HttpClient.
It looks like my mock no longer works.

What could be explain that ?
Ty

How to reproduce

I've this configuration for other tests but it's not used in this test.

when@test:
    framework:
        http_client:
            mock_response_factory: App\Mocks\Api\HttpClientMockResponseFactory

My scoped http client :

        scoped_clients:
            middleware.api.client:
                base_uri: '%env(MIDDLEWARE_API_BASE_URI)%'

My test :

$client = new MockHttpClient([
    new MockResponse('{}', ['http_code' => 404]),
]);

$container = self::getContainer();
$container->set('middleware.api.client', $client);

$this->getClient()->request('GET', '/zone/example.com/');

Service middleware.api.client before :

App\Dns\Http\AcceptLanguageHttpClientDecorator^ {#207
  -client: Symfony\Component\HttpClient\TraceableHttpClient^ {#215
    -tracedRequests: ArrayObject {#217
      storage: []
      flag::STD_PROP_LIST: false
      flag::ARRAY_AS_PROPS: false
      iteratorClass: "ArrayIterator"
    }
    -client: Symfony\Component\HttpClient\ScopingHttpClient^ {#222
      -client: Symfony\Component\HttpClient\UriTemplateHttpClient^ {#209
        -expander: null
        -defaultVars: []
        -client: Symfony\Component\HttpClient\MockHttpClient^ {#781
          -responseFactory: Generator {#204
            trace: {
              ./vendor/symfony/http-client/MockHttpClient.php:54 { …}
              Symfony\Component\HttpClient\MockHttpClient::Symfony\Component\HttpClient\{closure}() {}
            }
            closed: false
          }

Service middleware.api.client after:

App\Dns\Http\AcceptLanguageHttpClientDecorator^ {#207
  -client: Symfony\Component\HttpClient\ScopingHttpClient^ {#784
    -client: Symfony\Component\HttpClient\UriTemplateHttpClient^ {#215
      -expander: null
      -defaultVars: []
      -client: Symfony\Component\HttpClient\TraceableHttpClient^ {#209
        -tracedRequests: ArrayObject {#782
          storage: []
          flag::STD_PROP_LIST: false
          flag::ARRAY_AS_PROPS: false
          iteratorClass: "ArrayIterator"
        }
        -client: Sentry\SentryBundle\Tracing\HttpClient\TraceableHttpClientForV6^ {#222
          #client: Symfony\Component\HttpClient\MockHttpClient^ {#214
            -responseFactory: App\Mocks\Api\HttpClientMockResponseFactory::__invoke(string $method, string $url, array $options = []): ResponseInterface^ {#206
              returnType: "Symfony\Contracts\HttpClient\ResponseInterface"
              this: App\Mocks\Api\HttpClientMockResponseFactory {#211 …}
            }

Possible Solution

Reduce the TraceableHttpClient decorator priority fix the issue.

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions