-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
102 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |