Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/Symfony/Component/HttpClient/MockHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
})();
}
Expand All @@ -57,7 +57,9 @@ public function request(string $method, string $url, array $options = []): Respo
[$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.');
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/HttpClient/Response/MockResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ public static function fromRequest(string $method, string $url, array $options,
$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];

Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Component/HttpClient/ScopingHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ public function __construct(HttpClientInterface $client, array $defaultOptionsBy
$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}
*/
Expand Down
27 changes: 16 additions & 11 deletions src/Symfony/Component/HttpClient/Tests/ScopingHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@
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);
Expand All @@ -30,7 +29,7 @@ public function testRelativeUrl()

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'));
Expand All @@ -41,7 +40,7 @@ public function testRelativeUrlWithDefaultRegexp()
*/
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);
Expand Down Expand Up @@ -69,13 +68,7 @@ public function testMatchingUrlsAndOptions()
'.*' => ['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']]);
Expand All @@ -93,4 +86,16 @@ public function testMatchingUrlsAndOptions()
$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']);
}
}