Skip to content

Commit

Permalink
Improve base_url explanation
Browse files Browse the repository at this point in the history
Rename to getHttpClient()

* Rename getClient() to getHttpClient()
* Make convenience methods protected
* Remove getProxyClient() method from ProxyTestCase
* Prefix base_url with "http://" so we can use it safely for the HTTP client
* Update docs

Improve type hints

Remove leading whitespace
  • Loading branch information
ddeboer committed Aug 9, 2014
1 parent cb142b3 commit a3f47ad
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 39 deletions.
31 changes: 22 additions & 9 deletions DependencyInjection/FOSHttpCacheExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,16 @@ private function loadVarnish(ContainerBuilder $container, XmlFileLoader $loader,
{
$loader->load('varnish.xml');
foreach ($config['servers'] as $url) {
$this->validateUrl($url, 'Not a valid varnish server address: "%s"');
$this->validateUrl($url, 'Not a valid Varnish server address: "%s"');
}
if (!empty($config['base_url'])) {
$this->validateUrl($config['base_url'], 'Not a valid base path: "%s"');
$baseUrl = $this->prefixSchema($config['base_url'], 'Not a valid base path: "%s"');
$this->validateUrl($baseUrl, 'Not a valid base path: "%s"');
} else {
$baseUrl = null;
}
$container->setParameter($this->getAlias() . '.proxy_client.varnish.servers', $config['servers']);
$container->setParameter($this->getAlias() . '.proxy_client.varnish.base_url', $config['base_url']);
$container->setParameter($this->getAlias() . '.proxy_client.varnish.base_url', $baseUrl);
if ($config['guzzle_client']) {
$container->getDefinition($this->getAlias() . '.proxy_client.varnish')
->addArgument(
Expand All @@ -237,13 +240,15 @@ private function loadNginx(ContainerBuilder $container, XmlFileLoader $loader, a
{
$loader->load('nginx.xml');
foreach ($config['servers'] as $url) {
$this->validateUrl($url, 'Not a valid nginx server address: "%s"');
$this->validateUrl($url, 'Not a valid Nginx server address: "%s"');
}
if (!empty($config['base_url'])) {
$this->validateUrl($config['base_url'], 'Not a valid base path: "%s"');
$baseUrl = $this->prefixSchema($config['base_url'], 'Not a valid base path: "%s"');
} else {
$baseUrl = null;
}
$container->setParameter($this->getAlias() . '.proxy_client.nginx.servers', $config['servers']);
$container->setParameter($this->getAlias() . '.proxy_client.nginx.base_url', $config['base_url']);
$container->setParameter($this->getAlias() . '.proxy_client.nginx.base_url', $baseUrl);
$container->setParameter($this->getAlias() . '.proxy_client.nginx.purge_location', $config['purge_location']);
}

Expand Down Expand Up @@ -341,14 +346,22 @@ private function loadInvalidatorRules(ContainerBuilder $container, array $config
}

private function validateUrl($url, $msg)
{
$prefixed = $this->prefixSchema($url);

if (!$parts = parse_url($prefixed)) {
throw new InvalidConfigurationException(sprintf($msg, $url));
}
}


private function prefixSchema($url)
{
if (false === strpos($url, '://')) {
$url = sprintf('%s://%s', 'http', $url);
}

if (!$parts = parse_url($url)) {
throw new InvalidConfigurationException(sprintf($msg, $url));
}
return $url;
}

private function getDefault(array $config)
Expand Down
2 changes: 1 addition & 1 deletion Resources/doc/features/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ methods for cache testing::
Test Client
^^^^^^^^^^^

The ``getResponse()`` method calls ``getClient()`` to retrieve a test client. You
The ``getResponse()`` method calls ``getHttpClient()`` to retrieve a test client. You
can use this client yourself to customise the requests. Note that the test
client must be :doc:`enabled in your configuration </reference/configuration/test>`.
By default, it is enabled when you access your application in debug mode and
Expand Down
14 changes: 11 additions & 3 deletions Resources/doc/reference/configuration/proxy-client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,17 @@ base_url

**type**: ``string``

This must match the web host name clients are using when connecting
to the caching proxy. Optionally can contain a base path to your
application. Used for invalidation with paths.
The hostname (or base URL) where users access your web application. The base
URL may contain a path. If you access your web application on a port other than
80, include that port:

.. code-block:: yaml
# app/config/config.yml
fos_http_cache:
proxy_client:
varnish:
base_url: yourwebsite.com:8000
.. warning::

Expand Down
37 changes: 18 additions & 19 deletions Test/ProxyTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use FOS\HttpCache\Test\PHPUnit\IsCacheHitConstraint;
use FOS\HttpCache\Test\PHPUnit\IsCacheMissConstraint;
use FOS\HttpCache\Test\ProxyTestCaseInterface;
use FOS\HttpCache\Test\Proxy\ProxyInterface;
use Guzzle\Http\ClientInterface;
use Guzzle\Http\Message\Response;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Base class that you can extend to run integration tests against a live
Expand Down Expand Up @@ -57,21 +59,28 @@ public static function isCacheMiss()
}

/**
* Get test client
* Get HTTP test client for making requests to your application through a
* live caching proxy
*
* @return \Guzzle\Http\Client
* @return ClientInterface
*/
public function getClient()
protected function getHttpClient()
{
return static::getContainer()->get('fos_http_cache.test.default_client');
}

/**
* {@inheritdoc}
* Get a response from your application through a live caching proxy
*
* @param string $url Request URL (absolute or relative)
* @param array $headers Request HTTP headers
* @param array $options Request options
*
* @return Response
*/
public function getResponse($url, array $headers = array(), $options = array())
protected function getResponse($url, array $headers = array(), $options = array())
{
return $this->getClient()->get($url, $headers, $options)->send();
return $this->getHttpClient()->get($url, $headers, $options)->send();
}

/**
Expand All @@ -94,7 +103,7 @@ protected function setUp()
/**
* Get proxy server
*
* @return \FOS\HttpCache\Test\Proxy\ProxyInterface
* @return ProxyInterface
*
* @throws \RuntimeException If proxy server is not configured
*/
Expand All @@ -109,16 +118,6 @@ protected function getProxy()
return static::getContainer()->get('fos_http_cache.test.default_proxy_server');
}

/**
* Get default caching proxy client
*
* @return \FOS\HttpCache\ProxyClient\ProxyClientInterface
*/
protected function getProxyClient()
{
return static::getContainer()->get('fos_http_cache.default_proxy_client');
}

/**
* Get HTTP header that shows whether the response was a cache hit or miss
*
Expand All @@ -132,7 +131,7 @@ protected static function getCacheDebugHeader()
/**
* Get container
*
* @return \Symfony\Component\DependencyInjection\ContainerInterface
* @return ContainerInterface
*/
protected static function getContainer()
{
Expand Down
2 changes: 1 addition & 1 deletion Tests/Functional/Fixtures/app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fos_http_cache:
proxy_client:
varnish:
servers: 127.0.0.1
base_url: http://localhost:8080
base_url: localhost:8080
tags:
rules:
-
Expand Down
11 changes: 5 additions & 6 deletions Tests/Functional/Test/ProxyTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace FOS\HttpCacheBundle\Tests\Functional\Test;

use FOS\HttpCacheBundle\Test\ProxyTestCase;
use Symfony\Bundle\FrameworkBundle\Tests\Functional\WebTestCase;

class ProxyTestCaseTest extends ProxyTestCase
{
Expand All @@ -26,12 +25,12 @@ protected function setUp()
parent::setUp();
}

public function testGetProxyClient()
public function testGetHttpClient()
{
$this->assertInstanceOf(
'\FOS\HttpCache\ProxyClient\ProxyClientInterface',
$this->getProxyClient()
);
$client = $this->getHttpClient();

$this->assertInstanceOf('\Guzzle\Http\Client', $client);
$this->assertEquals('http://localhost:8080', $client->getBaseUrl());
}

public function testAssertHit()
Expand Down

0 comments on commit a3f47ad

Please sign in to comment.