Skip to content

Commit

Permalink
prettified and added max-age=0
Browse files Browse the repository at this point in the history
  • Loading branch information
n0nag0n committed Jun 29, 2024
1 parent f06febd commit ba80e04
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 29 deletions.
31 changes: 15 additions & 16 deletions flight/net/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public function cache($expires): self
{
if ($expires === false) {
$this->headers['Expires'] = 'Mon, 26 Jul 1997 05:00:00 GMT';
$this->headers['Cache-Control'] = 'no-store, no-cache, must-revalidate';
$this->headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, max-age=0';
$this->headers['Pragma'] = 'no-cache';
} else {
$expires = \is_int($expires) ? $expires : strtotime($expires);
Expand Down Expand Up @@ -432,11 +432,10 @@ public function send(): void
}

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);
}
// 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();
}
Expand All @@ -446,16 +445,16 @@ public function send(): void
$this->sent = true;
}

/**
* Headers have been sent
*
* @return bool
* @codeCoverageIgnore
*/
public function headersSent(): bool
{
return headers_sent();
}
/**
* 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
Expand Down
3 changes: 1 addition & 2 deletions tests/FlightTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,7 @@ public function testDoesNotPreserveVarsWhenFlagIsDisabled(
string $output,
array $renderParams,
string $regexp
): void
{
): void {
Flight::view()->preserveVars = false;

$this->expectOutputString($output);
Expand Down
22 changes: 11 additions & 11 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public function testCacheFalseExpiresValue()
$response->cache(false);
$this->assertEquals([
'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT',
'Cache-Control' => 'no-store, no-cache, must-revalidate',
'Cache-Control' => 'no-store, no-cache, must-revalidate, max-age=0',
'Pragma' => 'no-cache'
], $response->headers());
}
Expand Down Expand Up @@ -235,8 +235,8 @@ public function setRealHeader(string $header_string, bool $replace = true, int $
$this->assertTrue($response->sent());
}

public function testSendWithNoHeadersSent()
{
public function testSendWithNoHeadersSent()
{
$response = new class extends Response {
protected $test_sent_headers = [];

Expand All @@ -251,26 +251,26 @@ public function getSentHeaders(): array
return $this->test_sent_headers;
}

public function headersSent(): bool
{
return false;
}
public function headersSent(): bool
{
return false;
}
};
$response->header('Content-Type', 'text/html');
$response->header('X-Test', 'test');
$response->write('Something');

$this->expectOutputString('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',
'Expires: Mon, 26 Jul 1997 05:00:00 GMT',
'Cache-Control: no-store, no-cache, must-revalidate, max-age=0',
'Pragma: no-cache',
'Content-Length: 9'
], $sent_headers);
}
Expand Down

0 comments on commit ba80e04

Please sign in to comment.