This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
321 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @see http://github.com/zendframework/zend-diactoros for the canonical source repository | ||
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License | ||
*/ | ||
|
||
namespace Zend\Psr7Bridge; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Zend\Http\Response as ZendResponse; | ||
use Zend\Diactoros\Response; | ||
use Zend\Diactoros\Stream; | ||
|
||
final class Psr7Response | ||
{ | ||
/** | ||
* Convert a PSR-7 response in a Zend\Http\Response | ||
* | ||
* @param ResponseInterface $psr7Response | ||
* @return ZendResponse | ||
*/ | ||
public static function toZend(ResponseInterface $psr7Response) | ||
{ | ||
$response = sprintf( | ||
"HTTP/%s %d %s\r\n%s\r\n%s", | ||
$psr7Response->getProtocolVersion(), | ||
$psr7Response->getStatusCode(), | ||
$psr7Response->getReasonPhrase(), | ||
self::psr7HeadersToString($psr7Response), | ||
(string) $psr7Response->getBody() | ||
); | ||
return ZendResponse::fromString($response); | ||
} | ||
|
||
/** | ||
* Convert a Zend\Http\Response in a PSR-7 response, using zend-diactoros | ||
* | ||
* @param ZendResponse $zendResponse | ||
* @return Response | ||
*/ | ||
public static function fromZend(ZendResponse $zendResponse) | ||
{ | ||
$body = new Stream('php://temp', 'wb+'); | ||
$body->write($zendResponse->getBody()); | ||
|
||
return new Response( | ||
$body, | ||
$zendResponse->getStatusCode(), | ||
$zendResponse->getHeaders()->toArray() | ||
); | ||
} | ||
|
||
/** | ||
* Convert the PSR-7 headers to string | ||
* | ||
* @param ResponseInterface $psr7Response | ||
* @return string | ||
*/ | ||
private static function psr7HeadersToString(ResponseInterface $psr7Response) | ||
{ | ||
$headers = ''; | ||
foreach ($psr7Response->getHeaders() as $name => $value) { | ||
$headers .= $name . ": " . implode(", ", $value) . "\r\n"; | ||
} | ||
return $headers; | ||
} | ||
|
||
/** | ||
* Do not allow instantiation. | ||
*/ | ||
private function __construct() | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @see http://github.com/zendframework/zend-diactoros for the canonical source repository | ||
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License | ||
*/ | ||
|
||
namespace ZendTest\Psr7Bridge; | ||
|
||
use PHPUnit_Framework_TestCase as TestCase; | ||
use Zend\Diactoros\Response; | ||
use Zend\Diactoros\Stream; | ||
use Zend\Psr7Bridge\Psr7Response; | ||
use Zend\Http\Response as ZendResponse; | ||
|
||
class Psr7ResponseTest extends TestCase | ||
{ | ||
public function getResponseData() | ||
{ | ||
return [ | ||
[ 'Test!', 200, [ 'Content-Type' => [ 'text/html' ] ] ], | ||
[ '', 204, [] ], | ||
[ 'Test!', 200, [ | ||
'Content-Type' => [ 'text/html; charset=utf-8' ], | ||
'Content-Length' => [ '5' ] | ||
]], | ||
[ 'Test!', 202, [ | ||
'Content-Type' => [ 'text/html; level=1', 'text/html' ], | ||
'Content-Length' => [ '5' ] | ||
]], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider getResponseData | ||
*/ | ||
public function testResponseToZend($body, $status, $headers) | ||
{ | ||
$stream = new Stream('php://temp', 'wb+'); | ||
$stream->write($body); | ||
|
||
$psr7Response = new Response($stream, $status, $headers); | ||
$this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $psr7Response); | ||
|
||
$zendResponse = Psr7Response::toZend($psr7Response); | ||
$this->assertInstanceOf('Zend\Http\Response', $zendResponse); | ||
$this->assertEquals($body, (string) $zendResponse->getBody()); | ||
$this->assertEquals($status, $zendResponse->getStatusCode()); | ||
|
||
$zendHeaders = $zendResponse->getHeaders()->toArray(); | ||
foreach ($headers as $type => $values) { | ||
foreach ($values as $value) { | ||
$this->assertContains($value, $zendHeaders[$type]); | ||
} | ||
} | ||
} | ||
|
||
public function getResponseString() | ||
{ | ||
return [ | ||
[ "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\nTest!" ], | ||
[ "HTTP/1.1 204 OK\r\n\r\n" ], | ||
[ "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: 5\r\n\r\nTest!" ], | ||
[ "HTTP/1.1 200 OK\r\nContent-Type: text/html, text/xml\r\nContent-Length: 5\r\n\r\nTest!" ], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider getResponseString | ||
*/ | ||
public function testResponseFromZend($response) | ||
{ | ||
$zendResponse = ZendResponse::fromString($response); | ||
$this->assertInstanceOf('Zend\Http\Response', $zendResponse); | ||
$psr7Response = Psr7Response::fromZend($zendResponse); | ||
$this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $psr7Response); | ||
$this->assertEquals((string) $psr7Response->getBody(), $zendResponse->getBody()); | ||
$this->assertEquals($psr7Response->getStatusCode(), $zendResponse->getStatusCode()); | ||
|
||
$zendHeaders = $zendResponse->getHeaders()->toArray(); | ||
foreach ($psr7Response->getHeaders() as $type => $values) { | ||
foreach ($values as $value) { | ||
$this->assertContains($value, $zendHeaders[$type]); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters