Skip to content

Commit

Permalink
Test unexpected events when translating SSE to WebSockets
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Jan 18, 2025
1 parent 0b88cf0 commit 5d88f89
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/php/xp/web/srv/TranslateMessages.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function message($conn, $message) {
$conn->close();
break;
}
default: throw new IllegalStateException('Unexpected event '.$event);
default: throw new IllegalStateException('Unexpected event from backend://'.$conn->path().': '.$event);
}
}
} catch (Any $e) {
Expand Down
28 changes: 28 additions & 0 deletions src/test/php/web/unittest/server/TranslateMessagesTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,34 @@ public function close() {
Assert::false($ws->isConnected());
}

#[Test]
public function unexpected_type() {
$request= $this->message(
'POST /ws HTTP/1.1',
'Sec-WebSocket-Version: 9',
'Content-Type: text/plain',
'Content-Length: 4',
'',
'Test',
);
$response= $this->message(
'HTTP/1.1 200 OK',
'Content-Type: text/event-stream',
'Transfer-Encoding: chunked',
'',
"16\r\nevent: unknown\ndata: \n\r\n0\r\n\r\n"
);

$backend= new Channel([$response]);
$ws= new Channel([]);
$fixture= new TranslateMessages($backend);
$fixture->message(new Connection($ws, 1, null, '/ws', []), 'Test');

Assert::equals($request, implode('', $backend->out));
Assert::equals("\210\056\003\363Unexpected event from backend:///ws: unknown", implode('', $ws->out));
Assert::false($ws->isConnected());
}

#[Test]
public function backend_error() {
$request= $this->message(
Expand Down

0 comments on commit 5d88f89

Please sign in to comment.