changed default cache behavior

pull/598/head
Austin Collier 7 months ago
parent 9f5457ea1f
commit f06febdb2d

@ -288,13 +288,7 @@ class Response
{
if ($expires === false) {
$this->headers['Expires'] = 'Mon, 26 Jul 1997 05:00:00 GMT';
$this->headers['Cache-Control'] = [
'no-store, no-cache, must-revalidate',
'post-check=0, pre-check=0',
'max-age=0',
];
$this->headers['Cache-Control'] = 'no-store, no-cache, must-revalidate';
$this->headers['Pragma'] = 'no-cache';
} else {
$expires = \is_int($expires) ? $expires : strtotime($expires);
@ -437,8 +431,14 @@ class Response
$this->processResponseCallbacks();
}
if (headers_sent() === false) {
$this->sendHeaders(); // @codeCoverageIgnore
if ($this->headersSent() === false) {
// If you haven't set a Cache-Control header, we'll assume you don't want caching
if($this->getHeader('Cache-Control') === null) {
$this->cache(false);
}
$this->sendHeaders();
}
echo $this->body;
@ -446,6 +446,17 @@ class Response
$this->sent = true;
}
/**
* Headers have been sent
*
* @return bool
* @codeCoverageIgnore
*/
public function headersSent(): bool
{
return headers_sent();
}
/**
* Adds a callback to process the response body before it's sent. These are processed in the order
* they are added

@ -164,11 +164,7 @@ class ResponseTest extends TestCase
$response->cache(false);
$this->assertEquals([
'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT',
'Cache-Control' => [
'no-store, no-cache, must-revalidate',
'post-check=0, pre-check=0',
'max-age=0',
],
'Cache-Control' => 'no-store, no-cache, must-revalidate',
'Pragma' => 'no-cache'
], $response->headers());
}
@ -239,6 +235,46 @@ class ResponseTest extends TestCase
$this->assertTrue($response->sent());
}
public function testSendWithNoHeadersSent()
{
$response = new class extends Response {
protected $test_sent_headers = [];
public function setRealHeader(string $header_string, bool $replace = true, int $response_code = 0): self
{
$this->test_sent_headers[] = $header_string;
return $this;
}
public function getSentHeaders(): array
{
return $this->test_sent_headers;
}
public function headersSent(): bool
{
return false;
}
};
$response->header('Content-Type', 'text/html');
$response->header('X-Test', 'test');
$response->write('Something');
$this->expectOutputString('Something');
$response->send();
$sent_headers = $response->getSentHeaders();
$this->assertEquals([
'HTTP/1.1 200 OK',
'Content-Type: text/html',
'X-Test: test',
'Expires: Mon, 26 Jul 1997 05:00:00 GMT',
'Cache-Control: no-store, no-cache, must-revalidate',
'Pragma: no-cache',
'Content-Length: 9'
], $sent_headers);
}
public function testClearBody()
{
$response = new Response();

Loading…
Cancel
Save