mirror of
https://gitea.invidious.io/iv-org/shard-kemal.git
synced 2024-08-15 00:53:36 +00:00
Rename to Kemal::WebSocketHandler
This commit is contained in:
parent
d50d305e99
commit
fbc21b197a
3 changed files with 28 additions and 7 deletions
|
@ -1,8 +1,8 @@
|
||||||
require "./spec_helper"
|
require "./spec_helper"
|
||||||
|
|
||||||
describe "Kemal::WebsocketHandler" do
|
describe "Kemal::WebSocketHandler" do
|
||||||
it "doesn't match on wrong route" do
|
it "doesn't match on wrong route" do
|
||||||
handler = Kemal::WebsocketHandler.new "/" { }
|
handler = Kemal::WebSocketHandler.new "/" { }
|
||||||
headers = HTTP::Headers{
|
headers = HTTP::Headers{
|
||||||
"Upgrade": "websocket",
|
"Upgrade": "websocket",
|
||||||
"Connection": "Upgrade",
|
"Connection": "Upgrade",
|
||||||
|
@ -14,7 +14,7 @@ describe "Kemal::WebsocketHandler" do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "matches on given route" do
|
it "matches on given route" do
|
||||||
handler = Kemal::WebsocketHandler.new "/" { }
|
handler = Kemal::WebSocketHandler.new "/" { }
|
||||||
headers = HTTP::Headers{
|
headers = HTTP::Headers{
|
||||||
"Upgrade": "websocket",
|
"Upgrade": "websocket",
|
||||||
"Connection": "Upgrade",
|
"Connection": "Upgrade",
|
||||||
|
@ -35,7 +35,7 @@ describe "Kemal::WebsocketHandler" do
|
||||||
"hello #{env.params["message"]}"
|
"hello #{env.params["message"]}"
|
||||||
end
|
end
|
||||||
|
|
||||||
ws_handler = Kemal::WebsocketHandler.new "/" { }
|
ws_handler = Kemal::WebSocketHandler.new "/" { }
|
||||||
headers = HTTP::Headers{
|
headers = HTTP::Headers{
|
||||||
"Upgrade": "websocket",
|
"Upgrade": "websocket",
|
||||||
"Connection": "Upgrade",
|
"Connection": "Upgrade",
|
||||||
|
@ -56,4 +56,25 @@ describe "Kemal::WebsocketHandler" do
|
||||||
response.headers["Sec-WebSocket-Accept"].should eq("s3pPLMBiTxaQ9kYGzzhZRbK+xOo=")
|
response.headers["Sec-WebSocket-Accept"].should eq("s3pPLMBiTxaQ9kYGzzhZRbK+xOo=")
|
||||||
response.upgrade_handler.should_not be_nil
|
response.upgrade_handler.should_not be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "has a sockets array" do
|
||||||
|
|
||||||
|
ws_handler = Kemal::WebSocketHandler.new "/" do |socket|
|
||||||
|
|
||||||
|
end
|
||||||
|
headers = HTTP::Headers{
|
||||||
|
"Upgrade": "websocket",
|
||||||
|
"Connection": "Upgrade",
|
||||||
|
"Sec-WebSocket-Key": "dGhlIHNhbXBsZSBub25jZQ==",
|
||||||
|
}
|
||||||
|
|
||||||
|
# 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
|
end
|
||||||
|
|
|
@ -7,5 +7,5 @@ HTTP_METHODS = %w(get post put patch delete)
|
||||||
{% end %}
|
{% end %}
|
||||||
|
|
||||||
def ws(path, &block : HTTP::WebSocketHandler::WebSocketSession -> _)
|
def ws(path, &block : HTTP::WebSocketHandler::WebSocketSession -> _)
|
||||||
Kemal::WebsocketHandler.new path, &block
|
Kemal::WebSocketHandler.new path, &block
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
# Kemal::WebsocketHandler is used for each define WebSocket route.
|
# Kemal::WebSocketHandler is used for each define WebSocket route.
|
||||||
# For each WebSocket route a new handler is created and registered to global handlers.
|
# For each WebSocket route a new handler is created and registered to global handlers.
|
||||||
|
|
||||||
class Kemal::WebsocketHandler < HTTP::WebSocketHandler
|
class Kemal::WebSocketHandler < HTTP::WebSocketHandler
|
||||||
def initialize(@path, &@proc : WebSocketSession ->)
|
def initialize(@path, &@proc : WebSocketSession ->)
|
||||||
Kemal.config.add_ws_handler self
|
Kemal.config.add_ws_handler self
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue