feature #30843 [HttpClient] Add ScopingHttpClient::forBaseUri() + tweak MockHttpClient (nicolas-grekas)

This PR was merged into the 4.3-dev branch.

Discussion
----------

[HttpClient] Add ScopingHttpClient::forBaseUri() + tweak MockHttpClient

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

This allows creating scoped HTTP clients in one line:

```php
$client = ScopingHttpClient::forBaseUri($client, 'http://example.com');
```

`$client` now resolves relative URLs using the provided base URI.

If one also adds default options as 3rd argument, these will be applied conditionally when a URL matching the base URI is requested.

This PR also tweaks `MockHttpClient` to make it return `MockResponse` on its own when no constructor argument is provided, easing tests a bit.

Commits
-------

2b9b8e5707 [HttpClient] Add ScopingHttpClient::forBaseUri() + tweak MockHttpClient
This commit is contained in:
Fabien Potencier 2019-04-03 12:40:25 +02:00
commit 8da76862fa
4 changed files with 38 additions and 16 deletions

View File

@ -31,16 +31,16 @@ class MockHttpClient implements HttpClientInterface
private $baseUri;
/**
* @param callable|ResponseInterface|ResponseInterface[]|iterable $responseFactory
* @param callable|ResponseInterface|ResponseInterface[]|iterable|null $responseFactory
*/
public function __construct($responseFactory, string $baseUri = null)
public function __construct($responseFactory = null, string $baseUri = null)
{
if ($responseFactory instanceof ResponseInterface) {
$responseFactory = [$responseFactory];
}
if (!\is_callable($responseFactory) && !$responseFactory instanceof \Iterator) {
$responseFactory = (function () use ($responseFactory) {
if (null !== $responseFactory && !\is_callable($responseFactory) && !$responseFactory instanceof \Iterator) {
$responseFactory = (static function () use ($responseFactory) {
yield from $responseFactory;
})();
}
@ -57,7 +57,9 @@ class MockHttpClient implements HttpClientInterface
[$url, $options] = $this->prepareRequest($method, $url, $options, ['base_uri' => $this->baseUri], true);
$url = implode('', $url);
if (\is_callable($this->responseFactory)) {
if (null === $this->responseFactory) {
$response = new MockResponse();
} elseif (\is_callable($this->responseFactory)) {
$response = ($this->responseFactory)($method, $url, $options);
} elseif (!$this->responseFactory->valid()) {
throw new TransportException('The response factory iterator passed to MockHttpClient is empty.');

View File

@ -111,6 +111,10 @@ class MockResponse implements ResponseInterface
$response->info['user_data'] = $options['user_data'] ?? null;
$response->info['url'] = $url;
if ($mock instanceof self) {
$mock->requestOptions = $response->requestOptions;
}
self::writeRequest($response, $options, $mock);
$response->body[] = [$options, $mock];

View File

@ -38,6 +38,17 @@ class ScopingHttpClient implements HttpClientInterface
$this->defaultRegexp = $defaultRegexp;
}
public static function forBaseUri(HttpClientInterface $client, string $baseUri, array $defaultOptions = [], $regexp = null): self
{
if (null === $regexp) {
$regexp = preg_quote(implode('', self::resolveUrl(self::parseUrl('.'), self::parseUrl($baseUri))));
}
$defaultOptions['base_uri'] = $baseUri;
return new self($client, [$regexp => $defaultOptions], $regexp);
}
/**
* {@inheritdoc}
*/

View File

@ -14,14 +14,13 @@ namespace Symfony\Component\HttpClient\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\Exception\InvalidArgumentException;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\HttpClient\ScopingHttpClient;
class ScopingHttpClientTest extends TestCase
{
public function testRelativeUrl()
{
$mockClient = new MockHttpClient([]);
$mockClient = new MockHttpClient();
$client = new ScopingHttpClient($mockClient, []);
$this->expectException(InvalidArgumentException::class);
@ -30,7 +29,7 @@ class ScopingHttpClientTest extends TestCase
public function testRelativeUrlWithDefaultRegexp()
{
$mockClient = new MockHttpClient(new MockResponse());
$mockClient = new MockHttpClient();
$client = new ScopingHttpClient($mockClient, ['.*' => ['base_uri' => 'http://example.com']], '.*');
$this->assertSame('http://example.com/foo', $client->request('GET', '/foo')->getInfo('url'));
@ -41,7 +40,7 @@ class ScopingHttpClientTest extends TestCase
*/
public function testMatchingUrls(string $regexp, string $url, array $options)
{
$mockClient = new MockHttpClient(new MockResponse());
$mockClient = new MockHttpClient();
$client = new ScopingHttpClient($mockClient, $options);
$response = $client->request('GET', $url);
@ -69,13 +68,7 @@ class ScopingHttpClientTest extends TestCase
'.*' => ['headers' => ['content-type' => 'text/html']],
];
$mockResponses = [
new MockResponse(),
new MockResponse(),
new MockResponse(),
];
$mockClient = new MockHttpClient($mockResponses);
$mockClient = new MockHttpClient();
$client = new ScopingHttpClient($mockClient, $defaultOptions);
$response = $client->request('GET', 'http://example.com/foo-bar', ['json' => ['url' => 'http://example.com']]);
@ -93,4 +86,16 @@ class ScopingHttpClientTest extends TestCase
$this->assertEquals($requestOptions['headers']['x-app'][0], 'unit-test');
$this->assertEquals($requestOptions['headers']['content-type'][0], 'text/html');
}
public function testForBaseUri()
{
$client = ScopingHttpClient::forBaseUri(new MockHttpClient(), 'http://example.com/foo');
$response = $client->request('GET', '/bar');
$this->assertSame('http://example.com/foo', implode('', $response->getRequestOptions()['base_uri']));
$this->assertSame('http://example.com/bar', $response->getInfo('url'));
$response = $client->request('GET', 'http://foo.bar/');
$this->assertNull($response->getRequestOptions()['base_uri']);
}
}