Skip to content

Commit 0e5491e

Browse files
committed
[HttpClient] Add JsonMockResponse
1 parent 80cd9a5 commit 0e5491e

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* Add `UriTemplateHttpClient` to use URI templates as specified in the RFC 6570
88
* Add `ServerSentEvent::getArrayData()` to get the Server-Sent Event's data decoded as an array when it's a JSON payload
99
* Allow array of urls as `base_uri` option value in `RetryableHttpClient` to retry on a new url each time
10+
* Add `JsonMockResponse`, a `MockResponse` shortcut that automatically encodes the passed body to JSON and sets the content type to `application/json` by default
1011

1112
6.2
1213
---

Response/JsonMockResponse.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpClient\Response;
13+
14+
use Symfony\Component\HttpClient\Exception\InvalidArgumentException;
15+
16+
class JsonMockResponse extends MockResponse
17+
{
18+
public function __construct(mixed $body = [], array $info = [])
19+
{
20+
try {
21+
$json = json_encode($body, \JSON_THROW_ON_ERROR);
22+
} catch (\JsonException $e) {
23+
throw new InvalidArgumentException('JSON encoding failed: '.$e->getMessage(), $e->getCode());
24+
}
25+
26+
$info['response_headers']['content-type'] ??= 'application/json';
27+
28+
parent::__construct($json, $info);
29+
}
30+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpClient\Tests\Response;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpClient\Exception\InvalidArgumentException;
16+
use Symfony\Component\HttpClient\MockHttpClient;
17+
use Symfony\Component\HttpClient\Response\JsonMockResponse;
18+
19+
final class JsonMockResponseTest extends TestCase
20+
{
21+
public function testDefaults()
22+
{
23+
$client = new MockHttpClient(new JsonMockResponse());
24+
$response = $client->request('GET', 'https://symfony.com');
25+
26+
$this->assertSame([], $response->toArray());
27+
$this->assertSame('application/json', $response->getHeaders()['content-type'][0]);
28+
}
29+
30+
public function testInvalidBody()
31+
{
32+
$this->expectException(InvalidArgumentException::class);
33+
$this->expectExceptionMessage('JSON encoding failed: Malformed UTF-8 characters, possibly incorrectly encoded');
34+
35+
new JsonMockResponse("\xB1\x31");
36+
}
37+
38+
public function testJsonEncodeArray()
39+
{
40+
$client = new MockHttpClient(new JsonMockResponse([
41+
'foo' => 'bar',
42+
'ccc' => 123,
43+
]));
44+
$response = $client->request('GET', 'https://symfony.com');
45+
46+
$this->assertSame([
47+
'foo' => 'bar',
48+
'ccc' => 123,
49+
], $response->toArray());
50+
$this->assertSame('application/json', $response->getHeaders()['content-type'][0]);
51+
}
52+
53+
public function testJsonEncodeString()
54+
{
55+
$client = new MockHttpClient(new JsonMockResponse('foobarccc'));
56+
$response = $client->request('GET', 'https://symfony.com');
57+
58+
$this->assertSame('"foobarccc"', $response->getContent());
59+
$this->assertSame('application/json', $response->getHeaders()['content-type'][0]);
60+
}
61+
62+
/**
63+
* @dataProvider responseHeadersProvider
64+
*/
65+
public function testResponseHeaders(string $expectedContentType, array $responseHeaders)
66+
{
67+
$client = new MockHttpClient(new JsonMockResponse([
68+
'foo' => 'bar',
69+
], [
70+
'response_headers' => $responseHeaders,
71+
'http_code' => 201,
72+
]));
73+
$response = $client->request('GET', 'https://symfony.com');
74+
75+
$this->assertSame($expectedContentType, $response->getHeaders()['content-type'][0]);
76+
$this->assertSame(201, $response->getStatusCode());
77+
}
78+
79+
public static function responseHeadersProvider(): array
80+
{
81+
return [
82+
['application/json', []],
83+
['application/json', ['x-foo' => 'ccc']],
84+
['application/problem+json', ['content-type' => 'application/problem+json']],
85+
['application/problem+json', ['x-foo' => 'ccc', 'content-type' => 'application/problem+json']],
86+
];
87+
}
88+
}

0 commit comments

Comments
 (0)