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
|
|
|
|
handler = Kemal::WebSocketHandler.new "/" { }
|
|
|
|
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)
|
|
|
|
client_response.status_code.should eq(404)
|
|
|
|
end
|
|
|
|
|
2017-08-20 17:01:49 +00:00
|
|
|
it "matches on given route" do
|
2015-12-22 18:51:27 +00:00
|
|
|
handler = Kemal::WebSocketHandler.new "/" { }
|
2015-12-15 21:11:21 +00:00
|
|
|
headers = HTTP::Headers{
|
2016-06-14 21:18:00 +00:00
|
|
|
"Upgrade" => "websocket",
|
|
|
|
"Connection" => "Upgrade",
|
|
|
|
"Sec-WebSocket-Key" => "dGhlIHNhbXBsZSBub25jZQ==",
|
2015-12-15 21:11:21 +00:00
|
|
|
}
|
2017-08-20 17:01:49 +00:00
|
|
|
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")
|
2015-12-15 21:11:21 +00:00
|
|
|
end
|
|
|
|
|
2017-08-20 17:01:49 +00:00
|
|
|
it "fetches named url parameters" do
|
|
|
|
handler = Kemal::WebSocketHandler.new "/:id" { |s, c| c.params.url["id"] }
|
2015-12-15 21:11:21 +00:00
|
|
|
headers = HTTP::Headers{
|
2016-06-14 21:18:00 +00:00
|
|
|
"Upgrade" => "websocket",
|
|
|
|
"Connection" => "Upgrade",
|
|
|
|
"Sec-WebSocket-Key" => "dGhlIHNhbXBsZSBub25jZQ==",
|
2015-12-15 21:11:21 +00:00
|
|
|
}
|
2017-08-20 17:01:49 +00:00
|
|
|
request = HTTP::Request.new("GET", "/1234", headers)
|
2016-01-24 10:22:25 +00:00
|
|
|
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")
|
2015-12-15 21:11:21 +00:00
|
|
|
end
|
|
|
|
end
|