Skip to content
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
5 changes: 4 additions & 1 deletion src/Symfony/Bundle/Test/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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,
Expand Down
34 changes: 34 additions & 0 deletions tests/Symfony/Bundle/Test/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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());
}
}
Loading