Skip to content

Commit

Permalink
Fix bug with empty response message
Browse files Browse the repository at this point in the history
  • Loading branch information
Logioniz committed Jun 3, 2020
1 parent 9fbb3b4 commit d149a2b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 15 deletions.
20 changes: 5 additions & 15 deletions lib/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
5 changes: 5 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
5 changes: 5 additions & 0 deletions tests/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
39 changes: 39 additions & 0 deletions tests/scripts/ping-pong.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down

0 comments on commit d149a2b

Please sign in to comment.