Skip to content

Commit af60d85

Browse files
author
tomaszpaloc
committed
Merged branch '2.0' into 2.1
2 parents 7f07eb9 + d98a8ea commit af60d85

File tree

2 files changed

+104
-2
lines changed

2 files changed

+104
-2
lines changed

spec/ProxyClient/VarnishSpec.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
4+
* @license For full copyright and license information view LICENSE file distributed with this source code.
5+
*/
6+
namespace spec\EzSystems\PlatformHttpCacheBundle\ProxyClient;
7+
8+
use eZ\Publish\Core\MVC\ConfigResolverInterface;
9+
use FOS\HttpCache\ProxyClient\Dispatcher;
10+
use Http\Message\RequestFactory;
11+
use PhpSpec\ObjectBehavior;
12+
use Prophecy\Argument;
13+
use Psr\Http\Message\RequestInterface;
14+
15+
class VarnishSpec extends ObjectBehavior
16+
{
17+
private const URI = "/";
18+
private const REQUEST_HEADERS = [
19+
"X-Some-Header" => "__SOME_HEADER_VALUE__"
20+
];
21+
22+
public function let(
23+
ConfigResolverInterface $configResolver,
24+
Dispatcher $httpDispatcher,
25+
RequestFactory $messageFactory,
26+
RequestInterface $request
27+
28+
) {
29+
$messageFactory->createRequest(
30+
Argument::any(),
31+
Argument::any(),
32+
Argument::any(),
33+
Argument::any()
34+
)->willReturn($request);
35+
36+
$this->beConstructedWith($configResolver, $httpDispatcher, [], $messageFactory);
37+
}
38+
39+
public function it_should_purge_with_additional_token_header_when_configuration_key_with_token_is_not_null(
40+
ConfigResolverInterface $configResolver,
41+
RequestFactory $messageFactory
42+
) {
43+
$configResolver->hasParameter('http_cache.varnish_invalidate_token')->willReturn(true);
44+
$configResolver->getParameter('http_cache.varnish_invalidate_token')->willReturn('__TOKEN__');
45+
46+
$this->purge(self::URI, self::REQUEST_HEADERS);
47+
48+
$this->requestShouldHaveBeenCreatedWithHeaders(
49+
array_merge(self::REQUEST_HEADERS, ["X-Invalidate-Token" => "__TOKEN__"]),
50+
$messageFactory
51+
);
52+
}
53+
54+
public function it_should_purge_without_additional_token_header_when_configuration_key_with_token_do_not_exist_in_configuration(
55+
ConfigResolverInterface $configResolver,
56+
RequestFactory $messageFactory
57+
) {
58+
$configResolver->hasParameter('http_cache.varnish_invalidate_token')->willReturn(false);
59+
60+
$this->purge(self::URI, self::REQUEST_HEADERS);
61+
62+
$this->requestShouldHaveBeenCreatedWithHeaders(
63+
self::REQUEST_HEADERS,
64+
$messageFactory
65+
);
66+
}
67+
68+
public function it_should_purge_without_additional_token_header_when_configuration_key_with_token_exists_but_is_null(
69+
ConfigResolverInterface $configResolver,
70+
RequestFactory $messageFactory
71+
) {
72+
$configResolver->hasParameter('http_cache.varnish_invalidate_token')->willReturn(true);
73+
$configResolver->getParameter('http_cache.varnish_invalidate_token')->willReturn(null);
74+
75+
$this->purge(self::URI, self::REQUEST_HEADERS);
76+
77+
$this->requestShouldHaveBeenCreatedWithHeaders(
78+
self::REQUEST_HEADERS,
79+
$messageFactory
80+
);
81+
}
82+
83+
private function requestShouldHaveBeenCreatedWithHeaders($headers, RequestFactory $messageFactory)
84+
{
85+
$messageFactory->createRequest(
86+
'PURGE',
87+
self::URI,
88+
$headers,
89+
Argument::any()
90+
)->shouldHaveBeenCalled();
91+
}
92+
}

src/ProxyClient/Varnish.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,23 @@ public function __construct(
3535

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

4243
return $headers;
4344
}
4445

46+
private function getInvalidateToken(): ?string
47+
{
48+
if ($this->configResolver->hasParameter('http_cache.varnish_invalidate_token')) {
49+
return $this->configResolver->getParameter('http_cache.varnish_invalidate_token');
50+
}
51+
52+
return null;
53+
}
54+
4555
protected function queueRequest($method, $url, array $headers, $validateHost = true, $body = null)
4656
{
4757
parent::queueRequest($method, $url, $this->fetchAndMergeAuthHeaders($headers), $body);

0 commit comments

Comments
 (0)