1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpClient\Tests\Response;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\Exception\InvalidArgumentException;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\JsonMockResponse;
final class JsonMockResponseTest extends TestCase
{
public function testDefaults()
{
$client = new MockHttpClient(new JsonMockResponse());
$response = $client->request('GET', 'https://symfony.com');
$this->assertSame([], $response->toArray());
$this->assertSame('application/json', $response->getHeaders()['content-type'][0]);
}
public function testInvalidBody()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('JSON encoding failed: Malformed UTF-8 characters, possibly incorrectly encoded');
new JsonMockResponse("\xB1\x31");
}
public function testJsonEncodeArray()
{
$client = new MockHttpClient(new JsonMockResponse([
'foo' => 'bar',
'ccc' => 123,
]));
$response = $client->request('GET', 'https://symfony.com');
$this->assertSame([
'foo' => 'bar',
'ccc' => 123,
], $response->toArray());
$this->assertSame('application/json', $response->getHeaders()['content-type'][0]);
}
public function testJsonEncodeString()
{
$client = new MockHttpClient(new JsonMockResponse('foobarccc'));
$response = $client->request('GET', 'https://symfony.com');
$this->assertSame('"foobarccc"', $response->getContent());
$this->assertSame('application/json', $response->getHeaders()['content-type'][0]);
}
public function testJsonEncodeFloat()
{
$client = new MockHttpClient(new JsonMockResponse([
'foo' => 1.23,
'ccc' => 1.0,
'baz' => 10.,
]));
$response = $client->request('GET', 'https://symfony.com');
$this->assertSame([
'foo' => 1.23,
'ccc' => 1.,
'baz' => 10.,
], $response->toArray());
}
#[\PHPUnit\Framework\Attributes\DataProvider('responseHeadersProvider')]
public function testResponseHeaders(string $expectedContentType, array $responseHeaders)
{
$client = new MockHttpClient(new JsonMockResponse([
'foo' => 'bar',
], [
'response_headers' => $responseHeaders,
'http_code' => 201,
]));
$response = $client->request('GET', 'https://symfony.com');
$this->assertSame($expectedContentType, $response->getHeaders()['content-type'][0]);
$this->assertSame(201, $response->getStatusCode());
}
public static function responseHeadersProvider(): array
{
return [
['application/json', []],
['application/json', ['x-foo' => 'ccc']],
['application/problem+json', ['content-type' => 'application/problem+json']],
['application/problem+json', ['x-foo' => 'ccc', 'content-type' => 'application/problem+json']],
];
}
public function testFromFile()
{
$client = new MockHttpClient(JsonMockResponse::fromFile(__DIR__.'/Fixtures/response.json'));
$response = $client->request('GET', 'https://symfony.com');
$this->assertSame([
'foo' => 'bar',
], $response->toArray());
$this->assertSame('application/json', $response->getHeaders()['content-type'][0]);
}
public function testFromFileWithInvalidJson()
{
$path = __DIR__.'/Fixtures/invalid_json.json';
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(\sprintf('File "%s" does not contain valid JSON.', $path));
JsonMockResponse::fromFile($path);
}
}
|