Skip to content

Commit

Permalink
Add tests for web.io.EventSink
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Jan 19, 2025
1 parent 9d0f80e commit 170e39b
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/main/php/web/io/EventSink.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,27 @@
use util\Bytes;
use websocket\protocol\{Opcodes, Connection};

/** @test web.unittest.io.EventSinkTest */
class EventSink extends Connection {
private $request, $out;

/**
* Creates a new event sink
*
* @param web.Request $request
* @param web.Response $response
*/
public function __construct($request, $response) {
$this->request= $request;
$this->out= $response->stream();
parent::__construct(null, null, null, $request->uri()->resource(), $request->headers());
}

/**
* Receives messages
*
* @return iterable
*/
public function receive() {
switch ($mime= $this->request->header('Content-Type')) {
case 'text/plain': yield Opcodes::TEXT => Streams::readAll($this->request->stream()); break;
Expand All @@ -22,6 +34,12 @@ public function receive() {
}
}

/**
* Sends a websocket message
*
* @param string|util.Bytes $message
* @return void
*/
public function send($message) {
if ($message instanceof Bytes) {
$this->out->write("event: bytes\ndata: ".addcslashes($message, "\r\n")."\n\n");
Expand All @@ -30,11 +48,14 @@ public function send($message) {
}
}

/**
* Closes the websocket connection
*
* @param int $code
* @param string $reason
* @return void
*/
public function close($code= 1000, $reason= '') {
$this->out->write("event: close\ndata: ".$code.':'.addcslashes($reason, "\r\n")."\n\n");
}

public function flush() {
$this->out->finish();
}
}
77 changes: 77 additions & 0 deletions src/test/php/web/unittest/io/EventSinkTest.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php namespace web\unittest\io;

use test\{Assert, Test};
use util\Bytes;
use web\io\{EventSink, TestInput, TestOutput};
use web\{Request, Response};
use websocket\protocol\Opcodes;

class EventSinkTest {

#[Test]
public function can_create() {
new EventSink(new Request(new TestInput('POST', '/ws')), new Response(new TestOutput()));
}

#[Test]
public function receive_text_message() {
$request= new Request(new TestInput('POST', '/ws', [
'Sec-WebSocket-Version' => 9,
'Sec-WebSocket-Id' => 123,
'Content-Type' => 'text/plain',
'Content-Length' => 4,
], 'Test'));

Assert::equals(
[Opcodes::TEXT => 'Test'],
[...(new EventSink($request, new Response(new TestOutput())))->receive()]
);
}

#[Test]
public function receive_binary_message() {
$request= new Request(new TestInput('POST', '/ws', [
'Sec-WebSocket-Version' => 9,
'Sec-WebSocket-Id' => 123,
'Content-Type' => 'application/octet-stream',
'Content-Length' => 2,
], "\x47\x11"));

Assert::equals(
[Opcodes::BINARY => new Bytes("\x47\x11")],
[...(new EventSink($request, new Response(new TestOutput())))->receive()]
);
}

#[Test]
public function send_text_message() {
$response= new Response(new TestOutput());
(new EventSink(new Request(new TestInput('POST', '/ws')), $response))->send('Test');

Assert::matches('/data: Test\n/', $response->output()->bytes());
}

#[Test]
public function send_binary_message() {
$response= new Response(new TestOutput());
(new EventSink(new Request(new TestInput('POST', '/ws')), $response))->send(new Bytes("\x47\x11"));

Assert::matches('/event: bytes\ndata: .{2}\n/', $response->output()->bytes());
}

#[Test]
public function close_message() {
$response= new Response(new TestOutput());
(new EventSink(new Request(new TestInput('POST', '/ws')), $response))->close();

Assert::matches('/event: close\ndata: 1000:\n/', $response->output()->bytes());
}

#[Test]
public function close_message_with_reason() {
$response= new Response(new TestOutput());
(new EventSink(new Request(new TestInput('POST', '/ws')), $response))->close(1011, 'Test');

Assert::matches('/event: close\ndata: 1011:Test\n/', $response->output()->bytes());
}
}

0 comments on commit 170e39b

Please sign in to comment.