kemal/spec/kemal_ws_handler_spec.cr

60 lines
2 KiB
Crystal
Raw Normal View History

2015-12-15 21:11:21 +00:00
require "./spec_helper"
2015-12-22 18:51:27 +00:00
describe "Kemal::WebSocketHandler" do
2015-12-15 21:11:21 +00:00
it "doesn't match on wrong 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{
"Upgrade": "websocket",
"Connection": "Upgrade",
"Sec-WebSocket-Key": "dGhlIHNhbXBsZSBub25jZQ==",
}
request = HTTP::Request.new("GET", "/asd", headers)
response = handler.call request
response.status_code.should eq(404)
end
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{
"Upgrade": "websocket",
"Connection": "Upgrade",
"Sec-WebSocket-Key": "dGhlIHNhbXBsZSBub25jZQ==",
}
request = HTTP::Request.new("GET", "/", headers)
response = handler.call request
response.status_code.should eq(101)
response.headers["Upgrade"].should eq("websocket")
response.headers["Connection"].should eq("Upgrade")
response.headers["Sec-WebSocket-Accept"].should eq("s3pPLMBiTxaQ9kYGzzhZRbK+xOo=")
response.upgrade_handler.should_not be_nil
end
it "doesn't mix http and ws on same route" do
kemal = Kemal::Handler.new
kemal.add_route "GET", "/" do |env|
"hello #{env.params["message"]}"
end
2015-12-22 18:51:27 +00:00
ws_handler = Kemal::WebSocketHandler.new "/" { }
2015-12-15 21:11:21 +00:00
headers = HTTP::Headers{
"Upgrade": "websocket",
"Connection": "Upgrade",
"Sec-WebSocket-Key": "dGhlIHNhbXBsZSBub25jZQ==",
}
# HTTP Request
request = HTTP::Request.new("GET", "/?message=world")
response = kemal.call(request)
response.body.should eq("hello world")
# Websocket request
request = HTTP::Request.new("GET", "/", headers)
response = ws_handler.call request
response.status_code.should eq(101)
response.headers["Upgrade"].should eq("websocket")
response.headers["Connection"].should eq("Upgrade")
response.headers["Sec-WebSocket-Accept"].should eq("s3pPLMBiTxaQ9kYGzzhZRbK+xOo=")
response.upgrade_handler.should_not be_nil
end
end