Skip to content

Commit ccadc89

Browse files
committed
Merge remote-tracking branch 'tier4/ACP2E-3069' into T4-PR-07-02-2024
2 parents 1afc5b4 + e0a9430 commit ccadc89

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

app/code/Magento/PageCache/Model/App/Response/HttpPlugin.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,24 @@
88

99
use Magento\Framework\App\PageCache\NotCacheableInterface;
1010
use Magento\Framework\App\Response\Http as HttpResponse;
11+
use Magento\Framework\App\Request\Http as HttpRequest;
12+
use Magento\Framework\App\Http\Context;
1113

1214
/**
1315
* HTTP response plugin for frontend.
1416
*/
1517
class HttpPlugin
1618
{
19+
/**
20+
* @param Context $context
21+
* @param HttpRequest $request
22+
*/
23+
public function __construct(
24+
private Context $context,
25+
private HttpRequest $request
26+
) {
27+
}
28+
1729
/**
1830
* Set proper value of X-Magento-Vary cookie.
1931
*
@@ -28,6 +40,13 @@ public function beforeSendResponse(HttpResponse $subject)
2840
) {
2941
return;
3042
}
43+
44+
$currentVary = $this->context->getVaryString();
45+
$varyCookie = $this->request->get(HttpResponse::COOKIE_VARY_STRING);
46+
if ($currentVary !== $varyCookie) {
47+
//prevent caching with the old vary cookie
48+
$subject->setNoCacheHeaders();
49+
}
3150
$subject->sendVary();
3251
}
3352
}

app/code/Magento/PageCache/Test/Unit/Model/App/Response/HttpPluginTest.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
namespace Magento\PageCache\Test\Unit\Model\App\Response;
99

10+
use Magento\Framework\App\Http\Context;
1011
use Magento\Framework\App\Response\Http as HttpResponse;
12+
use Magento\Framework\App\Request\Http as HttpRequest;
1113
use Magento\MediaStorage\Model\File\Storage\Response as FileResponse;
1214
use Magento\PageCache\Model\App\Response\HttpPlugin;
1315
use PHPUnit\Framework\MockObject\MockObject;
@@ -23,13 +25,28 @@ class HttpPluginTest extends TestCase
2325
*/
2426
private $httpPlugin;
2527

28+
/**
29+
* @var Context|MockObject
30+
*/
31+
private $context;
32+
33+
/**
34+
* @var HttpRequest|MockObject
35+
*/
36+
private $request;
37+
2638
/**
2739
* @inheritdoc
2840
*/
2941
protected function setUp(): void
3042
{
3143
parent::setUp();
32-
$this->httpPlugin = new HttpPlugin();
44+
$this->context = $this->createMock(Context::class);
45+
$this->request = $this->createMock(HttpRequest::class);
46+
$this->httpPlugin = new HttpPlugin(
47+
$this->context,
48+
$this->request
49+
);
3350
}
3451

3552
/**
@@ -62,4 +79,17 @@ public function beforeSendResponseDataProvider(): array
6279
'file_response_headers_sent' => [FileResponse::class, true, 0],
6380
];
6481
}
82+
83+
public function testBeforeSendResponseVaryMismatch()
84+
{
85+
/** @var HttpResponse|MockObject $responseMock */
86+
$this->context->expects($this->any())->method('getVaryString')->willReturn('currentVary');
87+
$this->request->expects($this->any())->method('get')->willReturn('varyCookie');
88+
/** @var HttpResponse|MockObject $responseMock */
89+
$responseMock = $this->createMock(HttpResponse::class);
90+
$responseMock->expects($this->once())->method('setNoCacheHeaders');
91+
$responseMock->expects($this->once())->method('sendVary');
92+
93+
$this->httpPlugin->beforeSendResponse($responseMock);
94+
}
6595
}

0 commit comments

Comments
 (0)