-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[HttpClient][DX] Add URL context to JsonException messages
- Loading branch information
1 parent
688baac
commit 65ccb6c
Showing
2 changed files
with
72 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\HttpClient\Tests\Response; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpClient\Exception\JsonException; | ||
use Symfony\Component\HttpClient\Response\MockResponse; | ||
|
||
/** | ||
* Test methods from Symfony\Component\HttpClient\Response\ResponseTrait. | ||
*/ | ||
class MockResponseTest extends TestCase | ||
{ | ||
public function testToArray() | ||
{ | ||
$data = ['color' => 'orange', 'size' => 42]; | ||
$response = new MockResponse(json_encode($data)); | ||
$response = MockResponse::fromRequest('GET', 'https://example.com/file.json', [], $response); | ||
|
||
$this->assertSame($data, $response->toArray()); | ||
} | ||
|
||
/** | ||
* @dataProvider toArrayErrors | ||
*/ | ||
public function testToArrayError($content, $responseHeaders, $message) | ||
{ | ||
$this->expectException(JsonException::class); | ||
$this->expectExceptionMessage($message); | ||
|
||
$response = new MockResponse($content, ['response_headers' => $responseHeaders]); | ||
$response = MockResponse::fromRequest('GET', 'https://example.com/file.json', [], $response); | ||
$response->toArray(); | ||
} | ||
|
||
public function toArrayErrors() | ||
{ | ||
yield [ | ||
'content' => '{}', | ||
'responseHeaders' => ['content-type' => 'plain/text'], | ||
'message' => 'Response content-type is "plain/text" while a JSON-compatible one was expected for "https://example.com/file.json".', | ||
]; | ||
|
||
yield [ | ||
'content' => 'not json', | ||
'responseHeaders' => [], | ||
'message' => 'Syntax error for "https://example.com/file.json".', | ||
]; | ||
|
||
yield [ | ||
'content' => '[1,2}', | ||
'responseHeaders' => [], | ||
'message' => 'State mismatch (invalid or malformed JSON) for "https://example.com/file.json".', | ||
]; | ||
|
||
yield [ | ||
'content' => '"not an array"', | ||
'responseHeaders' => [], | ||
'message' => 'JSON content was expected to decode to an array, string returned for "https://example.com/file.json".', | ||
]; | ||
|
||
yield [ | ||
'content' => '8', | ||
'responseHeaders' => [], | ||
'message' => 'JSON content was expected to decode to an array, integer returned for "https://example.com/file.json".', | ||
]; | ||
} | ||
} |