2015-12-15 21:11:21 +00:00
|
|
|
require "./spec_helper"
|
|
|
|
|
2015-12-22 18:51:27 +00:00
|
|
|
describe "Kemal::WebSocketHandler" do
|
2017-08-20 17:10:38 +00:00
|
|
|
it "doesn't match on wrong route" do
|
2017-09-10 11:41:07 +00:00
|
|
|
handler = Kemal::WebSocketHandler::INSTANCE
|
2017-09-10 12:04:51 +00:00
|
|
|
handler.next = Kemal::RouteHandler::INSTANCE
|
2017-09-10 11:41:07 +00:00
|
|
|
ws "/" { }
|
2017-08-20 17:10:38 +00:00
|
|
|
headers = HTTP::Headers{
|
|
|
|
"Upgrade" => "websocket",
|
|
|
|
"Connection" => "Upgrade",
|
|
|
|
"Sec-WebSocket-Key" => "dGhlIHNhbXBsZSBub25jZQ==",
|
|
|
|
}
|
|
|
|
request = HTTP::Request.new("GET", "/asd", headers)
|
2017-09-10 11:41:07 +00:00
|
|
|
io = IO::Memory.new
|
|
|
|
response = HTTP::Server::Response.new(io)
|
|
|
|
context = HTTP::Server::Context.new(request, response)
|
2017-09-10 12:04:51 +00:00
|
|
|
|
|
|
|
expect_raises(Kemal::Exceptions::RouteNotFound) do
|
2017-09-10 11:41:07 +00:00
|
|
|
handler.call context
|
|
|
|
end
|
2017-08-20 17:10:38 +00:00
|
|
|
end
|
|
|
|
|
2017-08-20 17:01:49 +00:00
|
|
|
it "matches on given route" do
|
2017-09-10 11:41:07 +00:00
|
|
|
handler = Kemal::WebSocketHandler::INSTANCE
|
2021-03-15 05:45:35 +00:00
|
|
|
ws("/", &.send("Match"))
|
|
|
|
ws("/no_match", &.send("No Match"))
|
2015-12-15 21:11:21 +00:00
|
|
|
headers = HTTP::Headers{
|
2018-06-16 15:03:00 +00:00
|
|
|
"Upgrade" => "websocket",
|
|
|
|
"Connection" => "Upgrade",
|
|
|
|
"Sec-WebSocket-Key" => "dGhlIHNhbXBsZSBub25jZQ==",
|
|
|
|
"Sec-WebSocket-Version" => "13",
|
2015-12-15 21:11:21 +00:00
|
|
|
}
|
2017-08-20 17:01:49 +00:00
|
|
|
request = HTTP::Request.new("GET", "/", headers)
|
2017-09-10 11:41:07 +00:00
|
|
|
|
2018-06-30 13:49:04 +00:00
|
|
|
io_with_context = create_ws_request_and_return_io_and_context(handler, request)[0]
|
2018-06-16 15:03:00 +00:00
|
|
|
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")
|
2015-12-15 21:11:21 +00:00
|
|
|
end
|
|
|
|
|
2017-08-20 17:01:49 +00:00
|
|
|
it "fetches named url parameters" do
|
2017-09-10 11:41:07 +00:00
|
|
|
handler = Kemal::WebSocketHandler::INSTANCE
|
2018-06-30 13:49:04 +00:00
|
|
|
ws "/:id" { |_, c| c.ws_route_lookup.params["id"] }
|
2015-12-15 21:11:21 +00:00
|
|
|
headers = HTTP::Headers{
|
2018-06-16 15:03:00 +00:00
|
|
|
"Upgrade" => "websocket",
|
|
|
|
"Connection" => "Upgrade",
|
|
|
|
"Sec-WebSocket-Key" => "dGhlIHNhbXBsZSBub25jZQ==",
|
|
|
|
"Sec-WebSocket-Version" => "13",
|
2015-12-15 21:11:21 +00:00
|
|
|
}
|
2017-08-20 17:01:49 +00:00
|
|
|
request = HTTP::Request.new("GET", "/1234", headers)
|
2018-06-30 13:49:04 +00:00
|
|
|
io_with_context = create_ws_request_and_return_io_and_context(handler, request)[0]
|
2018-06-16 15:03:00 +00:00
|
|
|
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")
|
2015-12-15 21:11:21 +00:00
|
|
|
end
|
2017-09-14 16:59:22 +00:00
|
|
|
|
|
|
|
it "matches correct verb" do
|
|
|
|
handler = Kemal::WebSocketHandler::INSTANCE
|
|
|
|
handler.next = Kemal::RouteHandler::INSTANCE
|
|
|
|
ws "/" { }
|
|
|
|
get "/" { "get" }
|
|
|
|
request = HTTP::Request.new("GET", "/")
|
|
|
|
io = IO::Memory.new
|
|
|
|
response = HTTP::Server::Response.new(io)
|
|
|
|
context = HTTP::Server::Context.new(request, response)
|
|
|
|
handler.call(context)
|
|
|
|
response.close
|
|
|
|
io.rewind
|
|
|
|
client_response = HTTP::Client::Response.from_io(io, decompress: false)
|
|
|
|
client_response.body.should eq("get")
|
|
|
|
end
|
2015-12-15 21:11:21 +00:00
|
|
|
end
|