From 3e0a8a4d735a3f64c27c299b17d26fff08e8cc91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Jensen?= Date: Mon, 7 Nov 2022 19:53:36 +0100 Subject: [PATCH 1/2] Fix for default ports --- lib/Client.php | 1 + tests/ClientTest.php | 20 +++++++ .../client.connect-default-port-ws.json | 59 +++++++++++++++++++ .../client.connect-default-port-wss.json | 59 +++++++++++++++++++ 4 files changed, 139 insertions(+) create mode 100644 tests/scripts/client.connect-default-port-ws.json create mode 100644 tests/scripts/client.connect-default-port-wss.json diff --git a/lib/Client.php b/lib/Client.php index 2cf2136..1f46ab1 100644 --- a/lib/Client.php +++ b/lib/Client.php @@ -307,6 +307,7 @@ protected function connect(): void $host_uri = $this->socket_uri ->withScheme($this->socket_uri->getScheme() == 'wss' ? 'ssl' : 'tcp') + ->withPort($this->socket_uri->getPort() ?? ($this->socket_uri->getScheme() == 'wss' ? 443 : 80)) ->withPath('') ->withQuery('') ->withFragment('') diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 654d621..df23d75 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -82,6 +82,26 @@ public function testClientRelativePath(): void $this->assertTrue(MockSocket::isEmpty()); } + public function testClientWsDefaultPort(): void + { + MockSocket::initialize('client.connect-default-port-ws', $this); + $uri = new Uri('ws://localhost'); + $uri = $uri->withPath('my/mock/path'); + $client = new Client($uri); + $client->send('Connect'); + $this->assertTrue(MockSocket::isEmpty()); + } + + public function testClientWssDefaultPort(): void + { + MockSocket::initialize('client.connect-default-port-wss', $this); + $uri = new Uri('wss://localhost'); + $uri = $uri->withPath('my/mock/path'); + $client = new Client($uri); + $client->send('Connect'); + $this->assertTrue(MockSocket::isEmpty()); + } + public function testClientWithTimeout(): void { MockSocket::initialize('client.connect-timeout', $this); diff --git a/tests/scripts/client.connect-default-port-ws.json b/tests/scripts/client.connect-default-port-ws.json new file mode 100644 index 0000000..4fbd6b0 --- /dev/null +++ b/tests/scripts/client.connect-default-port-ws.json @@ -0,0 +1,59 @@ +[ + { + "function": "stream_context_create", + "params": [], + "return": "@mock-stream-context" + }, + { + "function": "stream_socket_client", + "params": [ + "tcp:\/\/localhost:80", + null, + null, + 5, + 4, + "@mock-stream-context" + ], + "return": "@mock-stream" + }, + { + "function": "get_resource_type", + "params": [ + "@mock-stream" + ], + "return": "stream" + }, + { + "function": "stream_set_timeout", + "params": [ + "@mock-stream", + 5 + ], + "return": true + }, + { + "function": "fwrite", + "params": [ + "@mock-stream", + "GET /my/mock/path HTTP/1.1\r\nHost: localhost:80\r\nUser-Agent: websocket-client-php\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Key: {key}\r\nSec-WebSocket-Version: 13\r\n\r\n" + ], + "input-op": "key-save", + "return": 224 + }, + { + "function": "fgets", + "params": [ + "@mock-stream", + 1024 + ], + "return-op": "key-respond", + "return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}\r\n\r\n" + }, + { + "function": "fwrite", + "params": [ + "@mock-stream" + ], + "return": 13 + } +] \ No newline at end of file diff --git a/tests/scripts/client.connect-default-port-wss.json b/tests/scripts/client.connect-default-port-wss.json new file mode 100644 index 0000000..c17c9cd --- /dev/null +++ b/tests/scripts/client.connect-default-port-wss.json @@ -0,0 +1,59 @@ +[ + { + "function": "stream_context_create", + "params": [], + "return": "@mock-stream-context" + }, + { + "function": "stream_socket_client", + "params": [ + "ssl:\/\/localhost:443", + null, + null, + 5, + 4, + "@mock-stream-context" + ], + "return": "@mock-stream" + }, + { + "function": "get_resource_type", + "params": [ + "@mock-stream" + ], + "return": "stream" + }, + { + "function": "stream_set_timeout", + "params": [ + "@mock-stream", + 5 + ], + "return": true + }, + { + "function": "fwrite", + "params": [ + "@mock-stream", + "GET /my/mock/path HTTP/1.1\r\nHost: localhost:443\r\nUser-Agent: websocket-client-php\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Key: {key}\r\nSec-WebSocket-Version: 13\r\n\r\n" + ], + "input-op": "key-save", + "return": 224 + }, + { + "function": "fgets", + "params": [ + "@mock-stream", + 1024 + ], + "return-op": "key-respond", + "return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}\r\n\r\n" + }, + { + "function": "fwrite", + "params": [ + "@mock-stream" + ], + "return": 13 + } +] \ No newline at end of file From 26ee875bd9ccf51318789524e7909fd3a7af7544 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Jensen?= Date: Mon, 7 Nov 2022 19:56:24 +0100 Subject: [PATCH 2/2] Fix issue with implicit default ports --- README.md | 2 +- docs/Changelog.md | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ab55cf2..f4a41a0 100644 --- a/README.md +++ b/README.md @@ -64,4 +64,4 @@ Fredrik Liljegren, Armen Baghumian Sankbarani, Ruslan Bekenev, Joshua Thijssen, Simon Lipp, Quentin Bellus, Patrick McCarren, swmcdonnell, Ignas Bernotas, Mark Herhold, Andreas Palm, Sören Jensen, pmaasz, Alexey Stavrov, Michael Slezak, Pierre Seznec, rmeisler, Nickolay V. Shmyrev, Christoph Kempen, -Marc Roberts, Antonio Mora, Simon Podlipsky. +Marc Roberts, Antonio Mora, Simon Podlipsky, etrinh. diff --git a/docs/Changelog.md b/docs/Changelog.md index d6225bb..38f264e 100644 --- a/docs/Changelog.md +++ b/docs/Changelog.md @@ -6,6 +6,10 @@ > PHP version `^7.4|^8.0` +### `1.6.3` + + * Fix issue with implicit default ports (@etrinh, @sirn-se) + ### `1.6.2` * Fix issue where port was missing in socket uri (@sirn-se)