Skip to content

Commit 615c8c0

Browse files
authored
Merge pull request #166 from Textalk/client-http-path-fix
Fix client path for http request
2 parents fc1390a + 30fa741 commit 615c8c0

File tree

6 files changed

+96
-8
lines changed

6 files changed

+96
-8
lines changed

lib/Client.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,15 @@ protected function connect(): void
310310
->withScheme($this->socket_uri->getScheme() == 'wss' ? 'ssl' : 'tcp')
311311
->withPort($this->socket_uri->getPort());
312312

313+
// Path must be absolute
314+
$http_path = $this->socket_uri->getPath();
315+
if ($http_path === '' || $http_path[0] !== '/') {
316+
$http_path = "/{$http_path}";
317+
}
318+
313319
$http_uri = (new Uri())
314-
->withPath($this->socket_uri->getPath())
315-
->withQuery($this->socket_uri->getQuery())
316-
->withFragment($this->socket_uri->getFragment());
320+
->withPath($http_path)
321+
->withQuery($this->socket_uri->getQuery());
317322

318323
// Set the stream context options if they're already set in the config
319324
if (isset($this->options['context'])) {

tests/ClientTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,24 @@ public function testClienExtendedUrl(): void
6464
$this->assertTrue(MockSocket::isEmpty());
6565
}
6666

67+
public function testClientNoPath(): void
68+
{
69+
MockSocket::initialize('client.connect-root', $this);
70+
$client = new Client('ws://localhost:8000');
71+
$client->send('Connect');
72+
$this->assertTrue(MockSocket::isEmpty());
73+
}
74+
75+
public function testClientRelativePath(): void
76+
{
77+
MockSocket::initialize('client.connect', $this);
78+
$uri = new Uri('ws://localhost:8000');
79+
$uri = $uri->withPath('my/mock/path');
80+
$client = new Client($uri);
81+
$client->send('Connect');
82+
$this->assertTrue(MockSocket::isEmpty());
83+
}
84+
6785
public function testClientWithTimeout(): void
6886
{
6987
MockSocket::initialize('client.connect-timeout', $this);

tests/mock/MockSocket.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public static function handle($function, $params = [])
2121
}
2222
self::$asserter->assertEquals($current['function'], $function);
2323
foreach ($current['params'] as $index => $param) {
24+
if (isset($current['input-op'])) {
25+
$param = self::op($current['input-op'], $params, $param);
26+
}
2427
self::$asserter->assertEquals($param, $params[$index], json_encode([$current, $params]));
2528
}
2629
if (isset($current['error'])) {
@@ -67,7 +70,7 @@ private static function op($op, $params, $data)
6770
case 'key-save':
6871
preg_match('#Sec-WebSocket-Key:\s(.*)$#mUi', $params[1], $matches);
6972
self::$stored['sec-websocket-key'] = trim($matches[1]);
70-
return $data;
73+
return str_replace('{key}', self::$stored['sec-websocket-key'], $data);
7174
case 'key-respond':
7275
$key = self::$stored['sec-websocket-key'] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
7376
$encoded = base64_encode(pack('H*', sha1($key)));

tests/scripts/client.connect-extended.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@
3434
{
3535
"function": "fwrite",
3636
"params": [
37-
"@mock-stream"
37+
"@mock-stream",
38+
"GET /my/mock/path?my_query=yes HTTP/1.1\r\nHost: localhost:8000\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"
3839
],
39-
"return-op": "key-save",
40+
"input-op": "key-save",
4041
"return": 224
4142
},
4243
{
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:8000",
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 / HTTP/1.1\r\nHost: localhost:8000\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+
]

tests/scripts/client.connect.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@
3333
},
3434
{
3535
"function": "fwrite",
36+
"regexp": true,
3637
"params": [
37-
"@mock-stream"
38+
"@mock-stream",
39+
"GET /my/mock/path HTTP/1.1\r\nHost: localhost:8000\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"
3840
],
39-
"return-op": "key-save",
41+
"input-op": "key-save",
4042
"return": 199
4143
},
4244
{

0 commit comments

Comments
 (0)