From fd596d1ff070b88c133de6e9cbe2e0a4d7e94db3 Mon Sep 17 00:00:00 2001 From: Tung Vu Date: Tue, 19 Apr 2022 09:27:04 +0700 Subject: [PATCH 1/3] don't write null to stream if response is empty --- Factory/PsrHttpFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Factory/PsrHttpFactory.php b/Factory/PsrHttpFactory.php index d19baa1..fb214d3 100644 --- a/Factory/PsrHttpFactory.php +++ b/Factory/PsrHttpFactory.php @@ -143,7 +143,7 @@ public function createResponse(Response $symfonyResponse) $symfonyResponse->sendContent(); ob_end_clean(); } else { - $stream->write($symfonyResponse->getContent()); + $stream->write($symfonyResponse->isEmpty() ? '' : $symfonyResponse->getContent()); } } From ab112c4f2fa97b7f452d2740d17d3c0648d99e3e Mon Sep 17 00:00:00 2001 From: Tung Vu Date: Tue, 19 Apr 2022 23:39:26 +0700 Subject: [PATCH 2/3] add test with up-to-date signature of \Symfony\Component\HttpFoundation\Response::getContent --- Factory/PsrHttpFactory.php | 2 +- Tests/Fixtures/FalseContentResponse.php | 18 ++++++++++++++++++ Tests/Functional/CovertTest.php | 19 +++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 Tests/Fixtures/FalseContentResponse.php diff --git a/Factory/PsrHttpFactory.php b/Factory/PsrHttpFactory.php index fb214d3..12f583f 100644 --- a/Factory/PsrHttpFactory.php +++ b/Factory/PsrHttpFactory.php @@ -143,7 +143,7 @@ public function createResponse(Response $symfonyResponse) $symfonyResponse->sendContent(); ob_end_clean(); } else { - $stream->write($symfonyResponse->isEmpty() ? '' : $symfonyResponse->getContent()); + $stream->write(is_string($symfonyResponse->getContent()) ? $symfonyResponse->getContent() : ''); } } diff --git a/Tests/Fixtures/FalseContentResponse.php b/Tests/Fixtures/FalseContentResponse.php new file mode 100644 index 0000000..3d6d1d7 --- /dev/null +++ b/Tests/Fixtures/FalseContentResponse.php @@ -0,0 +1,18 @@ +content = false; + } +} \ No newline at end of file diff --git a/Tests/Functional/CovertTest.php b/Tests/Functional/CovertTest.php index 3d72b71..ffa921e 100644 --- a/Tests/Functional/CovertTest.php +++ b/Tests/Functional/CovertTest.php @@ -22,6 +22,7 @@ use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory; use Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface; use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface; +use Symfony\Bridge\PsrHttpMessage\Tests\Fixtures\FalseContentResponse; use Symfony\Component\HttpFoundation\Cookie; use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\HttpFoundation\Request; @@ -234,4 +235,22 @@ private function createUploadedFile($content, $originalName, $mimeType, $error) return new UploadedFile($path, $originalName, $mimeType, $error, true); } + + public function testConvertResponseNotPanicOnNullContentResponse() + { + $sfResponse = new FalseContentResponse( + null, + 201, + ['x-symfony' => ['3.4']] + ); + $sfResponse->setContentFalse(); + + $factory = new Psr17Factory(); + + $finalFactory = new PsrHttpFactory($factory, $factory, $factory, $factory); + + $convertedResponse = $finalFactory->createResponse($sfResponse); + + self::assertNotNull($convertedResponse); + } } From d6160e3a68d159cbb3457b2467780998a330581c Mon Sep 17 00:00:00 2001 From: Tung Vu <96406390+tungfinblox@users.noreply.github.com> Date: Wed, 20 Apr 2022 12:44:50 +0700 Subject: [PATCH 3/3] convert to string without checking content MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Kévin Dunglas --- Factory/PsrHttpFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Factory/PsrHttpFactory.php b/Factory/PsrHttpFactory.php index 12f583f..2fe71ec 100644 --- a/Factory/PsrHttpFactory.php +++ b/Factory/PsrHttpFactory.php @@ -143,7 +143,7 @@ public function createResponse(Response $symfonyResponse) $symfonyResponse->sendContent(); ob_end_clean(); } else { - $stream->write(is_string($symfonyResponse->getContent()) ? $symfonyResponse->getContent() : ''); + $stream->write((string) $symfonyResponse->getContent()); } }