diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 5875f54a011..d7796e04226 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -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 diff --git a/framework/web/Response.php b/framework/web/Response.php index cbfc512b648..22a617cf07c 100644 --- a/framework/web/Response.php +++ b/framework/web/Response.php @@ -410,10 +410,6 @@ protected function sendCookies() */ protected function sendContent() { - if ($this->getStatusCode() === 204) { - return; - } - if ($this->stream === null) { echo $this->content; @@ -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; } diff --git a/tests/framework/web/ResponseTest.php b/tests/framework/web/ResponseTest.php index 97a96dc4838..20553a7ddf1 100644 --- a/tests/framework/web/ResponseTest.php +++ b/tests/framework/web/ResponseTest.php @@ -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); + } }