Skip to content

Commit b4b1fef

Browse files
committed
feature #18220 Don't send default cache header for 301 redirects (e-moe)
This PR was merged into the 3.2-dev branch. Discussion ---------- Don't send default cache header for 301 redirects | Q | A | ------------- | --- | Branch? | "master" | Bug fix? | no | New feature? | yes | BC breaks? | yes | Deprecations? | no | Tests pass? | yes | Fixed tickets | #17139 | License | MIT | Doc PR | see comments from #17139 Commits ------- cf253a9 17139: do not send default cache header for 301 redirects
2 parents f400f01 + cf253a9 commit b4b1fef

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/Symfony/Component/HttpFoundation/RedirectResponse.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ public function __construct($url, $status = 302, $headers = array())
4141
if (!$this->isRedirect()) {
4242
throw new \InvalidArgumentException(sprintf('The HTTP status code is not a redirect ("%s" given).', $status));
4343
}
44+
45+
if (301 == $status && !array_key_exists('cache-control', $headers)) {
46+
$this->headers->remove('cache-control');
47+
}
4448
}
4549

4650
/**

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,17 @@ public function testCreate()
8080
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
8181
$this->assertEquals(301, $response->getStatusCode());
8282
}
83+
84+
public function testCacheHeaders()
85+
{
86+
$response = new RedirectResponse('foo.bar', 301);
87+
$this->assertFalse($response->headers->hasCacheControlDirective('no-cache'));
88+
89+
$response = new RedirectResponse('foo.bar', 301, array('cache-control' => 'max-age=86400'));
90+
$this->assertFalse($response->headers->hasCacheControlDirective('no-cache'));
91+
$this->assertTrue($response->headers->hasCacheControlDirective('max-age'));
92+
93+
$response = new RedirectResponse('foo.bar', 302);
94+
$this->assertTrue($response->headers->hasCacheControlDirective('no-cache'));
95+
}
8396
}

0 commit comments

Comments
 (0)