Skip to content
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
35 changes: 20 additions & 15 deletions app/code/Magento/PageCache/Model/Cache/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
*/
namespace Magento\PageCache\Model\Cache;

use Zend\Uri\Uri;
use Magento\Framework\UrlInterface;
use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\Config\ConfigOptionsListConstants;
use Magento\Framework\App\RequestInterface;
use Zend\Uri\Uri;
use Zend\Uri\UriFactory;

class Server
{
/**
* @var \Magento\Framework\UrlInterface
* @var UrlInterface
*/
protected $urlBuilder;

Expand All @@ -33,12 +34,12 @@ class Server
/**
* Constructor
*
* @param \Magento\Framework\UrlInterface $urlBuilder
* @param UrlInterface $urlBuilder
* @param DeploymentConfig $config
* @param RequestInterface $request
*/
public function __construct(
\Magento\Framework\UrlInterface $urlBuilder,
UrlInterface $urlBuilder,
DeploymentConfig $config,
RequestInterface $request
) {
Expand All @@ -56,21 +57,25 @@ public function getUris()
{
$servers = [];
$configuredHosts = $this->config->get(ConfigOptionsListConstants::CONFIG_PATH_CACHE_HOSTS);
if (null == $configuredHosts) {
$httpHost = $this->request->getHttpHost();
$servers[] = $httpHost ?
UriFactory::factory('')->setHost($httpHost)->setPort(self::DEFAULT_PORT)->setScheme('http') :
UriFactory::factory($this->urlBuilder->getUrl('*', ['_nosid' => true])) // Don't use SID in building URL
->setScheme('http')
->setPath(null)
->setQuery(null);

} else {
if (is_array($configuredHosts)) {
foreach ($configuredHosts as $host) {
$servers[] = UriFactory::factory('')->setHost($host['host'])
$servers[] = UriFactory::factory('')
->setHost($host['host'])
->setPort(isset($host['port']) ? $host['port'] : self::DEFAULT_PORT)
->setScheme('http');
;
}
} elseif ($this->request->getHttpHost()) {
$servers[] = UriFactory::factory('')->setHost($this->request->getHttpHost())->setPort(self::DEFAULT_PORT);
} else {
$servers[] = UriFactory::factory($this->urlBuilder->getUrl('*', ['_nosid' => true]));
}

foreach ($servers as $key => $value) {
$servers[$key]->setScheme('http')
->setPath('/')
->setQuery(null)
;
}
return $servers;
}
Expand Down
35 changes: 18 additions & 17 deletions app/code/Magento/PageCache/Test/Unit/Model/Cache/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
*/
namespace Magento\PageCache\Test\Unit\Model\Cache;

use \Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use \Magento\PageCache\Model\Cache\Server;
use \Zend\Uri\UriFactory;

class ServerTest extends \PHPUnit_Framework_TestCase
{
/** @var \Magento\PageCache\Model\Cache\Server */
/** @var Server */
protected $model;

/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\DeploymentConfig */
Expand All @@ -30,7 +32,7 @@ public function setUp()
->disableOriginalConstructor()
->getMock();

$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$objectManager = new ObjectManager($this);
$this->model = $objectManager->getObject(
'Magento\PageCache\Model\Cache\Server',
[
Expand All @@ -56,12 +58,9 @@ public function testGetUris(
$url,
$hostConfig = null
) {
$this->configMock->expects($this->once())
->method('get')
->willReturn($hostConfig);
$this->requestMock->expects($this->exactly($getHttpHostCallCtr))
->method('getHttpHost')
->willReturn($httpHost);
$this->configMock->expects($this->once())->method('get')->willReturn($hostConfig);
$this->requestMock->expects($this->exactly($getHttpHostCallCtr))->method('getHttpHost')->willReturn($httpHost);

$this->urlBuilderMock->expects($this->exactly($getUrlCallCtr))
->method('getUrl')
->with('*', ['_nosid' => true])
Expand All @@ -70,30 +69,32 @@ public function testGetUris(
$uris = [];
if (null === $hostConfig) {
if (!empty($httpHost)) {
$uris[] = UriFactory::factory('')->setHost($httpHost)
->setPort(\Magento\PageCache\Model\Cache\Server::DEFAULT_PORT)
->setScheme('http');
$uris[] = UriFactory::factory('')->setHost($httpHost)->setPort(Server::DEFAULT_PORT);
}
if (!empty($url)) {
$uris[] = UriFactory::factory($url);
}
} else {
foreach ($hostConfig as $host) {
$port = isset($host['port']) ? $host['port'] : \Magento\PageCache\Model\Cache\Server::DEFAULT_PORT;
$uris[] = UriFactory::factory('')->setHost($host['host'])
->setPort($port)
->setScheme('http');
$port = isset($host['port']) ? $host['port'] : Server::DEFAULT_PORT;
$uris[] = UriFactory::factory('')->setHost($host['host'])->setPort($port);
}
}

foreach ($uris as $key => $value) {
$uris[$key]->setScheme('http')
->setPath('/')
->setQuery(null);
}

$this->assertEquals($uris, $this->model->getUris());
}

public function getUrisDataProvider()
{
return [
'http host' => [1, '127.0.0.1', 0, '',],
'url' => [1, '', 1, 'http://host',],
'http host' => [2, '127.0.0.1', 0, ''],
'url' => [1, '', 1, 'http://host'],
'config' => [
0,
'',
Expand Down