From 59f63090ddd141e7d279f4e3d08638e2ccb573ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Wed, 4 Aug 2021 19:06:48 +0200 Subject: [PATCH] Simplify usage by supporting new Socket API without nullable loop --- README.md | 18 +++++------ composer.json | 6 ++-- examples/01-http-request.php | 4 +-- examples/02-optional-proxy-http-request.php | 4 +-- examples/11-proxy-raw-https-protocol.php | 2 +- .../12-optional-proxy-raw-https-protocol.php | 2 +- examples/13-custom-proxy-headers.php | 2 +- examples/21-proxy-raw-smtp-protocol.php | 2 +- examples/22-proxy-raw-smtps-protocol.php | 2 +- tests/AbstractTestCase.php | 4 +-- tests/FunctionalTest.php | 31 ++++++------------- tests/ProxyConnectorTest.php | 2 +- 12 files changed, 32 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index ac9eb75..a4ffe14 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ secure HTTPS request to google.com through a local HTTP proxy server: ```php $proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080'); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'timeout' => 3.0, 'dns' => false @@ -115,7 +115,7 @@ proxy servers etc.), you can explicitly pass a custom instance of the [`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface): ```php -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'dns' => '127.0.0.1', 'tcp' => array( 'bindto' => '192.168.10.1:0' @@ -175,7 +175,7 @@ in ReactPHP's [`Connector`](https://github.com/reactphp/socket#connector): ```php $proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080'); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'dns' => false )); @@ -201,7 +201,7 @@ ReactPHP's [`Connector`](https://github.com/reactphp/socket#connector): ```php $proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080'); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'dns' => false )); @@ -228,12 +228,12 @@ This allows you to send both plain HTTP and TLS-encrypted HTTPS requests like th ```php $proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080'); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'dns' => false )); -$browser = new React\Http\Browser(null, $connector); +$browser = new React\Http\Browser($connector); $browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) { var_dump($response->getHeaders(), (string) $response->getBody()); @@ -263,7 +263,7 @@ underlying connection attempt if it takes too long: ```php $proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080'); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'dns' => false, 'timeout' => 3.0 @@ -307,7 +307,7 @@ other examples explicitly disable DNS resolution like this: ```php $proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080'); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'dns' => false )); @@ -319,7 +319,7 @@ If you want to explicitly use *local DNS resolution*, you can use the following $proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080'); // set up Connector which uses Google's public DNS (8.8.8.8) -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'dns' => '8.8.8.8' )); diff --git a/composer.json b/composer.json index 6e427a9..13f6e0e 100644 --- a/composer.json +++ b/composer.json @@ -19,13 +19,13 @@ "require": { "php": ">=5.3", "react/promise": " ^2.1 || ^1.2.1", - "react/socket": "^1.8", + "react/socket": "^1.9", "ringcentral/psr7": "^1.2" }, "require-dev": { + "clue/block-react": "^1.1", "phpunit/phpunit": "^9.3 || ^5.7 || ^4.8", "react/event-loop": "^1.2", - "react/http": "^1.4", - "clue/block-react": "^1.1" + "react/http": "^1.5" } } diff --git a/examples/01-http-request.php b/examples/01-http-request.php index a6bc6c0..4bbbbf4 100644 --- a/examples/01-http-request.php +++ b/examples/01-http-request.php @@ -23,12 +23,12 @@ $proxy = new Clue\React\HttpProxy\ProxyConnector($url); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'dns' => false )); -$browser = new React\Http\Browser(null, $connector); +$browser = new React\Http\Browser($connector); $browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) { var_dump($response->getHeaders(), (string) $response->getBody()); diff --git a/examples/02-optional-proxy-http-request.php b/examples/02-optional-proxy-http-request.php index d018cea..be340c3 100644 --- a/examples/02-optional-proxy-http-request.php +++ b/examples/02-optional-proxy-http-request.php @@ -20,14 +20,14 @@ if ($url !== false) { $proxy = new Clue\React\HttpProxy\ProxyConnector($url); - $connector = new React\Socket\Connector(null, array( + $connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'timeout' => 3.0, 'dns' => false )); } -$browser = new React\Http\Browser(null, $connector); +$browser = new React\Http\Browser($connector); $browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) { var_dump($response->getHeaders(), (string) $response->getBody()); diff --git a/examples/11-proxy-raw-https-protocol.php b/examples/11-proxy-raw-https-protocol.php index 1c98cc2..1764e99 100644 --- a/examples/11-proxy-raw-https-protocol.php +++ b/examples/11-proxy-raw-https-protocol.php @@ -26,7 +26,7 @@ $proxy = new Clue\React\HttpProxy\ProxyConnector($url); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'timeout' => 3.0, 'dns' => false diff --git a/examples/12-optional-proxy-raw-https-protocol.php b/examples/12-optional-proxy-raw-https-protocol.php index 873f963..b41246f 100644 --- a/examples/12-optional-proxy-raw-https-protocol.php +++ b/examples/12-optional-proxy-raw-https-protocol.php @@ -26,7 +26,7 @@ if ($url !== false) { $proxy = new Clue\React\HttpProxy\ProxyConnector($url); - $connector = new React\Socket\Connector(null, array( + $connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'timeout' => 3.0, 'dns' => false diff --git a/examples/13-custom-proxy-headers.php b/examples/13-custom-proxy-headers.php index 5314765..56d3622 100644 --- a/examples/13-custom-proxy-headers.php +++ b/examples/13-custom-proxy-headers.php @@ -33,7 +33,7 @@ ) ); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'timeout' => 3.0, 'dns' => false, diff --git a/examples/21-proxy-raw-smtp-protocol.php b/examples/21-proxy-raw-smtp-protocol.php index 4feeea9..98e262a 100644 --- a/examples/21-proxy-raw-smtp-protocol.php +++ b/examples/21-proxy-raw-smtp-protocol.php @@ -25,7 +25,7 @@ $proxy = new Clue\React\HttpProxy\ProxyConnector($url); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'timeout' => 3.0, 'dns' => false diff --git a/examples/22-proxy-raw-smtps-protocol.php b/examples/22-proxy-raw-smtps-protocol.php index 296390a..321507a 100644 --- a/examples/22-proxy-raw-smtps-protocol.php +++ b/examples/22-proxy-raw-smtps-protocol.php @@ -28,7 +28,7 @@ $proxy = new Clue\React\HttpProxy\ProxyConnector($url); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'timeout' => 3.0, 'dns' => false diff --git a/tests/AbstractTestCase.php b/tests/AbstractTestCase.php index 85eb676..72bb151 100644 --- a/tests/AbstractTestCase.php +++ b/tests/AbstractTestCase.php @@ -4,7 +4,7 @@ use PHPUnit\Framework\TestCase; -abstract class AbstractTestCase extends \PHPUnit\Framework\TestCase +abstract class AbstractTestCase extends TestCase { protected function expectCallableNever() { @@ -77,6 +77,4 @@ public function setExpectedException($exception, $exceptionMessage = '', $except parent::setExpectedException($exception, $exceptionMessage, $exceptionCode); } } - } - diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index bbf60ba..38605db 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -4,27 +4,14 @@ use Clue\React\Block; use Clue\React\HttpProxy\ProxyConnector; -use React\EventLoop\Factory; -use React\Socket\Connector; +use React\EventLoop\Loop; /** @group internet */ class FunctionalTest extends AbstractTestCase { - private $loop; - private $connector; - - /** - * @before - */ - public function setUpConnector() - { - $this->loop = Factory::create(); - $this->connector = new Connector($this->loop); - } - public function testNonListeningSocketRejectsConnection() { - $proxy = new ProxyConnector('127.0.0.1:9999', $this->connector); + $proxy = new ProxyConnector('127.0.0.1:9999'); $promise = $proxy->connect('google.com:80'); @@ -33,12 +20,12 @@ public function testNonListeningSocketRejectsConnection() 'Connection to tcp://google.com:80 failed because connection to proxy failed (ECONNREFUSED)', defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111 ); - Block\await($promise, $this->loop, 3.0); + Block\await($promise, Loop::get(), 3.0); } public function testPlainGoogleDoesNotAcceptConnectMethod() { - $proxy = new ProxyConnector('google.com', $this->connector); + $proxy = new ProxyConnector('google.com'); $promise = $proxy->connect('google.com:80'); @@ -47,7 +34,7 @@ public function testPlainGoogleDoesNotAcceptConnectMethod() 'Connection to tcp://google.com:80 failed because proxy refused connection with HTTP error code 405 (Method Not Allowed) (ECONNREFUSED)', defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111 ); - Block\await($promise, $this->loop, 3.0); + Block\await($promise, Loop::get(), 3.0); } public function testSecureGoogleDoesNotAcceptConnectMethod() @@ -56,7 +43,7 @@ public function testSecureGoogleDoesNotAcceptConnectMethod() $this->markTestSkipped('TLS not supported on legacy HHVM'); } - $proxy = new ProxyConnector('https://google.com:443', $this->connector); + $proxy = new ProxyConnector('https://google.com:443'); $promise = $proxy->connect('google.com:80'); @@ -65,7 +52,7 @@ public function testSecureGoogleDoesNotAcceptConnectMethod() 'Connection to tcp://google.com:80 failed because proxy refused connection with HTTP error code 405 (Method Not Allowed) (ECONNREFUSED)', defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111 ); - Block\await($promise, $this->loop, 3.0); + Block\await($promise, Loop::get(), 3.0); } public function testSecureGoogleDoesNotAcceptPlainStream() @@ -79,7 +66,7 @@ public function testSecureGoogleDoesNotAcceptPlainStream() 'Connection to tcp://google.com:80 failed because connection to proxy was lost while waiting for response (ECONNRESET)', defined('SOCKET_ECONNRESET') ? SOCKET_ECONNRESET : 104 ); - Block\await($promise, $this->loop, 3.0); + Block\await($promise, Loop::get(), 3.0); } /** @@ -87,7 +74,7 @@ public function testSecureGoogleDoesNotAcceptPlainStream() */ public function testCancelWhileConnectingShouldNotCreateGarbageCycles() { - $proxy = new ProxyConnector('google.com', $this->connector); + $proxy = new ProxyConnector('google.com'); gc_collect_cycles(); gc_collect_cycles(); // clear twice to avoid leftovers in PHP 7.4 with ext-xdebug and code coverage turned on diff --git a/tests/ProxyConnectorTest.php b/tests/ProxyConnectorTest.php index a92a5c2..6272d6a 100644 --- a/tests/ProxyConnectorTest.php +++ b/tests/ProxyConnectorTest.php @@ -3,9 +3,9 @@ namespace Clue\Tests\React\HttpProxy; use Clue\React\HttpProxy\ProxyConnector; -use React\Promise\Promise; use React\Socket\ConnectionInterface; use React\Promise\Deferred; +use React\Promise\Promise; class ProxyConnectorTest extends AbstractTestCase {