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

Skip to content

Commit 897b21d

Browse files
committed
[HttpClient] Throw JsonException instead of TransportException on empty response in Response::toArray()
1 parent 4368848 commit 897b21d

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/Symfony/Component/HttpClient/Response/ResponseTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public function getContent(bool $throw = true): string
132132
public function toArray(bool $throw = true): array
133133
{
134134
if ('' === $content = $this->getContent($throw)) {
135-
throw new TransportException('Response body is empty.');
135+
throw new JsonException('Response body is empty.');
136136
}
137137

138138
if (null !== $this->jsonData) {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Symfony\Component\HttpClient\Tests\Response\Response;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Symfony\Component\HttpClient\Exception\JsonException;
7+
use Symfony\Component\HttpClient\Response\MockResponse;
8+
9+
/**
10+
* Test methods from Symfony\Component\HttpClient\Response\ResponseTrait.
11+
*/
12+
class MockResponseTest extends TestCase
13+
{
14+
/**
15+
* @dataProvider toArrayErrors
16+
*/
17+
public function testToArrayError($content, $responseHeaders, $message)
18+
{
19+
$this->expectException(JsonException::class);
20+
$this->expectExceptionMessage($message);
21+
22+
$response = new MockResponse($content, ['response_headers' => $responseHeaders]);
23+
$response = MockResponse::fromRequest('GET', 'https://example.com/file.json', [], $response);
24+
$response->toArray();
25+
}
26+
27+
public function toArrayErrors()
28+
{
29+
yield [
30+
'content' => '',
31+
'responseHeaders' => [],
32+
'message' => 'Response body is empty.',
33+
];
34+
}
35+
}

0 commit comments

Comments
 (0)