|
| 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