From ddba9cf9cc02097ad2212ed0a17b30a6124e0bae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cla=CC=81udio=20Sa=CC=81=20de=20Castro?= Date: Mon, 31 Dec 2018 10:05:29 -0200 Subject: [PATCH 1/2] Converts "wss" scheme into "https" scheme instead of always using "http" scheme and does not add unnecessary default ports when generating request. --- src/Connector.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Connector.php b/src/Connector.php index e44d938..b80bc9c 100644 --- a/src/Connector.php +++ b/src/Connector.php @@ -119,11 +119,7 @@ protected function generateRequest($url, array $subProtocols, array $headers) { throw new \InvalidArgumentException(sprintf('Cannot connect to invalid URL (%s)', $url)); } - $uri = $uri->withScheme('HTTP'); - - if (!$uri->getPort()) { - $uri = $uri->withPort('wss' === $scheme ? 443 : 80); - } + $uri = $uri->withScheme('wss' === $scheme ? 'HTTPS' : 'HTTP'); $headers += ['User-Agent' => 'Ratchet-Pawl/0.3']; From 00a49ef612aa62d186f16429353edd1eae6583f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cla=CC=81udio=20Sa=CC=81=20de=20Castro?= Date: Mon, 31 Dec 2018 12:44:01 -0200 Subject: [PATCH 2/2] Add test case for validating conversion of WebSocket URI into corresponding HTTP/S request URI. --- tests/unit/RequestUriTest.php | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 tests/unit/RequestUriTest.php diff --git a/tests/unit/RequestUriTest.php b/tests/unit/RequestUriTest.php new file mode 100644 index 0000000..0b30240 --- /dev/null +++ b/tests/unit/RequestUriTest.php @@ -0,0 +1,41 @@ +getMethod($methodName); + $method->setAccessible(true); + return $method; + } + + function uriDataProvider() { + return [ + ['ws://127.0.0.1/bla', 'http://127.0.0.1/bla'], + ['wss://127.0.0.1/bla', 'https://127.0.0.1/bla'], + ['ws://127.0.0.1:1234/bla', 'http://127.0.0.1:1234/bla'], + ['wss://127.0.0.1:4321/bla', 'https://127.0.0.1:4321/bla'] + ]; + } + + /** + * @dataProvider uriDataProvider + */ + function testGeneratedRequestUri($uri, $expectedRequestUri) { + $loop = Factory::create(); + + $connector = new Connector($loop); + + $generateRequest = self::getPrivateClassMethod('\Ratchet\Client\Connector', 'generateRequest'); + $request = $generateRequest->invokeArgs($connector, [$uri, [], []]); + + $this->assertEquals((string)$request->getUri(), $expectedRequestUri); + } +} \ No newline at end of file