|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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; |
| 13 | + |
| 14 | +use Psr\Log\LoggerInterface; |
| 15 | +use Symfony\Component\HttpClient\CachingHttpClient; |
| 16 | +use Symfony\Component\HttpClient\Exception\TransportException; |
| 17 | +use Symfony\Component\HttpClient\MockHttpClient; |
| 18 | +use Symfony\Component\HttpClient\NativeHttpClient; |
| 19 | +use Symfony\Component\HttpClient\Response\MockResponse; |
| 20 | +use Symfony\Component\HttpKernel\HttpCache\StoreInterface; |
| 21 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 22 | +use Symfony\Contracts\HttpClient\ResponseInterface; |
| 23 | +use Symfony\Contracts\HttpClient\Test\HttpClientTestCase; |
| 24 | + |
| 25 | +class TestCachingHttpClient extends CachingHttpClient |
| 26 | +{ |
| 27 | + protected $client; |
| 28 | + |
| 29 | + public function __construct(StoreInterface $storeInterface, $testCase) |
| 30 | + { |
| 31 | + parent::__construct(new NativeHttpClient(), $storeInterface); |
| 32 | + |
| 33 | + $headers = [ |
| 34 | + 'Host: localhost:8057', |
| 35 | + 'Content-Type: application/json', |
| 36 | + ]; |
| 37 | + |
| 38 | + $body = '{ |
| 39 | + "SERVER_PROTOCOL": "HTTP/1.1", |
| 40 | + "SERVER_NAME": "127.0.0.1", |
| 41 | + "REQUEST_URI": "/", |
| 42 | + "REQUEST_METHOD": "GET", |
| 43 | + "HTTP_FOO": "baR", |
| 44 | + "HTTP_HOST": "localhost:8057" |
| 45 | + }'; |
| 46 | + |
| 47 | + $this->client = new MockHttpClient(function (string $method, string $url, array $options) use ($headers, $body, $testCase) { |
| 48 | + |
| 49 | + switch ($testCase) { |
| 50 | + default: |
| 51 | + // force the request to be completed so that we don't test side effects of the transport |
| 52 | + $response = $this->request($method, $url, $options); |
| 53 | + $content = $response->getContent(false); |
| 54 | + |
| 55 | + return new MockResponse($content, $response->getInfo()); |
| 56 | + |
| 57 | + case 'testGetRequest': |
| 58 | + array_unshift($headers, 'HTTP/1.1 200 OK'); |
| 59 | + |
| 60 | + if (preg_match('/length-broken$/', $url)){ |
| 61 | + $headers = [ |
| 62 | + 'Host: localhost:8057', |
| 63 | + 'Content-Length: 1000', |
| 64 | + 'Content-Type: application/json', |
| 65 | + ]; |
| 66 | + |
| 67 | + return new MockResponse($body, ['raw_headers' => $headers]); |
| 68 | + } |
| 69 | + |
| 70 | + return new MockResponse($body, ['raw_headers' => $headers]); |
| 71 | +// |
| 72 | +// case 'testDnsError': |
| 73 | +// $mock = $this->getMockBuilder(ResponseInterface::class)->getMock(); |
| 74 | +// $mock->expects($this->any()) |
| 75 | +// ->method('getStatusCode') |
| 76 | +// ->willThrowException(new TransportException('DSN error')); |
| 77 | +// $mock->expects($this->any()) |
| 78 | +// ->method('getInfo') |
| 79 | +// ->willReturn([]); |
| 80 | +// |
| 81 | +// $responses[] = $mock; |
| 82 | +// $responses[] = $mock; |
| 83 | +// break; |
| 84 | +// |
| 85 | +// case 'testBadRequestBody': |
| 86 | +// case 'testOnProgressCancel': |
| 87 | +// case 'testOnProgressError': |
| 88 | +// $responses[] = new MockResponse($body, ['raw_headers' => $headers]); |
| 89 | +// break; |
| 90 | +// |
| 91 | +// case 'testTimeoutOnAccess': |
| 92 | +// $mock = $this->getMockBuilder(ResponseInterface::class)->getMock(); |
| 93 | +// $mock->expects($this->any()) |
| 94 | +// ->method('getHeaders') |
| 95 | +// ->willThrowException(new TransportException('Timeout')); |
| 96 | +// |
| 97 | +// $responses[] = $mock; |
| 98 | +// break; |
| 99 | +// |
| 100 | +// case 'testResolve': |
| 101 | +// $responses[] = new MockResponse($body, ['raw_headers' => $headers]); |
| 102 | +// $responses[] = new MockResponse($body, ['raw_headers' => $headers]); |
| 103 | +// $responses[] = $client->request('GET', 'http://symfony.com:8057/'); |
| 104 | +// break; |
| 105 | +// |
| 106 | +// case 'testTimeoutOnStream': |
| 107 | +// case 'testUncheckedTimeoutThrows': |
| 108 | +// $body = ['<1>', '', '<2>']; |
| 109 | +// $responses[] = new MockResponse($body, ['raw_headers' => $headers]); |
| 110 | +// break; |
| 111 | + } |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + public function request(string $method, string $url, array $options = []): ResponseInterface |
| 116 | + { |
| 117 | + return $this->client->request($method, $url, $options); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +class CachingHttpClientTest extends HttpClientTestCase |
| 122 | +{ |
| 123 | + protected function getHttpClient(string $testCase): HttpClientInterface |
| 124 | + { |
| 125 | + $storeInterface = $this->createMock(StoreInterface::class); |
| 126 | + |
| 127 | + return new TestCachingHttpClient($storeInterface, $testCase); |
| 128 | + } |
| 129 | +} |
0 commit comments