Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/40'
Browse files Browse the repository at this point in the history
Close #40
  • Loading branch information
weierophinney committed Dec 20, 2018
2 parents 95d89f0 + 681f4ab commit 1e06a77
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- Nothing.
- [#40](https://github.com/zendframework/zend-psr7bridge/pull/40) fixes how headers are translated from PSR-7 to zend-http. Previously, they
were always cast to `GenericHeader` instances; now, the bridge uses `Psr7Response::psr7HeadersToString`
to pass them to `Zend\Http\Headers::fromString()`, ensuring that the more
specific zend-http `HeaderInterface` instance types are created.

## 1.1.0 - 2018-09-27

Expand Down
12 changes: 1 addition & 11 deletions src/Psr7Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,10 @@ public static function toZend(ResponseInterface $psr7Response)
return ZendResponse::fromString($response);
}

// it's a real file stream:
$response = new ZendResponse\Stream();

// copy the headers
$zendHeaders = new Headers();
foreach ($psr7Response->getHeaders() as $headerName => $headerValues) {
$zendHeaders->addHeader(new GenericHeader($headerName, implode('; ', $headerValues)));
}

// set the status
$zendHeaders = Headers::fromString(self::psr7HeadersToString($psr7Response));
$response->setStatusCode($psr7Response->getStatusCode());
// set the headers
$response->setHeaders($zendHeaders);
// set the stream
$response->setStream(fopen($uri, 'rb'));

return $response;
Expand Down
16 changes: 16 additions & 0 deletions test/Psr7ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
namespace ZendTest\Psr7Bridge;

use Error;
use Iterator;
use PHPUnit\Framework\TestCase as TestCase;
use Psr\Http\Message\ResponseInterface;
use Zend\Diactoros\Response;
use Zend\Diactoros\Stream;
use Zend\Http\Header\SetCookie;
use Zend\Http\Response as ZendResponse;
use Zend\Psr7Bridge\Psr7Response;

Expand Down Expand Up @@ -144,4 +146,18 @@ public function testPrivateConstruct()
$this->expectExceptionMessage(sprintf('Call to private %s::__construct', Psr7Response::class));
new Psr7Response();
}

public function testConvertedHeadersAreInstanceOfTheirAppropriateClasses()
{
$psr7Response = (new Response(tmpfile()))->withAddedHeader('Set-Cookie', 'foo=bar;domain=.zendframework.com');
$zendResponse = Psr7Response::toZend($psr7Response);

$cookies = $zendResponse->getHeaders()->get('Set-Cookie');
$this->assertInstanceOf(Iterator::class, $cookies);
$this->assertCount(1, $cookies);
/** @var SetCookie $cookie */
$cookie = $cookies[0];
$this->assertInstanceOf(SetCookie::class, $cookie);
$this->assertSame('.zendframework.com', $cookie->getDomain());
}
}

0 comments on commit 1e06a77

Please sign in to comment.