Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[HttpClient] Fix that staged exceptions were not thrown when callingMockResponse::getStatusCode()#57081
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
mpdude commentedMay 23, 2024
Inviting@fancyweb since you authored the last relevant changes in that area of code and you seem to have a good understanding of what's going on. |
MockResponse::getStatus()MockResponse::getStatus()MockResponse::getStatusCode()nicolas-grekas commentedJun 25, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Did you try setting the "error" info when constructing the MockResponse instead? |
mpdude commentedJun 27, 2024
No, I was not aware of that. I can confirm the following code throws: $httpClient =newMockHttpClient([newMockResponse(info: ['error' =>'network error']), ]);$response =$httpClient->request('GET','/test');$code =$response->getStatusCode();// throws a TransportException So, the documentation athttps://symfony.com/doc/current/http_client.html#testing-network-transport-exceptions is wrong? It suggests to do the following: $httpClient =newMockHttpClient([// You can create the exception directly in the body...newMockResponse([new \RuntimeException('Error at transport level')]),// ... or you can yield the exception from a callbacknewMockResponse((staticfunction ():\Generator {yieldnewTransportException('Error at transport level'); })()), ]); |
nicolas-grekas commentedJun 27, 2024
Those are mocking two different moments where exceptions can be thrown: before headers vs after them; |
nicolas-grekas commentedJun 27, 2024
Doc PR welcome! |
mpdude commentedJun 27, 2024
Docs PR atsymfony/symfony-docs#19997. Please check if what I wrote is correct. |
…hat occur before headers are received (mpdude)This PR was squashed before being merged into the 6.4 branch.Discussion----------[HttpClient] Explain how to mock `TransportExceptions` that occur before headers are receivedThe `error` option value was previously undocumented.Seesymfony/symfony#57081 (comment) for the discussion leading to this.PR based on 6.4 since the section was not yet present in 5.4 docs.Commits-------6fde4e0 [HttpClient] Explain how to mock `TransportExceptions` that occur before headers are received
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
TransportExceptionbeing 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 athttps://symfony.com/doc/current/http_client.html#testing-network-transport-exceptions, and also at the definition of
ExternalArticleService::createArticle()as given inhttps://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
$httpClienteither asor
did not lead to the exception being thrown when accessing
$response->getStatusCode().Digging into
\Symfony\Component\HttpClient\Response\MockResponse::readResponseI 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.