Seperate websocket and websocket handler. Fixes #395

This commit is contained in:
Serdar Dogruyol 2017-09-10 14:41:07 +03:00
parent 3050b75f0a
commit fe9d193418
10 changed files with 76 additions and 39 deletions

View file

@ -27,7 +27,7 @@ describe "Config" do
config = Kemal.config
config.add_handler CustomTestHandler.new
Kemal.config.setup
config.handlers.size.should eq(6)
config.handlers.size.should eq(7)
end
it "toggles the shutdown message" do

View file

@ -12,7 +12,7 @@ describe "Macros" do
it "adds a custom handler" do
add_handler CustomTestHandler.new
Kemal.config.setup
Kemal.config.handlers.size.should eq 6
Kemal.config.handlers.size.should eq 7
end
end

View file

@ -48,8 +48,7 @@ def create_ws_request_and_return_io(handler, request)
rescue IO::Error
# Raises because the IO::Memory is empty
end
response.close
io
io.rewind
end
def call_request_on_app(request)
@ -83,5 +82,5 @@ end
Spec.after_each do
Kemal.config.clear
Kemal::RouteHandler::INSTANCE.http_routes = Radix::Tree(Route).new
Kemal::RouteHandler::INSTANCE.ws_routes = Radix::Tree(String).new
Kemal::WebSocketHandler::INSTANCE.ws_routes = Radix::Tree(WebSocket).new
end

View file

@ -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",