Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions spec/websocket_handler_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,29 @@ describe "Kemal::WebSocketHandler" do
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==",
"Upgrade" => "websocket",
"Connection" => "Upgrade",
"Sec-WebSocket-Key" => "dGhlIHNhbXBsZSBub25jZQ==",
"Sec-WebSocket-Version" => "13",
}
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\x81\u0005Match")
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::INSTANCE
ws "/:id" { |s, c| c.params.url["id"] }
headers = HTTP::Headers{
"Upgrade" => "websocket",
"Connection" => "Upgrade",
"Sec-WebSocket-Key" => "dGhlIHNhbXBsZSBub25jZQ==",
"Upgrade" => "websocket",
"Connection" => "Upgrade",
"Sec-WebSocket-Key" => "dGhlIHNhbXBsZSBub25jZQ==",
"Sec-WebSocket-Version" => "13",
}
request = HTTP::Request.new("GET", "/1234", 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")
end

it "matches correct verb" do
Expand Down
10 changes: 5 additions & 5 deletions src/kemal.cr
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ module Kemal
# This route serves the built-in images for not_found and exceptions.
get "/__kemal__/404.png" do |env|
file_path = File.expand_path("lib/kemal/images/404.png", Dir.current)

if File.exists? file_path
send_file env, file_path
else
Expand All @@ -59,7 +59,7 @@ module Kemal
end
end

config.server ||= HTTP::Server.new(config.host_binding, config.port, config.handlers)
server = config.server ||= HTTP::Server.new(config.handlers)

{% if !flag?(:without_openssl) %}
config.server.not_nil!.tls = config.ssl
Expand All @@ -68,13 +68,13 @@ module Kemal
config.running = true

yield config
config.server.not_nil!.listen if config.env != "test" && config.server
server.listen(config.host_binding, config.port) if config.env != "test"
end

def self.stop
if config.running
if config.server
config.server.not_nil!.close
if server = config.server
server.close unless server.closed?
config.running = false
else
raise "Kemal.config.server is not set. Please use Kemal.run to set the server."
Expand Down