diff --git a/.travis.yml b/.travis.yml index 1c5e371..3f1d11c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,13 +1,12 @@ language: php php: - - 7.1 - - 7.2 - 7.3 + - 7.4 sudo: false -dist: trusty +dist: bionic before_script: - composer self-update diff --git a/README.md b/README.md index f29a549..81238df 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ This is a HTTP/HTTPS proxy script that forwards requests to a different server and returns the response. The Proxy class uses PSR7 request/response objects as input/output, and uses Guzzle to do the actual HTTP request. + ## Installation Install using composer: @@ -34,11 +35,16 @@ $proxy = new Proxy(new GuzzleAdapter($guzzle)); // Add a response filter that removes the encoding headers. $proxy->filter(new RemoveEncodingFilter()); -// Forward the request and get the response. -$response = $proxy->forward($request)->to('http://example.com'); +try { + // Forward the request and get the response. + $response = $proxy->forward($request)->to('http://example.com'); -// Output response to the browser. -(new Laminas\HttpHandlerRunner\Emitter\SapiEmitter)->emit($response); + // Output response to the browser. + (new Laminas\HttpHandlerRunner\Emitter\SapiEmitter)->emit($response); +} catch(\GuzzleHttp\Exception\BadResponseException $e) { + // Correct way to handle bad responses + (new Laminas\HttpHandlerRunner\Emitter\SapiEmitter)->emit($e->getResponse()); +} ``` ## Filters diff --git a/composer.json b/composer.json index d1c56b9..39e983a 100644 --- a/composer.json +++ b/composer.json @@ -16,17 +16,18 @@ } ], "require": { - "php": "^5.6 || ^7.0", + "php": "^7.3 || ^8.0", "psr/http-message": "^1.0", - "guzzlehttp/guzzle": "^6.0", - "laminas/laminas-diactoros": "^2.0", + "guzzlehttp/guzzle": "^7.1", + "laminas/laminas-diactoros": "^2.5", "relay/relay": "^1.0", - "laminas/laminas-httphandlerrunner": "^1.1" + "laminas/laminas-httphandlerrunner": "^1.2" }, "require-dev": { - "phpunit/phpunit": "^5.0|^6.0|^7.0", - "php-coveralls/php-coveralls": "^2.0", - "mockery/mockery": "^1.1" + "roave/security-advisories": "dev-latest", + "phpunit/phpunit": "^9.5", + "php-coveralls/php-coveralls": "^2.4", + "mockery/mockery": "^1.4" }, "autoload": { "psr-4": { diff --git a/phpunit.xml b/phpunit.xml index 8034fdf..eb45487 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,22 +1,23 @@ - - - - tests/Proxy/ - - - - - ./src - - + + + + ./src + + + + + tests/Proxy/ + + diff --git a/src/Proxy.php b/src/Proxy.php index 630501f..42d4bfb 100644 --- a/src/Proxy.php +++ b/src/Proxy.php @@ -2,6 +2,7 @@ namespace Proxy; +use GuzzleHttp\Exception\ClientException; use Proxy\Adapter\AdapterInterface; use Proxy\Exception\UnexpectedValueException; use Psr\Http\Message\RequestInterface; @@ -79,7 +80,11 @@ public function to($target) $stack = $this->filters; $stack[] = function (RequestInterface $request, ResponseInterface $response, callable $next) { - $response = $this->adapter->send($request); + try { + $response = $this->adapter->send($request); + } catch (ClientException $ex) { + $response = $ex->getResponse(); + } return $next($request, $response); }; diff --git a/tests/Proxy/Adapter/Dummy/DummyAdapterTest.php b/tests/Proxy/Adapter/Dummy/DummyAdapterTest.php index 62a869b..a4a52cc 100644 --- a/tests/Proxy/Adapter/Dummy/DummyAdapterTest.php +++ b/tests/Proxy/Adapter/Dummy/DummyAdapterTest.php @@ -13,7 +13,7 @@ class DummyAdapterTest extends TestCase */ private $adapter; - public function setUp() + public function setUp(): void { $this->adapter = new DummyAdapter(); } diff --git a/tests/Proxy/Adapter/Guzzle/GuzzleAdapterTest.php b/tests/Proxy/Adapter/Guzzle/GuzzleAdapterTest.php index 9f9940b..0bdc338 100644 --- a/tests/Proxy/Adapter/Guzzle/GuzzleAdapterTest.php +++ b/tests/Proxy/Adapter/Guzzle/GuzzleAdapterTest.php @@ -32,7 +32,7 @@ class GuzzleAdapterTest extends TestCase */ private $body = 'Totally awesome response body'; - public function setUp() + public function setUp(): void { $mock = new MockHandler([ $this->createResponse(), diff --git a/tests/Proxy/Filter/RemoveEncodingFilterTest.php b/tests/Proxy/Filter/RemoveEncodingFilterTest.php index c407695..7a397ca 100644 --- a/tests/Proxy/Filter/RemoveEncodingFilterTest.php +++ b/tests/Proxy/Filter/RemoveEncodingFilterTest.php @@ -13,7 +13,7 @@ class RemoveEncodingFilterTest extends TestCase */ private $filter; - public function setUp() + public function setUp(): void { $this->filter = new RemoveEncodingFilter(); } diff --git a/tests/Proxy/Filter/RemoveLocationFilterTest.php b/tests/Proxy/Filter/RemoveLocationFilterTest.php index ea96bde..a9a4971 100644 --- a/tests/Proxy/Filter/RemoveLocationFilterTest.php +++ b/tests/Proxy/Filter/RemoveLocationFilterTest.php @@ -13,7 +13,7 @@ class RemoveLocationFilterTest extends TestCase */ private $filter; - public function setUp() + public function setUp(): void { $this->filter = new RemoveLocationFilter(); } diff --git a/tests/Proxy/Filter/RewriteLocationFilterTest.php b/tests/Proxy/Filter/RewriteLocationFilterTest.php index 3bcc5f4..c67b71e 100644 --- a/tests/Proxy/Filter/RewriteLocationFilterTest.php +++ b/tests/Proxy/Filter/RewriteLocationFilterTest.php @@ -11,7 +11,7 @@ class RewriteLocationFilterTest extends TestCase */ private $filter; - public function setUp() + public function setUp(): void { $this->filter = new RewriteLocationFilter(); } diff --git a/tests/Proxy/ProxyTest.php b/tests/Proxy/ProxyTest.php index b46017e..37f4359 100644 --- a/tests/Proxy/ProxyTest.php +++ b/tests/Proxy/ProxyTest.php @@ -17,17 +17,17 @@ class ProxyTest extends TestCase */ private $proxy; - public function setUp() + public function setUp(): void { $this->proxy = new Proxy(new DummyAdapter()); } /** * @test - * @expectedException UnexpectedValueException */ public function to_throws_exception_if_no_request_is_given() { + $this->expectException('UnexpectedValueException'); $this->proxy->to('http://www.example.com'); }