Permalink
Browse files

feature #349 Support setting Guzzle's "base_uri" option (dunglas)

This PR was merged into the 3.2-dev branch.

Discussion
----------

Support setting Guzzle's "base_uri" option

Allow to set a base URI. Fix #328.

Commits
-------

71dc217 Support setting Guzzle's "base_uri" option
  • Loading branch information...
fabpot committed Jun 29, 2018
2 parents 395f61d + 71dc217 commit 3f0eaf0a40181359470651f1565b3e07e3dd31b8
Showing with 65 additions and 3 deletions.
  1. +26 −1 Goutte/Client.php
  2. +39 −2 Goutte/Tests/ClientTest.php
View
@@ -16,6 +16,7 @@
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;
use Symfony\Component\BrowserKit\Client as BaseClient;
use Symfony\Component\BrowserKit\Request;
use Symfony\Component\BrowserKit\Response;
@@ -26,18 +27,42 @@
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Michael Dowling <michael@guzzlephp.org>
* @author Charles Sarrazin <charles@sarraz.in>
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class Client extends BaseClient
{
protected $client;
private $headers = array();
private $auth = null;
private $auth;
public function setClient(GuzzleClientInterface $client)
{
$this->client = $client;
/**
* @var $baseUri UriInterface
*/
if (null !== $this->getServerParameter('HTTP_HOST', null) || null === $baseUri = $client->getConfig('base_uri')) {
return $this;
}
$path = $baseUri->getPath();
if ('' !== $path && '/' !== $path) {
throw new \InvalidArgumentException('Setting a path in the Guzzle "base_uri" config option is not supported by Goutte yet.');
}
if (null === $this->getServerParameter('HTTPS', null) && 'https' === $baseUri->getScheme()) {
$this->setServerParameter('HTTPS', 'on');
}
$host = $baseUri->getHost();
if (null !== $port = $baseUri->getPort()) {
$host .= ":$port";
}
$this->setServerParameter('HTTP_HOST', $host);
return $this;
}
@@ -33,7 +33,7 @@ class ClientTest extends TestCase
/** @var MockHandler */
protected $mock;
protected function getGuzzle(array $responses = [])
protected function getGuzzle(array $responses = [], array $extraConfig = [])
{
if (empty($responses)) {
$responses = [new GuzzleResponse(200, [], '<html><body><p>Hi</p></body></html>')];
@@ -42,7 +42,7 @@ protected function getGuzzle(array $responses = [])
$handlerStack = HandlerStack::create($this->mock);
$this->history = [];
$handlerStack->push(Middleware::history($this->history));
$guzzle = new GuzzleClient(array('redirect.disable' => true, 'base_uri' => '', 'handler' => $handlerStack));
$guzzle = new GuzzleClient(array_merge(array('redirect.disable' => true, 'base_uri' => '', 'handler' => $handlerStack), $extraConfig));
return $guzzle;
}
@@ -407,4 +407,41 @@ public function testRestart()
$this->assertEquals([], $headersReflectionProperty->getValue($client));
$this->assertNull($authReflectionProperty->getValue($client));
}
public function testSetBaseUri()
{
$guzzle = $this->getGuzzle([], ['base_uri' => 'http://example.com/']);
$client = new Client();
$client->setClient($guzzle);
$this->assertNull($client->getServerParameter('HTTPS', null));
$this->assertSame('example.com', $client->getServerParameter('HTTP_HOST'));
$client->request('GET', '/foo');
$this->assertSame('http://example.com/foo', (string) end($this->history)['request']->getUri());
}
public function testSetHttpsBaseUri()
{
$guzzle = $this->getGuzzle([], ['base_uri' => 'https://example.com:1234']);
$client = new Client();
$client->setClient($guzzle);
$this->assertSame('on', $client->getServerParameter('HTTPS'));
$this->assertSame('example.com:1234', $client->getServerParameter('HTTP_HOST'));
$client->request('GET', '/foo');
$this->assertSame('https://example.com:1234/foo', (string) end($this->history)['request']->getUri());
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Setting a path in the Guzzle "base_uri" config option is not supported by Goutte yet.
*/
public function testSetBaseUriWithPath()
{
$guzzle = $this->getGuzzle([], ['base_uri' => 'http://example.com/foo/']);
$client = new Client();
$client->setClient($guzzle);
}
}

0 comments on commit 3f0eaf0

Please sign in to comment.