Skip to content

Commit

Permalink
Fix file response conversions.
Browse files Browse the repository at this point in the history
The toCake() conversion did not convert files properly.
  • Loading branch information
markstory committed Jun 23, 2016
1 parent 985d50c commit ca87223
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/Http/ResponseTransformer.php
Expand Up @@ -38,12 +38,17 @@ class ResponseTransformer
*/
public static function toCake(PsrResponse $response)
{
$body = static::getBody($response);
$data = [
'status' => $response->getStatusCode(),
'body' => static::getBody($response),
'body' => $body['body'],
];
$cake = new CakeResponse($data);
foreach (static::parseCookies($response->getHeader('Set-Cookie')) as $cookie) {
if ($body['file']) {
$cake->file($body['file']);
}
$cookies = static::parseCookies($response->getHeader('Set-Cookie'));
foreach ($cookies as $cookie) {
$cake->cookie($cookie);
}
$cake->header(static::collapseHeaders($response));
Expand All @@ -54,16 +59,19 @@ public static function toCake(PsrResponse $response)
* Get the response body from a PSR7 Response.
*
* @param PsrResponse $response The response to convert.
* @return string The response body.
* @return array A hash of 'body' and 'file'
*/
protected static function getBody(PsrResponse $response)
{
$stream = $response->getBody();
if ($stream->getMetadata('wrapper_type') === 'plainfile') {
return ['body' => '', 'file' => $stream->getMetadata('uri')];
}
if ($stream->getSize() === 0) {
return '';
return ['body' => '', 'file' => false];
}
$stream->rewind();
return $stream->getContents();
return ['body' => $stream->getContents(), 'file' => false];
}

/**
Expand Down Expand Up @@ -195,7 +203,7 @@ protected static function getStream($response)
{
$stream = 'php://memory';
$body = $response->body();
if (is_string($body)) {
if (is_string($body) && strlen($body)) {
$stream = new Stream('php://memory', 'wb');
$stream->write($body);
return $stream;
Expand Down
22 changes: 22 additions & 0 deletions tests/TestCase/Http/ResponseTransformerTest.php
Expand Up @@ -18,6 +18,7 @@
use Cake\Network\Response as CakeResponse;
use Cake\TestSuite\TestCase;
use Zend\Diactoros\Response as PsrResponse;
use Zend\Diactoros\Stream;

/**
* Test case for the response transformer.
Expand Down Expand Up @@ -118,6 +119,27 @@ public function testToCakeBody()
$this->assertSame('A message for you', $result->body());
}

/**
* Test conversion with a file body.
*
* @return void
*/
public function testToCakeFileStream()
{
$stream = new Stream('file://' . __FILE__, 'rb');
$psr = new PsrResponse($stream, 200, ['Content-Disposition' => 'attachment; filename="test.php"']);
$result = ResponseTransformer::toCake($psr);

$this->assertNotEmpty($result->getFile(), 'Should convert file responses.');

$headers = $result->header();
$this->assertArrayHasKey('Content-Length', $headers);
$this->assertArrayHasKey('Content-Disposition', $headers);
$this->assertArrayHasKey('Content-Transfer-Encoding', $headers);
$this->assertArrayHasKey('Accept-Ranges', $headers);
$this->assertEquals('attachment; filename="test.php"', $headers['Content-Disposition']);
}

/**
* Test conversion getting cookies.
*
Expand Down
14 changes: 14 additions & 0 deletions tests/TestCase/TestSuite/IntegrationTestCaseTest.php
Expand Up @@ -745,6 +745,20 @@ public function testSendFile()
$this->assertFileResponse(TEST_APP . 'TestApp' . DS . 'Controller' . DS . 'PostsController.php');
}

/**
* Test sending file with psr7 stack
*
* @return void
*/
public function testSendFileHttpServer()
{
DispatcherFactory::clear();
$this->useHttpServer(true);

$this->get('/posts/file');
$this->assertFileResponse(TEST_APP . 'TestApp' . DS . 'Controller' . DS . 'PostsController.php');
}

/**
* Test that assertFile requires a response
*
Expand Down

0 comments on commit ca87223

Please sign in to comment.