diff --git a/composer.json b/composer.json index 324f092..3e9cf21 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "codeinc/psr7-responses", - "version": "2.0.1", + "version": "2.0.2", "description": "A collection of PSR-7 responses", "homepage": "https://github.com/CodeIncHQ/Psr7Responses", "type": "library", @@ -15,7 +15,8 @@ }, "require-dev": { "phpunit/phpunit": "^7", - "codeinc/psr7-response-sender": "^1.1" + "codeinc/psr7-response-sender": "^1.1", + "guzzlehttp/guzzle": "^6" }, "autoload": { "psr-4": {"CodeInc\\Psr7Responses\\": "src/"} @@ -35,7 +36,8 @@ "codeinc/psr7-response-sender": "A library to send PSR-7 responses to the web browser", "codeinc/psr15-middlewares": "Provides a collection of PSR15 middlewares", "hansott/psr7-cookies": "Adds cookies into a PSR7 response", - "micheh/psr7-cache": "Adds cache HTTP headers to responses" + "micheh/psr7-cache": "Adds cache HTTP headers to responses", + "guzzlehttp/guzzle": "Required in order to use HttpProxyResponse" }, "config": { "preferred-install": { diff --git a/src/HttpProxyResponse.php b/src/HttpProxyResponse.php index 5f4d574..b1be307 100644 --- a/src/HttpProxyResponse.php +++ b/src/HttpProxyResponse.php @@ -22,9 +22,9 @@ declare(strict_types=1); namespace CodeInc\Psr7Responses; use CodeInc\Psr7Responses\Tests\HttpProxyResponseTest; +use GuzzleHttp\Client; use GuzzleHttp\Psr7\Response; -use GuzzleHttp\Psr7\Stream; -use function GuzzleHttp\Psr7\stream_for; +use Psr\Http\Message\ResponseInterface; /** @@ -39,7 +39,20 @@ */ class HttpProxyResponse extends Response { - protected const IMPORT_HEADERS = [ + /** + * @var string + */ + private $remoteUrl; + + /** + * @var ResponseInterface + */ + private $response; + + /** + * @var string[] + */ + private $acceptableResponseHeaders = [ 'content-type', 'content-length', 'content-disposition', @@ -51,22 +64,15 @@ class HttpProxyResponse extends Response 'cache-control' ]; - /** - * @var string - */ - private $remoteUrl; - /** * ProxyResponse constructor. * * @param string $remoteUrl - * @param int $code - * @param string $reasonPhrase * @param array $headers * @param string $version + * @throws \GuzzleHttp\Exception\GuzzleException */ - public function __construct(string $remoteUrl, int $code = 200, string $reasonPhrase = '', - array $headers = [], string $version = '1.1') + public function __construct(string $remoteUrl, array $headers = [], string $version = '1.1') { if (!filter_var($remoteUrl, FILTER_VALIDATE_URL)) { throw new ResponseException( @@ -77,53 +83,68 @@ public function __construct(string $remoteUrl, int $code = 200, string $reasonPh $this->remoteUrl = $remoteUrl; parent::__construct( - $code, - $this->importHttpHeader($headers), - $this->getStream(), + $this->getResponse()->getStatusCode(), + $this->getResponseHeaders() + $headers, + $this->getResponse()->getBody(), $version, - $reasonPhrase + $this->getResponse()->getReasonPhrase() ); } /** - * Returns the remote URL. + * Returns the HTTP response. * - * @return string + * @return ResponseInterface + * @throws \GuzzleHttp\Exception\GuzzleException */ - public function getRemoteUrl():string + public function getResponse():ResponseInterface { - return $this->remoteUrl; + if (!$this->response) { + $this->response = (new Client())->request('GET', $this->remoteUrl); + } + return $this->response; } /** - * @return Stream - * @throws ResponseException + * Returns all the imported headers from the HTTP response. + * + * @return array + * @throws \GuzzleHttp\Exception\GuzzleException */ - private function getStream():Stream + public function getResponseHeaders():array { - $context = stream_context_create(['http' => ['method' => 'GET']]); - if (($f = fopen($this->remoteUrl, 'r', false, $context)) === false) { - throw new ResponseException( - sprintf("Unable to open the URL %s", $this->remoteUrl), - $this - ); + $headers = []; + foreach ($this->getResponse()->getHeaders() as $header => $values) { + if (in_array(strtolower($header), $this->acceptableResponseHeaders)) { + $headers[$header] = $values; + } } - return stream_for($f); + return $headers; } /** - * @param array $headers - * @return array + * @return string[] */ - private function importHttpHeader(array $headers):array + public function getAcceptableResponseHeaders():array { - foreach ($http_response_header as $header) { - if (preg_match('/^([\\w-]+): +(.+)$/ui', $header, $matches)) { - if (in_array(strtolower($matches[1]), self::IMPORT_HEADERS)) { - $headers[$matches[1]] = $matches[2]; - } - } - } - return $headers; + return $this->acceptableResponseHeaders; + } + + /** + * @param string[] $acceptableResponseHeaders + */ + public function setAcceptableResponseHeaders(array $acceptableResponseHeaders):void + { + $this->acceptableResponseHeaders = $acceptableResponseHeaders; + } + + /** + * Returns the remote URL. + * + * @return string + */ + public function getRemoteUrl():string + { + return $this->remoteUrl; } } \ No newline at end of file diff --git a/tests/DebugResponseTest.php b/tests/DebugResponseTest.php deleted file mode 100644 index 161b622..0000000 --- a/tests/DebugResponseTest.php +++ /dev/null @@ -1,45 +0,0 @@ - -// Date: 03/05/2018 -// Time: 12:33 -// Project: Psr7Responses -// -declare(strict_types=1); -namespace CodeInc\Psr7Responses\Tests; -use CodeInc\Psr7Responses\DebugResponse; - - -/** - * Class DebugResponseText - * - * @uses DebugResponse - * @package CodeInc\Psr7Responses\Tests - * @author Joan Fabrégat - * @license MIT - * @link https://github.com/CodeIncHQ/Psr7Responses - */ -class DebugResponseTest extends AbstractResponseTestCase -{ - public function test():void - { - $response = new DebugResponse(["test" => 1]); - self::assertIsResponse($response); - self::assertResponseStatusCode(200, $response); - self::assertResponseHasBody($response); - } -} \ No newline at end of file diff --git a/tests/ErrorResponseTest.php b/tests/ErrorResponseTest.php index 3161465..2b7851f 100644 --- a/tests/ErrorResponseTest.php +++ b/tests/ErrorResponseTest.php @@ -36,6 +36,9 @@ */ class ErrorResponseTest extends AbstractResponseTestCase { + /** + * @throws \ReflectionException + */ public function test():void { $response = new ErrorResponse(new FakeException('Test'));