Skip to content

Commit 8dd255c

Browse files
authored
Merge pull request #598 from flightphp/cache-control
Changed default cache behavior
2 parents 9f5457e + ba80e04 commit 8dd255c

File tree

3 files changed

+61
-16
lines changed

3 files changed

+61
-16
lines changed

flight/net/Response.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -288,13 +288,7 @@ public function cache($expires): self
288288
{
289289
if ($expires === false) {
290290
$this->headers['Expires'] = 'Mon, 26 Jul 1997 05:00:00 GMT';
291-
292-
$this->headers['Cache-Control'] = [
293-
'no-store, no-cache, must-revalidate',
294-
'post-check=0, pre-check=0',
295-
'max-age=0',
296-
];
297-
291+
$this->headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, max-age=0';
298292
$this->headers['Pragma'] = 'no-cache';
299293
} else {
300294
$expires = \is_int($expires) ? $expires : strtotime($expires);
@@ -437,15 +431,31 @@ public function send(): void
437431
$this->processResponseCallbacks();
438432
}
439433

440-
if (headers_sent() === false) {
441-
$this->sendHeaders(); // @codeCoverageIgnore
434+
if ($this->headersSent() === false) {
435+
// If you haven't set a Cache-Control header, we'll assume you don't want caching
436+
if ($this->getHeader('Cache-Control') === null) {
437+
$this->cache(false);
438+
}
439+
440+
$this->sendHeaders();
442441
}
443442

444443
echo $this->body;
445444

446445
$this->sent = true;
447446
}
448447

448+
/**
449+
* Headers have been sent
450+
*
451+
* @return bool
452+
* @codeCoverageIgnore
453+
*/
454+
public function headersSent(): bool
455+
{
456+
return headers_sent();
457+
}
458+
449459
/**
450460
* Adds a callback to process the response body before it's sent. These are processed in the order
451461
* they are added

tests/FlightTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,7 @@ public function testDoesNotPreserveVarsWhenFlagIsDisabled(
361361
string $output,
362362
array $renderParams,
363363
string $regexp
364-
): void
365-
{
364+
): void {
366365
Flight::view()->preserveVars = false;
367366

368367
$this->expectOutputString($output);

tests/ResponseTest.php

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,7 @@ public function testCacheFalseExpiresValue()
164164
$response->cache(false);
165165
$this->assertEquals([
166166
'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT',
167-
'Cache-Control' => [
168-
'no-store, no-cache, must-revalidate',
169-
'post-check=0, pre-check=0',
170-
'max-age=0',
171-
],
167+
'Cache-Control' => 'no-store, no-cache, must-revalidate, max-age=0',
172168
'Pragma' => 'no-cache'
173169
], $response->headers());
174170
}
@@ -239,6 +235,46 @@ public function setRealHeader(string $header_string, bool $replace = true, int $
239235
$this->assertTrue($response->sent());
240236
}
241237

238+
public function testSendWithNoHeadersSent()
239+
{
240+
$response = new class extends Response {
241+
protected $test_sent_headers = [];
242+
243+
public function setRealHeader(string $header_string, bool $replace = true, int $response_code = 0): self
244+
{
245+
$this->test_sent_headers[] = $header_string;
246+
return $this;
247+
}
248+
249+
public function getSentHeaders(): array
250+
{
251+
return $this->test_sent_headers;
252+
}
253+
254+
public function headersSent(): bool
255+
{
256+
return false;
257+
}
258+
};
259+
$response->header('Content-Type', 'text/html');
260+
$response->header('X-Test', 'test');
261+
$response->write('Something');
262+
263+
$this->expectOutputString('Something');
264+
265+
$response->send();
266+
$sent_headers = $response->getSentHeaders();
267+
$this->assertEquals([
268+
'HTTP/1.1 200 OK',
269+
'Content-Type: text/html',
270+
'X-Test: test',
271+
'Expires: Mon, 26 Jul 1997 05:00:00 GMT',
272+
'Cache-Control: no-store, no-cache, must-revalidate, max-age=0',
273+
'Pragma: no-cache',
274+
'Content-Length: 9'
275+
], $sent_headers);
276+
}
277+
242278
public function testClearBody()
243279
{
244280
$response = new Response();

0 commit comments

Comments
 (0)