diff --git a/spec/websocket_handler_spec.cr b/spec/websocket_handler_spec.cr index 5f2974df..b07c91b1 100644 --- a/spec/websocket_handler_spec.cr +++ b/spec/websocket_handler_spec.cr @@ -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 diff --git a/src/kemal.cr b/src/kemal.cr index 13880eae..32300221 100644 --- a/src/kemal.cr +++ b/src/kemal.cr @@ -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 @@ -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 @@ -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."