Skip to content

Commit 3e0a8a4

Browse files
committed
Fix for default ports
1 parent 3f8fd6f commit 3e0a8a4

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed

lib/Client.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ protected function connect(): void
307307

308308
$host_uri = $this->socket_uri
309309
->withScheme($this->socket_uri->getScheme() == 'wss' ? 'ssl' : 'tcp')
310+
->withPort($this->socket_uri->getPort() ?? ($this->socket_uri->getScheme() == 'wss' ? 443 : 80))
310311
->withPath('')
311312
->withQuery('')
312313
->withFragment('')

tests/ClientTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,26 @@ public function testClientRelativePath(): void
8282
$this->assertTrue(MockSocket::isEmpty());
8383
}
8484

85+
public function testClientWsDefaultPort(): void
86+
{
87+
MockSocket::initialize('client.connect-default-port-ws', $this);
88+
$uri = new Uri('ws://localhost');
89+
$uri = $uri->withPath('my/mock/path');
90+
$client = new Client($uri);
91+
$client->send('Connect');
92+
$this->assertTrue(MockSocket::isEmpty());
93+
}
94+
95+
public function testClientWssDefaultPort(): void
96+
{
97+
MockSocket::initialize('client.connect-default-port-wss', $this);
98+
$uri = new Uri('wss://localhost');
99+
$uri = $uri->withPath('my/mock/path');
100+
$client = new Client($uri);
101+
$client->send('Connect');
102+
$this->assertTrue(MockSocket::isEmpty());
103+
}
104+
85105
public function testClientWithTimeout(): void
86106
{
87107
MockSocket::initialize('client.connect-timeout', $this);
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
[
2+
{
3+
"function": "stream_context_create",
4+
"params": [],
5+
"return": "@mock-stream-context"
6+
},
7+
{
8+
"function": "stream_socket_client",
9+
"params": [
10+
"tcp:\/\/localhost:80",
11+
null,
12+
null,
13+
5,
14+
4,
15+
"@mock-stream-context"
16+
],
17+
"return": "@mock-stream"
18+
},
19+
{
20+
"function": "get_resource_type",
21+
"params": [
22+
"@mock-stream"
23+
],
24+
"return": "stream"
25+
},
26+
{
27+
"function": "stream_set_timeout",
28+
"params": [
29+
"@mock-stream",
30+
5
31+
],
32+
"return": true
33+
},
34+
{
35+
"function": "fwrite",
36+
"params": [
37+
"@mock-stream",
38+
"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"
39+
],
40+
"input-op": "key-save",
41+
"return": 224
42+
},
43+
{
44+
"function": "fgets",
45+
"params": [
46+
"@mock-stream",
47+
1024
48+
],
49+
"return-op": "key-respond",
50+
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}\r\n\r\n"
51+
},
52+
{
53+
"function": "fwrite",
54+
"params": [
55+
"@mock-stream"
56+
],
57+
"return": 13
58+
}
59+
]
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
[
2+
{
3+
"function": "stream_context_create",
4+
"params": [],
5+
"return": "@mock-stream-context"
6+
},
7+
{
8+
"function": "stream_socket_client",
9+
"params": [
10+
"ssl:\/\/localhost:443",
11+
null,
12+
null,
13+
5,
14+
4,
15+
"@mock-stream-context"
16+
],
17+
"return": "@mock-stream"
18+
},
19+
{
20+
"function": "get_resource_type",
21+
"params": [
22+
"@mock-stream"
23+
],
24+
"return": "stream"
25+
},
26+
{
27+
"function": "stream_set_timeout",
28+
"params": [
29+
"@mock-stream",
30+
5
31+
],
32+
"return": true
33+
},
34+
{
35+
"function": "fwrite",
36+
"params": [
37+
"@mock-stream",
38+
"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"
39+
],
40+
"input-op": "key-save",
41+
"return": 224
42+
},
43+
{
44+
"function": "fgets",
45+
"params": [
46+
"@mock-stream",
47+
1024
48+
],
49+
"return-op": "key-respond",
50+
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}\r\n\r\n"
51+
},
52+
{
53+
"function": "fwrite",
54+
"params": [
55+
"@mock-stream"
56+
],
57+
"return": 13
58+
}
59+
]

0 commit comments

Comments
 (0)