diff --git a/src/Symfony/Bundle/Test/Response.php b/src/Symfony/Bundle/Test/Response.php index 3ac943beed..41a1afe82f 100644 --- a/src/Symfony/Bundle/Test/Response.php +++ b/src/Symfony/Bundle/Test/Response.php @@ -20,6 +20,7 @@ use Symfony\Component\HttpClient\Exception\ServerException; use Symfony\Component\HttpClient\Exception\TransportException; use Symfony\Component\HttpFoundation\Response as HttpFoundationResponse; +use Symfony\Component\HttpFoundation\StreamedResponse; use Symfony\Contracts\HttpClient\ResponseInterface; /** @@ -50,7 +51,9 @@ public function __construct(private readonly HttpFoundationResponse $httpFoundat } } - $this->content = (string) $httpFoundationResponse->getContent(); + $this->content = $httpFoundationResponse instanceof StreamedResponse + ? $browserKitResponse->getContent() + : (string) $httpFoundationResponse->getContent(); $this->info = [ 'http_code' => $httpFoundationResponse->getStatusCode(), 'error' => null, diff --git a/tests/Symfony/Bundle/Test/ResponseTest.php b/tests/Symfony/Bundle/Test/ResponseTest.php index 4d5c674420..b8b3276e41 100644 --- a/tests/Symfony/Bundle/Test/ResponseTest.php +++ b/tests/Symfony/Bundle/Test/ResponseTest.php @@ -23,6 +23,8 @@ use Symfony\Component\HttpClient\Exception\ServerException; use Symfony\Component\HttpClient\Exception\TransportException; use Symfony\Component\HttpFoundation\Response as HttpFoundationResponse; +use Symfony\Component\HttpFoundation\StreamedJsonResponse; +use Symfony\Component\HttpFoundation\StreamedResponse; class ResponseTest extends TestCase { @@ -122,4 +124,36 @@ public function testCancel(): void $this->assertSame('Response has been canceled.', $response->getInfo('error')); } + + public function testStreamedResponseContentIsCapturedFromBrowserKitResponse(): void + { + $streamedBody = '{"foo":"bar"}'; + // BrowserKit's HttpKernelBrowser::filterResponse already buffered the streamed body + // into the BrowserKit Response, but the HttpFoundation StreamedResponse::getContent() + // still returns false. The Response wrapper must read from the BrowserKit response. + $browserKitResponse = new BrowserKitResponse($streamedBody, 200, ['content-type' => 'application/json']); + $httpFoundationResponse = new StreamedResponse(static function () use ($streamedBody): void { + echo $streamedBody; + }, 200, ['content-type' => 'application/json']); + + $response = new Response($httpFoundationResponse, $browserKitResponse, []); + + $this->assertSame($streamedBody, $response->getContent()); + $this->assertSame(['foo' => 'bar'], $response->toArray()); + } + + public function testStreamedJsonResponseContentIsCapturedFromBrowserKitResponse(): void + { + if (!class_exists(StreamedJsonResponse::class)) { + $this->markTestSkipped('StreamedJsonResponse is not available.'); + } + + $streamedBody = '{"items":[1,2,3]}'; + $browserKitResponse = new BrowserKitResponse($streamedBody, 200, ['content-type' => 'application/json']); + $httpFoundationResponse = new StreamedJsonResponse(['items' => [1, 2, 3]]); + + $response = new Response($httpFoundationResponse, $browserKitResponse, []); + + $this->assertSame($streamedBody, $response->getContent()); + } }