Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do not populate $response when response code is 204 #17181

Merged
merged 2 commits into from
Mar 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Yii Framework 2 Change Log

- Bug #17156: Fixes PHP 7.2 warning when a data provider has no data as a parameter for a GridView (evilito)
- Bug #17083: Fixed `yii\validators\EmailValidator::$checkDNS` tells that every domain is correct on alpine linux (mikk150)
- Bug #17180: Do not populate `yii\web\Response::$response` when response code is 204 (mikk150)


2.0.16.1 February 28, 2019
Expand Down
10 changes: 6 additions & 4 deletions framework/web/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,10 +410,6 @@ protected function sendCookies()
*/
protected function sendContent()
{
if ($this->getStatusCode() === 204) {
return;
}

if ($this->stream === null) {
echo $this->content;

Expand Down Expand Up @@ -1035,6 +1031,12 @@ protected function defaultFormatters()
*/
protected function prepare()
{
if ($this->statusCode === 204) {
$this->content = '';
$this->stream = null;
return;
}

if ($this->stream !== null) {
return;
}
Expand Down
28 changes: 28 additions & 0 deletions tests/framework/web/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,32 @@ public function testEmptyContentOn204()
$content = ob_get_clean();
$this->assertSame($content, '');
}

public function testSettingContentToNullOn204()
{
$response = new Response();
$response->setStatusCode(204);
$response->content = 'not empty content';

ob_start();
$response->send();
$content = ob_get_clean();
$this->assertSame($content, '');
$this->assertSame($response->content, '');
}

public function testSettingStreamToNullOn204()
{
$response = new Response();
$dataFile = \Yii::getAlias('@yiiunit/data/web/data.txt');

$response->sendFile($dataFile);
$response->setStatusCode(204);

ob_start();
$response->send();
$content = ob_get_clean();
$this->assertSame($content, '');
$this->assertNull($response->stream);
}
}