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