|
| 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; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\HttpClient\ConditionalHttpClient; |
| 16 | +use Symfony\Component\HttpClient\Exception\InvalidArgumentException; |
| 17 | +use Symfony\Component\HttpClient\MockHttpClient; |
| 18 | +use Symfony\Component\HttpClient\Response\MockResponse; |
| 19 | + |
| 20 | +class ConditionalHttpClientTest extends TestCase |
| 21 | +{ |
| 22 | + public function testInvalidUrl() |
| 23 | + { |
| 24 | + $defaultOptions = [ |
| 25 | + '#^/foo-bar$#' => [ |
| 26 | + 'headers' => ['Content-Type' => 'application/json'], |
| 27 | + ], |
| 28 | + '#^.*$#' => [ |
| 29 | + 'headers' => ['Content-Type' => 'application/json'], |
| 30 | + ], |
| 31 | + ]; |
| 32 | + |
| 33 | + $mockClient = new MockHttpClient(null); |
| 34 | + $client = new ConditionalHttpClient($mockClient, $defaultOptions); |
| 35 | + |
| 36 | + $this->expectException(InvalidArgumentException::class); |
| 37 | + $client->request('GET', '/foo'); |
| 38 | + } |
| 39 | + |
| 40 | + public function getUrlsAndDefaultOptions() |
| 41 | + { |
| 42 | + $defaultOptions = [ |
| 43 | + '#^.*/foo-bar$#' => [ |
| 44 | + 'headers' => ['content-type' => 'application/json'], |
| 45 | + ], |
| 46 | + '#^.*$#' => [ |
| 47 | + 'headers' => ['content-type' => 'text/html'], |
| 48 | + ], |
| 49 | + ]; |
| 50 | + |
| 51 | + yield ['regexp' => '#^.*/foo-bar$#', 'url' => 'http://example.com/foo-bar', 'default_options' => $defaultOptions]; |
| 52 | + yield ['regexp' => '#^.*$#', 'url' => 'http://example.com/bar-foo', 'default_options' => $defaultOptions]; |
| 53 | + yield ['regexp' => '#^.*$#', 'url' => 'http://example.com/foobar', 'default_options' => $defaultOptions]; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @dataProvider getUrlsAndDefaultOptions |
| 58 | + */ |
| 59 | + public function testMatchingUrls(string $regexp, string $url, array $options) |
| 60 | + { |
| 61 | + $mockClient = new MockHttpClient(new MockResponse($url, $options[$regexp]), 'http://example.com'); |
| 62 | + $client = new ConditionalHttpClient($mockClient, $options); |
| 63 | + |
| 64 | + $response = $client->request('GET', $url); |
| 65 | + $responseInfo = $response->getInfo('headers'); |
| 66 | + |
| 67 | + $this->assertEquals([], array_diff_assoc($responseInfo, $options[$regexp]['headers'])); |
| 68 | + } |
| 69 | + |
| 70 | + public function testMatchingUrlsAndOptions() |
| 71 | + { |
| 72 | + $defaultOptions = [ |
| 73 | + '#^/foo-bar$#' => ['headers' => ['x-app' => 'unit-test-foo-bar']], |
| 74 | + '#^.*$#' => ['headers' => ['content-type' => 'text/html']], |
| 75 | + ]; |
| 76 | + |
| 77 | + $mockResponses = [ |
| 78 | + new MockResponse('http://example.com/foo-bar', $defaultOptions['#^/foo-bar$#']), |
| 79 | + new MockResponse('http://example.com/bar-foo', $defaultOptions['#^.*$#']), |
| 80 | + new MockResponse('http://example.com/', $defaultOptions['#^.*$#']), |
| 81 | + ]; |
| 82 | + |
| 83 | + $mockClient = new MockHttpClient($mockResponses, 'http://example.com'); |
| 84 | + $client = new ConditionalHttpClient($mockClient, $defaultOptions); |
| 85 | + |
| 86 | + $response = $client->request('GET', 'http://example.com/foo-bar', ['json' => ['url' => 'http://example.com']]); |
| 87 | + $requestOptions = $response->getRequestOptions(); |
| 88 | + $responseInfos = $response->getInfo('headers'); |
| 89 | + $this->assertEquals($requestOptions['json']['url'], 'http://example.com'); |
| 90 | + $this->assertEquals($responseInfos['x-app'], $defaultOptions['#^/foo-bar$#']['headers']['x-app']); |
| 91 | + |
| 92 | + $response = $client->request('GET', 'http://example.com/bar-foo', ['headers' => ['x-app' => 'unit-test']]); |
| 93 | + $requestOptions = $response->getRequestOptions(); |
| 94 | + $responseInfos = $response->getInfo('headers'); |
| 95 | + $this->assertEquals($requestOptions['headers']['x-app'][0], 'unit-test'); |
| 96 | + $this->assertEquals($responseInfos['content-type'], 'text/html'); |
| 97 | + |
| 98 | + $response = $client->request('GET', 'http://example.com/bar-foo', ['headers' => ['x-app' => 'unit-test']]); |
| 99 | + $requestOptions = $response->getRequestOptions(); |
| 100 | + $responseInfos = $response->getInfo('headers'); |
| 101 | + $this->assertEquals($requestOptions['headers']['x-app'][0], 'unit-test'); |
| 102 | + $this->assertEquals($responseInfos['content-type'], 'text/html'); |
| 103 | + } |
| 104 | +} |
0 commit comments