Skip to content

Commit

Permalink
added unit test, corrected other response logic
Browse files Browse the repository at this point in the history
  • Loading branch information
n0nag0n committed Apr 11, 2024
1 parent 3659a76 commit 77d3131
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
20 changes: 10 additions & 10 deletions flight/net/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,15 @@ public function sendHeaders(): self
);
}

if ($this->content_length === true) {
// Send content length
$length = $this->getContentLength();

if ($length > 0) {
$this->setHeader('Content-Length', (string) $length);
}
}

// Send other headers
foreach ($this->headers as $field => $value) {
if (\is_array($value)) {
Expand All @@ -352,15 +361,6 @@ public function sendHeaders(): self
}
}

if ($this->content_length) {
// Send content length
$length = $this->getContentLength();

if ($length > 0) {
$this->setRealHeader('Content-Length: ' . $length);
}
}

return $this;
}

Expand Down Expand Up @@ -437,7 +437,7 @@ public function send(): void
$this->processResponseCallbacks();
}

if (!headers_sent()) {
if (headers_sent() === false) {
$this->sendHeaders(); // @codeCoverageIgnore
}

Expand Down
1 change: 1 addition & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<exclude name="Generic.Commenting.DocComment.ContentBeforeClose" />
<exclude name="Generic.Commenting.DocComment.MissingShort" />
<exclude name="Generic.Commenting.DocComment.SpacingBeforeShort" />
<exclude name="Generic.Formatting.NoSpaceAfterCast.SpaceFound" />
<exclude name="Generic.Functions.OpeningFunctionBraceKernighanRitchie.BraceOnNewLine" />
<exclude name="Generic.Formatting.MultipleStatementAlignment.NotSameWarning" />
<exclude name="Generic.Functions.OpeningFunctionBraceBsdAllman.BraceOnSameLine" />
Expand Down
16 changes: 16 additions & 0 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,22 @@ public function testResponseBodyCallback()
$this->assertEquals('grfg', $rot13_body);
}

public function testResponseBodyCallbackGzip()
{
$response = new Response();
$response->content_length = true;
$response->write('test');
$gzip = function ($body) {
return gzencode($body);
};
$response->addResponseBodyCallback($gzip);
ob_start();
$response->send();
$gzip_body = ob_get_clean();
$this->assertEquals('H4sIAAAAAAAAAytJLS4BAAx+f9gEAAAA', base64_encode($gzip_body));
$this->assertEquals(strlen(gzencode('test')), strlen($gzip_body));
}

public function testResponseBodyCallbackMultiple()
{
$response = new Response();
Expand Down

0 comments on commit 77d3131

Please sign in to comment.