[HttpClient] Fix that staged exceptions were not thrown when calling MockResponse::getStatusCode()
#57081
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Disclaimer: I don't fully grasp all the internal workings and the async tricks the HTTP Client does, so take this with a grain of salt.
I have application code like the following:
To my understanding, conditions like e. g. a DNS resolution error will lead to a
TransportException
being thrown, and this may happen fromgetStatusCode()
, since this is the first method that really has to wait for results to arrive over the network.I'd like to write a test to make sure my code deals with the exception in an appropriate way. I have been following the examples given at https://symfony.com/doc/current/http_client.html#testing-network-transport-exceptions, and also at the definition of
ExternalArticleService::createArticle()
as given in https://symfony.com/doc/current/http_client.html#full-example. Note thatcreateArticle()
also accessesgetStatusCode()
, like my code does.However, I could not get this test to work. Defining
$httpClient
either asor
did not lead to the exception being thrown when accessing
$response->getStatusCode()
.Digging into
\Symfony\Component\HttpClient\Response\MockResponse::readResponse
I noticed that the canned exception was thrown only to be caught again a few lines down, seemingly using it as a plain body part. I do not fully understand what the code is trying to do there. Maybe the code is the result of #44438 and #44568 that were merged in close timely relationship, but against different target branches (4.4 and 6.1).I have added new tests to cover exception handling also when accessing the
getStatusCode()
method, with exceptions being yielded from a generator or being part of an array.Also, I split up some test cases where – to my understanding – two different things were tested at the same time.