Skip to content

Commit bc55b03

Browse files
committed
Merge branch '6.2' into 6.3
* 6.2: fix test [Response] `getMaxAge()` returns non-negative integer
2 parents 1655d17 + d59d2e5 commit bc55b03

File tree

6 files changed

+39
-10
lines changed

6 files changed

+39
-10
lines changed

src/Symfony/Component/ErrorHandler/Tests/ErrorRenderer/HtmlErrorRendererTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function getRenderData(): iterable
3737

3838
$expectedNonDebug = <<<HTML
3939
<!DOCTYPE html>
40-
<html>
40+
<html lang="en">
4141
%A<title>An Error Occurred: Internal Server Error</title>
4242
%A<h2>The server returned a "500 Internal Server Error".</h2>%A
4343
HTML;

src/Symfony/Component/HttpFoundation/Response.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -758,8 +758,10 @@ public function getMaxAge(): ?int
758758
return (int) $this->headers->getCacheControlDirective('max-age');
759759
}
760760

761-
if (null !== $this->getExpires()) {
762-
return (int) $this->getExpires()->format('U') - (int) $this->getDate()->format('U');
761+
if (null !== $expires = $this->getExpires()) {
762+
$maxAge = (int) $expires->format('U') - (int) $this->getDate()->format('U');
763+
764+
return max($maxAge, 0);
763765
}
764766

765767
return null;
@@ -835,7 +837,7 @@ public function setSharedMaxAge(int $value): static
835837
*
836838
* It returns null when no freshness information is present in the response.
837839
*
838-
* When the responses TTL is <= 0, the response may not be served from cache without first
840+
* When the response's TTL is 0, the response may not be served from cache without first
839841
* revalidating with the origin.
840842
*
841843
* @final
@@ -844,7 +846,7 @@ public function getTtl(): ?int
844846
{
845847
$maxAge = $this->getMaxAge();
846848

847-
return null !== $maxAge ? $maxAge - $this->getAge() : null;
849+
return null !== $maxAge ? max($maxAge - $this->getAge(), 0) : null;
848850
}
849851

850852
/**

src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,8 @@ public function testGetMaxAge()
341341
$this->assertEquals(3600, $response->getMaxAge(), '->getMaxAge() falls back to Expires when no max-age or s-maxage directive present');
342342

343343
$response = new Response();
344-
$response->headers->set('Cache-Control', 'must-revalidate');
345344
$response->headers->set('Expires', -1);
346-
$this->assertLessThanOrEqual(time() - 2 * 86400, $response->getExpires()->format('U'));
345+
$this->assertSame(0, $response->getMaxAge());
347346

348347
$response = new Response();
349348
$this->assertNull($response->getMaxAge(), '->getMaxAge() returns null if no freshness information available');
@@ -462,7 +461,7 @@ public function testGetTtl()
462461

463462
$response = new Response();
464463
$response->headers->set('Expires', $this->createDateTimeOneHourAgo()->format(\DATE_RFC2822));
465-
$this->assertLessThan(0, $response->getTtl(), '->getTtl() returns negative values when Expires is in past');
464+
$this->assertSame(0, $response->getTtl(), '->getTtl() returns zero when Expires is in past');
466465

467466
$response = new Response();
468467
$response->headers->set('Expires', $response->getDate()->format(\DATE_RFC2822));

src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,11 @@ private function mayServeStaleWhileRevalidate(Response $entry): bool
698698
$timeout = $entry->headers->getCacheControlDirective('stale-while-revalidate');
699699
$timeout ??= $this->options['stale_while_revalidate'];
700700

701-
return abs($entry->getTtl() ?? 0) < $timeout;
701+
$age = $entry->getAge();
702+
$maxAge = $entry->getMaxAge() ?? 0;
703+
$ttl = $maxAge - $age;
704+
705+
return abs($ttl) < $timeout;
702706
}
703707

704708
/**

src/Symfony/Component/HttpKernel/Tests/EventListener/SessionListenerTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,30 @@ public function testResponseHeadersMaxAgeAndExpiresDefaultValuesIfSessionStarted
618618
$this->assertFalse($response->headers->has(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER));
619619
}
620620

621+
public function testPrivateResponseMaxAgeIsRespectedIfSessionStarted()
622+
{
623+
$kernel = $this->createMock(HttpKernelInterface::class);
624+
625+
$session = $this->createMock(Session::class);
626+
$session->expects($this->once())->method('getUsageIndex')->willReturn(1);
627+
$request = new Request([], [], [], [], [], ['SERVER_PROTOCOL' => 'HTTP/1.0']);
628+
$request->setSession($session);
629+
630+
$response = new Response();
631+
$response->headers->set('Cache-Control', 'no-cache');
632+
$response->prepare($request);
633+
634+
$listener = new SessionListener(new Container());
635+
$listener->onKernelResponse(new ResponseEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $response));
636+
637+
$this->assertSame(0, $response->getMaxAge());
638+
$this->assertFalse($response->headers->hasCacheControlDirective('public'));
639+
$this->assertTrue($response->headers->hasCacheControlDirective('private'));
640+
$this->assertTrue($response->headers->hasCacheControlDirective('must-revalidate'));
641+
$this->assertLessThanOrEqual(new \DateTimeImmutable('now', new \DateTimeZone('UTC')), new \DateTimeImmutable($response->headers->get('Expires')));
642+
$this->assertFalse($response->headers->has(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER));
643+
}
644+
621645
public function testSurrogateMainRequestIsPublic()
622646
{
623647
$session = $this->createMock(Session::class);

src/Symfony/Component/HttpKernel/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"symfony/deprecation-contracts": "^2.5|^3",
2121
"symfony/error-handler": "^6.1",
2222
"symfony/event-dispatcher": "^5.4|^6.0",
23-
"symfony/http-foundation": "^5.4|^6.0",
23+
"symfony/http-foundation": "^5.4.21|^6.2.7",
2424
"symfony/polyfill-ctype": "^1.8",
2525
"psr/log": "^1|^2|^3"
2626
},

0 commit comments

Comments
 (0)