mirror of
https://gitea.invidious.io/iv-org/shard-kemal.git
synced 2024-08-15 00:53:36 +00:00
Seperate websocket and websocket handler. Fixes #395
This commit is contained in:
parent
3050b75f0a
commit
fe9d193418
10 changed files with 76 additions and 39 deletions
|
@ -2,32 +2,47 @@ require "./spec_helper"
|
|||
|
||||
describe "Kemal::WebSocketHandler" do
|
||||
it "doesn't match on wrong route" do
|
||||
handler = Kemal::WebSocketHandler.new "/" { }
|
||||
handler = Kemal::WebSocketHandler::INSTANCE
|
||||
handler.next = Kemal::CommonExceptionHandler::INSTANCE
|
||||
ws "/" { }
|
||||
headers = HTTP::Headers{
|
||||
"Upgrade" => "websocket",
|
||||
"Connection" => "Upgrade",
|
||||
"Sec-WebSocket-Key" => "dGhlIHNhbXBsZSBub25jZQ==",
|
||||
}
|
||||
request = HTTP::Request.new("GET", "/asd", headers)
|
||||
io_with_context = create_request_and_return_io(handler, request)
|
||||
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
|
||||
io = IO::Memory.new
|
||||
response = HTTP::Server::Response.new(io)
|
||||
context = HTTP::Server::Context.new(request, response)
|
||||
begin
|
||||
handler.call context
|
||||
rescue IO::Error
|
||||
# Raises because the IO::Memory is empty
|
||||
end
|
||||
response.close
|
||||
io.rewind
|
||||
client_response = HTTP::Client::Response.from_io(io, decompress: false)
|
||||
client_response.status_code.should eq(404)
|
||||
end
|
||||
|
||||
it "matches on given route" do
|
||||
handler = Kemal::WebSocketHandler.new "/" { }
|
||||
handler = Kemal::WebSocketHandler::INSTANCE
|
||||
ws "/" { |socket, context| socket.send("Match") }
|
||||
ws "/no_match" { |socket, context| socket.send "No Match" }
|
||||
headers = HTTP::Headers{
|
||||
"Upgrade" => "websocket",
|
||||
"Connection" => "Upgrade",
|
||||
"Sec-WebSocket-Key" => "dGhlIHNhbXBsZSBub25jZQ==",
|
||||
}
|
||||
request = HTTP::Request.new("GET", "/", headers)
|
||||
|
||||
io_with_context = create_ws_request_and_return_io(handler, request)
|
||||
io_with_context.to_s.should eq("HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-Websocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n")
|
||||
io_with_context.to_s.should eq("HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-Websocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n\x81\u0005Match")
|
||||
end
|
||||
|
||||
it "fetches named url parameters" do
|
||||
handler = Kemal::WebSocketHandler.new "/:id" { |s, c| c.params.url["id"] }
|
||||
handler = Kemal::WebSocketHandler::INSTANCE
|
||||
ws "/:id" { |s, c| c.params.url["id"] }
|
||||
headers = HTTP::Headers{
|
||||
"Upgrade" => "websocket",
|
||||
"Connection" => "Upgrade",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue