How to retrieve a streamed response (e.g. download a file) with Symfony test client
Asked Answered
M

3

18

I am writing functional tests with Symfony2.

I have a controller that calls a getImage() function which streams an image file as follows:

public function getImage($filePath)
    $response = new StreamedResponse();
    $response->headers->set('Content-Type', 'image/png');

    $response->setCallback(function () use ($filePath) {
        $bytes = @readfile(filePath);
        if ($bytes === false || $bytes <= 0)
            throw new NotFoundHttpException();
    });

    return $response;
}

In functional testing, I try to request the content with the Symfony test client as follows:

$client = static::createClient();
$client->request('GET', $url);
$content = $client->getResponse()->getContent();

The problem is that $content is empty, I guess because the response is generated as soon as the HTTP headers are received by the client, without waiting for a data stream to be delivered.

Is there a way to catch the content of the streamed response while still using $client->request() (or even some other function) to send the request to the server?

Mame answered 31/3, 2013 at 21:23 Comment(0)
W
23

The return value of sendContent (rather than getContent) is the callback that you've set. getContent actually just returns false in Symfony2

Using sendContent you can enable the output buffer and assign the content to that for your tests, like so:

$client = static::createClient();
$client->request('GET', $url);

// Enable the output buffer
ob_start();
// Send the response to the output buffer
$client->getResponse()->sendContent();
// Get the contents of the output buffer
$content = ob_get_contents();
// Clean the output buffer and end it
ob_end_clean();

You can read more on the output buffer here

The API for StreamResponse is here

Wryneck answered 16/8, 2013 at 16:14 Comment(1)
For me to get this to work I had to place ob_start() before making the request.Zilvia
T
13

The current best answer used to work well for me for some time, but for some reason it isn't anymore. The response is parsed into a DOM crawler and the binary data is lost.

I could fix that by using the internal response. Here's the git patch of my changes[1]:

-        ob_start();
         $this->request('GET', $uri);
-        $responseData = ob_get_clean();
+        $responseData = self::$client->getInternalResponse()->getContent();

I hope this can help someone.

[1]: you just need access to the client, which is a Symfony\Bundle\FrameworkBundle\KernelBrowser

Tailpipe answered 24/4, 2020 at 16:33 Comment(2)
Great stuff man, just saved me with this one.Cobnut
in symfony 6, $client->getInternalResponse()->getContent() did the job. thanksKendall
M
10

For me didn't work like that. Instead, I used ob_start() before making the request, and after the request i used $content = ob_get_clean() and made asserts on that content.

In test:

    // Enable the output buffer
    ob_start();
    $this->client->request(
        'GET',
        '$url',
        array(),
        array(),
        array('CONTENT_TYPE' => 'application/json')
    );
    // Get the output buffer and clean it
    $content = ob_get_clean();
    $this->assertEquals('my response content', $content);

Maybe this was because my response is a csv file.

In controller:

    $response->headers->set('Content-Type', 'text/csv; charset=utf-8');
Mediant answered 26/5, 2015 at 10:12 Comment(1)
Thanks, it worked for me also using a Symfony\Component\HttpFoundation\Response object.Philippines

© 2022 - 2024 — McMap. All rights reserved.