From d149a2b6f410e1a8c05a21d06587f5dd0de41cf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D0=B9=20=D0=A1=D1=82?= =?UTF-8?q?=D0=B0=D0=B2=D1=80=D0=BE=D0=B2?= Date: Wed, 3 Jun 2020 09:27:57 +0500 Subject: [PATCH] Fix bug with empty response message --- lib/Base.php | 20 +++++------------- tests/ClientTest.php | 5 +++++ tests/ServerTest.php | 5 +++++ tests/scripts/ping-pong.json | 39 ++++++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 15 deletions(-) diff --git a/lib/Base.php b/lib/Base.php index f5fea95..fd9b8c4 100644 --- a/lib/Base.php +++ b/lib/Base.php @@ -71,23 +71,13 @@ public function send($payload, $opcode = 'text', $masked = true) throw new BadOpcodeException("Bad opcode '$opcode'. Try 'text' or 'binary'."); } - // record the length of the payload - $payload_length = strlen($payload); - - $fragment_cursor = 0; - // while we have data to send - while ($payload_length > $fragment_cursor) { - // get a fragment of the payload - $sub_payload = substr($payload, $fragment_cursor, $this->options['fragment_size']); - - // advance the cursor - $fragment_cursor += $this->options['fragment_size']; + $payload_chunks = str_split($payload, $this->options['fragment_size']); - // is this the final fragment to send? - $final = $payload_length <= $fragment_cursor; + for ($index = 0; $index < count($payload_chunks); ++$index) { + $chunk = $payload_chunks[$index]; + $final = $index == count($payload_chunks) - 1; - // send the fragment - $this->sendFragment($final, $sub_payload, $opcode, $masked); + $this->sendFragment($final, $chunk, $opcode, $masked); // all fragments after the first will be marked a continuation $opcode = 'continuation'; diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 3c060fc..bbcb055 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -162,6 +162,11 @@ public function testPingPong() $this->assertEquals('Server ping', $message); $this->assertEquals('pong', $client->getLastOpcode()); + $client->send('', 'ping'); + $message = $client->receive(); + $this->assertEquals('', $message); + $this->assertEquals('pong', $client->getLastOpcode()); + $message = $client->receive(); $this->assertEquals('Client ping', $message); $this->assertTrue(MockSocket::isEmpty()); diff --git a/tests/ServerTest.php b/tests/ServerTest.php index e789976..9edc011 100644 --- a/tests/ServerTest.php +++ b/tests/ServerTest.php @@ -165,6 +165,11 @@ public function testPingPong() $this->assertEquals('Server ping', $message); $this->assertEquals('pong', $server->getLastOpcode()); + $server->send('', 'ping'); + $message = $server->receive(); + $this->assertEquals('', $message); + $this->assertEquals('pong', $server->getLastOpcode()); + $message = $server->receive(); $this->assertEquals('Client ping', $message); diff --git a/tests/scripts/ping-pong.json b/tests/scripts/ping-pong.json index 77a5c51..9c7adf9 100644 --- a/tests/scripts/ping-pong.json +++ b/tests/scripts/ping-pong.json @@ -54,6 +54,45 @@ ], "return": "stream" }, + { + "function": "fwrite", + "params": [ + "@mock-stream" + ], + "return": 6 + }, + { + "function": "get_resource_type", + "params": [ + "@mock-stream" + ], + "return": "stream" + }, + { + "function": "fread", + "params": [ + "@mock-stream", + 2 + ], + "return-op": "chr-array", + "return": [138, 128] + }, + { + "function": "fread", + "params": [ + "@mock-stream", + 4 + ], + "return-op": "chr-array", + "return": [1, 1, 1, 1] + }, + { + "function": "get_resource_type", + "params": [ + "@mock-stream" + ], + "return": "stream" + }, { "function": "fread", "params": [