Skip to content
New issue

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

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

Already on GitHub? # to your account

Fix Adding X-Invalidate-Token header only when TOKEN is not null #133

Merged
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
92 changes: 92 additions & 0 deletions spec/ProxyClient/VarnishSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace spec\EzSystems\PlatformHttpCacheBundle\ProxyClient;

use eZ\Publish\Core\MVC\ConfigResolverInterface;
use FOS\HttpCache\ProxyClient\Dispatcher;
use Http\Message\RequestFactory;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Psr\Http\Message\RequestInterface;

class VarnishSpec extends ObjectBehavior
{
private const URI = "/";
private const REQUEST_HEADERS = [
"X-Some-Header" => "__SOME_HEADER_VALUE__"
];

public function let(
ConfigResolverInterface $configResolver,
Dispatcher $httpDispatcher,
RequestFactory $messageFactory,
RequestInterface $request

) {
$messageFactory->createRequest(
Argument::any(),
Argument::any(),
Argument::any(),
Argument::any()
)->willReturn($request);

$this->beConstructedWith($configResolver, $httpDispatcher, [], $messageFactory);
}

public function it_should_purge_with_additional_token_header_when_configuration_key_with_token_is_not_null(
ConfigResolverInterface $configResolver,
RequestFactory $messageFactory
) {
$configResolver->hasParameter('http_cache.varnish_invalidate_token')->willReturn(true);
$configResolver->getParameter('http_cache.varnish_invalidate_token')->willReturn('__TOKEN__');

$this->purge(self::URI, self::REQUEST_HEADERS);

$this->requestShouldHaveBeenCreatedWithHeaders(
array_merge(self::REQUEST_HEADERS, ["X-Invalidate-Token" => "__TOKEN__"]),
$messageFactory
);
}

public function it_should_purge_without_additional_token_header_when_configuration_key_with_token_do_not_exist_in_configuration(
ConfigResolverInterface $configResolver,
RequestFactory $messageFactory
) {
$configResolver->hasParameter('http_cache.varnish_invalidate_token')->willReturn(false);

$this->purge(self::URI, self::REQUEST_HEADERS);

$this->requestShouldHaveBeenCreatedWithHeaders(
self::REQUEST_HEADERS,
$messageFactory
);
}

public function it_should_purge_without_additional_token_header_when_configuration_key_with_token_exists_but_is_null(
ConfigResolverInterface $configResolver,
RequestFactory $messageFactory
) {
$configResolver->hasParameter('http_cache.varnish_invalidate_token')->willReturn(true);
$configResolver->getParameter('http_cache.varnish_invalidate_token')->willReturn(null);

$this->purge(self::URI, self::REQUEST_HEADERS);

$this->requestShouldHaveBeenCreatedWithHeaders(
self::REQUEST_HEADERS,
$messageFactory
);
}

private function requestShouldHaveBeenCreatedWithHeaders($headers, RequestFactory $messageFactory)
{
$messageFactory->createRequest(
'PURGE',
self::URI,
$headers,
Argument::any()
)->shouldHaveBeenCalled();
}
}
14 changes: 12 additions & 2 deletions src/ProxyClient/Varnish.php
Original file line number Diff line number Diff line change
@@ -35,13 +35,23 @@ public function __construct(

private function fetchAndMergeAuthHeaders(array $headers): array
{
if ($this->configResolver->hasParameter('http_cache.varnish_invalidate_token')) {
$headers[InvalidateTokenController::TOKEN_HEADER_NAME] = $this->configResolver->getParameter('http_cache.varnish_invalidate_token');
$invalidateToken = $this->getInvalidateToken();
if (null !== $invalidateToken) {
$headers[InvalidateTokenController::TOKEN_HEADER_NAME] = $invalidateToken;
}

return $headers;
}

private function getInvalidateToken(): ?string
{
if ($this->configResolver->hasParameter('http_cache.varnish_invalidate_token')) {
return $this->configResolver->getParameter('http_cache.varnish_invalidate_token');
}

return null;
}

protected function queueRequest($method, $url, array $headers, $validateHost = true, $body = null)
{
parent::queueRequest($method, $url, $this->fetchAndMergeAuthHeaders($headers), $body);