mirror of
				https://gitea.invidious.io/iv-org/shard-kemal.git
				synced 2024-08-15 00:53:36 +00:00 
			
		
		
		
	Refactor global namespace DSL into OOP Kemal::Base
This commit is contained in:
		
							parent
							
								
									a5d8df7382
								
							
						
					
					
						commit
						aaa2109837
					
				
					 25 changed files with 420 additions and 387 deletions
				
			
		|  | @ -2,15 +2,17 @@ require "./spec_helper" | |||
| 
 | ||||
| describe "Config" do | ||||
|   it "sets default port to 3000" do | ||||
|     Kemal::Config.new.port.should eq 3000 | ||||
|     config = Kemal::Config.new | ||||
|     config.port.should eq 3000 | ||||
|   end | ||||
| 
 | ||||
|   it "sets default environment to development" do | ||||
|     Kemal::Config.new.env.should eq "development" | ||||
|     config = Kemal::Config.new | ||||
|     config.env.should eq "development" | ||||
|   end | ||||
| 
 | ||||
|   it "sets environment to production" do | ||||
|     config = Kemal.config | ||||
|     config = Kemal::Config.new | ||||
|     config.env = "production" | ||||
|     config.env.should eq "production" | ||||
|   end | ||||
|  | @ -20,28 +22,28 @@ describe "Config" do | |||
|   end | ||||
| 
 | ||||
|   it "sets host binding" do | ||||
|     config = Kemal.config | ||||
|     config = Kemal::Config.new | ||||
|     config.host_binding = "127.0.0.1" | ||||
|     config.host_binding.should eq "127.0.0.1" | ||||
|   end | ||||
| 
 | ||||
|   it "adds a custom handler" do | ||||
|     config = Kemal.config | ||||
|     config.add_handler CustomTestHandler.new | ||||
|     Kemal.config.setup | ||||
|     config.handlers.size.should eq(7) | ||||
|     application = Kemal::Base.new | ||||
|     application.add_handler CustomTestHandler.new | ||||
|     application.setup | ||||
|     application.handlers.size.should eq(8) | ||||
|   end | ||||
| 
 | ||||
|   it "toggles the shutdown message" do | ||||
|     config = Kemal.config | ||||
|     config = Kemal::Config.new | ||||
|     config.shutdown_message = false | ||||
|     config.shutdown_message.should eq false | ||||
|     config.shutdown_message?.should be_false | ||||
|     config.shutdown_message = true | ||||
|     config.shutdown_message.should eq true | ||||
|     config.shutdown_message?.should be_true | ||||
|   end | ||||
| 
 | ||||
|   it "adds custom options" do | ||||
|     config = Kemal.config | ||||
|     config = Kemal::Config.new | ||||
|     ARGV.push("--test") | ||||
|     ARGV.push("FOOBAR") | ||||
|     test_option = nil | ||||
|  | @ -51,7 +53,8 @@ describe "Config" do | |||
|         test_option = opt | ||||
|       end | ||||
|     end | ||||
|     Kemal::CLI.new ARGV | ||||
| 
 | ||||
|     Kemal::CLI.new(ARGV, config) | ||||
|     test_option.should eq("FOOBAR") | ||||
|   end | ||||
| 
 | ||||
|  |  | |||
|  | @ -90,18 +90,17 @@ describe "Context" do | |||
|       context.get("another_context_test").as(AnotherContextStorageType).name.should eq "kemal-context" | ||||
|     end | ||||
| 
 | ||||
|     it "fetches non-existent keys from store with get?" do | ||||
|       get "/" { } | ||||
| 
 | ||||
|       request = HTTP::Request.new("GET", "/") | ||||
|       io = IO::Memory.new | ||||
|       response = HTTP::Server::Response.new(io) | ||||
|       context = HTTP::Server::Context.new(request, response) | ||||
|       Kemal::FilterHandler::INSTANCE.call(context) | ||||
|       Kemal::RouteHandler::INSTANCE.call(context) | ||||
| 
 | ||||
|       context.get?("non_existent_key").should be_nil | ||||
|       context.get?("another_non_existent_key").should be_nil | ||||
|     end | ||||
|     request = HTTP::Request.new("GET", "/") | ||||
|     io = IO::Memory.new | ||||
|     response = HTTP::Server::Response.new(io) | ||||
|     context = HTTP::Server::Context.new(request, response) | ||||
|     context.app = Kemal.application | ||||
|     Kemal.application.filter_handler.call(context) | ||||
|     Kemal.application.route_handler.call(context) | ||||
|     context.store["key"].should eq "value" | ||||
|     context.store["before_get"].should eq "Kemal" | ||||
|     context.store["before_get_int"].should eq 123 | ||||
|     context.store["before_get_float"].should eq 3.5 | ||||
|     context.store["before_get_context_test"].as(TestContextStorageType).id.should eq 32 | ||||
|   end | ||||
| end | ||||
|  |  | |||
|  | @ -1,5 +1,7 @@ | |||
| require "./spec_helper" | ||||
| 
 | ||||
| private INSTANCE = Kemal::ExceptionHandler.new | ||||
| 
 | ||||
| describe "Kemal::ExceptionHandler" do | ||||
|   it "renders 404 on route not found" do | ||||
|     get "/" do | ||||
|  | @ -10,7 +12,7 @@ describe "Kemal::ExceptionHandler" do | |||
|     io = IO::Memory.new | ||||
|     response = HTTP::Server::Response.new(io) | ||||
|     context = HTTP::Server::Context.new(request, response) | ||||
|     Kemal::ExceptionHandler::INSTANCE.call(context) | ||||
|     INSTANCE.call(context) | ||||
|     response.close | ||||
|     io.rewind | ||||
|     response = HTTP::Client::Response.from_io(io, decompress: false) | ||||
|  | @ -28,8 +30,9 @@ describe "Kemal::ExceptionHandler" do | |||
|     io = IO::Memory.new | ||||
|     response = HTTP::Server::Response.new(io) | ||||
|     context = HTTP::Server::Context.new(request, response) | ||||
|     Kemal::ExceptionHandler::INSTANCE.next = Kemal::RouteHandler::INSTANCE | ||||
|     Kemal::ExceptionHandler::INSTANCE.call(context) | ||||
|     context.app = Kemal.application | ||||
|     INSTANCE.next = Kemal::RouteHandler.new | ||||
|     INSTANCE.call(context) | ||||
|     response.close | ||||
|     io.rewind | ||||
|     response = HTTP::Client::Response.from_io(io, decompress: false) | ||||
|  | @ -49,8 +52,9 @@ describe "Kemal::ExceptionHandler" do | |||
|     io = IO::Memory.new | ||||
|     response = HTTP::Server::Response.new(io) | ||||
|     context = HTTP::Server::Context.new(request, response) | ||||
|     Kemal::ExceptionHandler::INSTANCE.next = Kemal::RouteHandler::INSTANCE | ||||
|     Kemal::ExceptionHandler::INSTANCE.call(context) | ||||
|     context.app = Kemal.application | ||||
|     INSTANCE.next = Kemal::RouteHandler.new | ||||
|     INSTANCE.call(context) | ||||
|     response.close | ||||
|     io.rewind | ||||
|     response = HTTP::Client::Response.from_io(io, decompress: false) | ||||
|  | @ -71,8 +75,9 @@ describe "Kemal::ExceptionHandler" do | |||
|     io = IO::Memory.new | ||||
|     response = HTTP::Server::Response.new(io) | ||||
|     context = HTTP::Server::Context.new(request, response) | ||||
|     Kemal::ExceptionHandler::INSTANCE.next = Kemal::RouteHandler::INSTANCE | ||||
|     Kemal::ExceptionHandler::INSTANCE.call(context) | ||||
|     context.app = Kemal.application | ||||
|     INSTANCE.next = Kemal::RouteHandler.new | ||||
|     INSTANCE.call(context) | ||||
|     response.close | ||||
|     io.rewind | ||||
|     response = HTTP::Client::Response.from_io(io, decompress: false) | ||||
|  | @ -93,8 +98,9 @@ describe "Kemal::ExceptionHandler" do | |||
|     io = IO::Memory.new | ||||
|     response = HTTP::Server::Response.new(io) | ||||
|     context = HTTP::Server::Context.new(request, response) | ||||
|     Kemal::ExceptionHandler::INSTANCE.next = Kemal::RouteHandler::INSTANCE | ||||
|     Kemal::ExceptionHandler::INSTANCE.call(context) | ||||
|     context.app = Kemal.application | ||||
|     INSTANCE.next = Kemal::RouteHandler.new | ||||
|     INSTANCE.call(context) | ||||
|     response.close | ||||
|     io.rewind | ||||
|     response = HTTP::Client::Response.from_io(io, decompress: false) | ||||
|  |  | |||
|  | @ -70,6 +70,7 @@ end | |||
| describe "Handler" do | ||||
|   it "adds custom handler before before_*" do | ||||
|     filter_middleware = Kemal::FilterHandler.new | ||||
|     Kemal.application.add_filter_handler filter_middleware | ||||
|     filter_middleware._add_route_filter("GET", "/", :before) do |env| | ||||
|       env.response << " is" | ||||
|     end | ||||
|  | @ -77,6 +78,8 @@ describe "Handler" do | |||
|     filter_middleware._add_route_filter("GET", "/", :before) do |env| | ||||
|       env.response << " so" | ||||
|     end | ||||
|     Kemal.application.add_filter_handler filter_middleware | ||||
| 
 | ||||
|     add_handler CustomTestHandler.new | ||||
| 
 | ||||
|     get "/" do | ||||
|  | @ -141,21 +144,21 @@ describe "Handler" do | |||
|   it "adds a handler at given position" do | ||||
|     post_handler = PostOnlyHandler.new | ||||
|     add_handler post_handler, 1 | ||||
|     Kemal.config.setup | ||||
|     Kemal.config.handlers[1].should eq post_handler | ||||
|     Kemal.application.setup | ||||
|     Kemal.application.handlers[1].should eq post_handler | ||||
|   end | ||||
| 
 | ||||
|   it "assigns custom handlers" do | ||||
|     post_only_handler = PostOnlyHandler.new | ||||
|     post_exclude_handler = PostExcludeHandler.new | ||||
|     Kemal.config.handlers = [post_only_handler, post_exclude_handler] | ||||
|     Kemal.config.handlers.should eq [post_only_handler, post_exclude_handler] | ||||
|     Kemal.application.handlers = [post_only_handler, post_exclude_handler] | ||||
|     Kemal.application.handlers.should eq [post_only_handler, post_exclude_handler] | ||||
|   end | ||||
| 
 | ||||
|   it "is able to use %w in macros" do | ||||
|     post_only_handler = PostOnlyHandlerPercentW.new | ||||
|     exclude_handler = ExcludeHandlerPercentW.new | ||||
|     Kemal.config.handlers = [post_only_handler, exclude_handler] | ||||
|     Kemal.config.handlers.should eq [post_only_handler, exclude_handler] | ||||
|     Kemal.application.handlers = [post_only_handler, exclude_handler] | ||||
|     Kemal.application.handlers.should eq [post_only_handler, exclude_handler] | ||||
|   end | ||||
| end | ||||
|  |  | |||
|  | @ -11,21 +11,21 @@ describe "Macros" do | |||
|   describe "#add_handler" do | ||||
|     it "adds a custom handler" do | ||||
|       add_handler CustomTestHandler.new | ||||
|       Kemal.config.setup | ||||
|       Kemal.config.handlers.size.should eq 7 | ||||
|       Kemal.application.setup | ||||
|       Kemal.application.handlers.size.should eq 7 | ||||
|     end | ||||
|   end | ||||
| 
 | ||||
|   describe "#logging" do | ||||
|     it "sets logging status" do | ||||
|       logging false | ||||
|       Kemal.config.logging.should eq false | ||||
|       Kemal.config.logging?.should be_false | ||||
|     end | ||||
| 
 | ||||
|     it "sets a custom logger" do | ||||
|       config = Kemal::Config::INSTANCE | ||||
|       config = Kemal.config | ||||
|       logger CustomLogHandler.new | ||||
|       config.logger.should be_a(CustomLogHandler) | ||||
|       Kemal.application.logger.should be_a(CustomLogHandler) | ||||
|     end | ||||
|   end | ||||
| 
 | ||||
|  | @ -119,24 +119,24 @@ describe "Macros" do | |||
|   describe "#gzip" do | ||||
|     it "adds HTTP::CompressHandler to handlers" do | ||||
|       gzip true | ||||
|       Kemal.config.setup | ||||
|       Kemal.config.handlers[4].should be_a(HTTP::CompressHandler) | ||||
|       Kemal.application.setup | ||||
|       Kemal.application.handlers[4].should be_a(HTTP::CompressHandler) | ||||
|     end | ||||
|   end | ||||
| 
 | ||||
|   describe "#serve_static" do | ||||
|     it "should disable static file hosting" do | ||||
|       serve_static false | ||||
|       Kemal.config.serve_static.should eq false | ||||
|       Kemal.config.serve_static.should be_false | ||||
|     end | ||||
| 
 | ||||
|     it "should disble enable gzip and dir_listing" do | ||||
|       serve_static({"gzip" => true, "dir_listing" => true}) | ||||
|       conf = Kemal.config.serve_static | ||||
|       conf.is_a?(Hash).should eq true | ||||
|       conf.is_a?(Hash).should be_true # Can't use be_a(Hash) because Hash can't be used as generic argument | ||||
|       if conf.is_a?(Hash) | ||||
|         conf["gzip"].should eq true | ||||
|         conf["dir_listing"].should eq true | ||||
|         conf["gzip"].should be_true | ||||
|         conf["dir_listing"].should be_true | ||||
|       end | ||||
|     end | ||||
|   end | ||||
|  |  | |||
|  | @ -6,8 +6,9 @@ describe "Kemal::InitHandler" do | |||
|     io = IO::Memory.new | ||||
|     response = HTTP::Server::Response.new(io) | ||||
|     context = HTTP::Server::Context.new(request, response) | ||||
|     Kemal::InitHandler::INSTANCE.next = ->(_context : HTTP::Server::Context) {} | ||||
|     Kemal::InitHandler::INSTANCE.call(context) | ||||
|     init_handler = Kemal::InitHandler.new(Kemal::Base.new) | ||||
|     init_handler.next = ->(context : HTTP::Server::Context) {} | ||||
|     init_handler.call(context) | ||||
|     context.response.headers["Content-Type"].should eq "text/html" | ||||
|   end | ||||
| 
 | ||||
|  | @ -16,7 +17,8 @@ describe "Kemal::InitHandler" do | |||
|     io = IO::Memory.new | ||||
|     response = HTTP::Server::Response.new(io) | ||||
|     context = HTTP::Server::Context.new(request, response) | ||||
|     Kemal::InitHandler::INSTANCE.call(context) | ||||
|     init_handler = Kemal::InitHandler.new(Kemal::Base.new) | ||||
|     init_handler.call(context) | ||||
|     context.response.headers["X-Powered-By"].should eq "Kemal" | ||||
|   end | ||||
| 
 | ||||
|  |  | |||
|  | @ -8,7 +8,7 @@ describe "Kemal::FilterHandler" do | |||
|     filter_middleware = Kemal::FilterHandler.new | ||||
|     filter_middleware._add_route_filter("GET", "/greetings", :before) { test_filter.modified = "true" } | ||||
| 
 | ||||
|     kemal = Kemal::RouteHandler::INSTANCE | ||||
|     kemal = Kemal.application.route_handler | ||||
|     kemal.add_route "GET", "/greetings" { test_filter.modified } | ||||
| 
 | ||||
|     test_filter.modified.should eq("false") | ||||
|  | @ -26,7 +26,7 @@ describe "Kemal::FilterHandler" do | |||
|     filter_middleware = Kemal::FilterHandler.new | ||||
|     filter_middleware._add_route_filter("GET", "/greetings", :before) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" } | ||||
| 
 | ||||
|     kemal = Kemal::RouteHandler::INSTANCE | ||||
|     kemal = Kemal.application.route_handler | ||||
|     kemal.add_route "GET", "/greetings" { test_filter.modified } | ||||
|     kemal.add_route "POST", "/greetings" { test_filter.modified } | ||||
| 
 | ||||
|  | @ -54,7 +54,7 @@ describe "Kemal::FilterHandler" do | |||
|     filter_middleware._add_route_filter("GET", "/greetings", :before) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" } | ||||
|     filter_middleware._add_route_filter("POST", "/greetings", :before) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" } | ||||
| 
 | ||||
|     kemal = Kemal::RouteHandler::INSTANCE | ||||
|     kemal = Kemal.application.route_handler | ||||
|     kemal.add_route "GET", "/greetings" { test_filter.modified } | ||||
|     kemal.add_route "POST", "/greetings" { test_filter.modified } | ||||
| 
 | ||||
|  | @ -80,7 +80,7 @@ describe "Kemal::FilterHandler" do | |||
|     filter_middleware = Kemal::FilterHandler.new | ||||
|     filter_middleware._add_route_filter("GET", "/greetings", :after) { test_filter.modified = "true" } | ||||
| 
 | ||||
|     kemal = Kemal::RouteHandler::INSTANCE | ||||
|     kemal = Kemal.application.route_handler | ||||
|     kemal.add_route "GET", "/greetings" { test_filter.modified } | ||||
| 
 | ||||
|     test_filter.modified.should eq("false") | ||||
|  | @ -98,7 +98,7 @@ describe "Kemal::FilterHandler" do | |||
|     filter_middleware = Kemal::FilterHandler.new | ||||
|     filter_middleware._add_route_filter("GET", "/greetings", :after) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" } | ||||
| 
 | ||||
|     kemal = Kemal::RouteHandler::INSTANCE | ||||
|     kemal = Kemal.application.route_handler | ||||
|     kemal.add_route "GET", "/greetings" { test_filter.modified } | ||||
|     kemal.add_route "POST", "/greetings" { test_filter.modified } | ||||
| 
 | ||||
|  | @ -126,7 +126,7 @@ describe "Kemal::FilterHandler" do | |||
|     filter_middleware._add_route_filter("GET", "/greetings", :after) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" } | ||||
|     filter_middleware._add_route_filter("POST", "/greetings", :after) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" } | ||||
| 
 | ||||
|     kemal = Kemal::RouteHandler::INSTANCE | ||||
|     kemal = Kemal.application.route_handler | ||||
|     kemal.add_route "GET", "/greetings" { test_filter.modified } | ||||
|     kemal.add_route "POST", "/greetings" { test_filter.modified } | ||||
| 
 | ||||
|  | @ -157,7 +157,7 @@ describe "Kemal::FilterHandler" do | |||
|     filter_middleware._add_route_filter("ALL", "/greetings", :before) { test_filter_second.modified = test_filter_second.modified == "true" ? "false" : "true" } | ||||
|     filter_middleware._add_route_filter("ALL", "/greetings", :before) { test_filter_third.modified = test_filter_third.modified == "true" ? "false" : "true" } | ||||
| 
 | ||||
|     kemal = Kemal::RouteHandler::INSTANCE | ||||
|     kemal = Kemal.application.route_handler | ||||
|     kemal.add_route "GET", "/greetings" { test_filter.modified } | ||||
|     kemal.add_route "POST", "/greetings" { test_filter_second.modified } | ||||
|     kemal.add_route "PUT", "/greetings" { test_filter_third.modified } | ||||
|  |  | |||
|  | @ -22,7 +22,7 @@ describe "ParamParser" do | |||
|   end | ||||
| 
 | ||||
|   it "parses url params" do | ||||
|     kemal = Kemal::RouteHandler::INSTANCE | ||||
|     kemal = Kemal.application.route_handler | ||||
|     kemal.add_route "POST", "/hello/:hasan" do |env| | ||||
|       "hello #{env.params.url["hasan"]}" | ||||
|     end | ||||
|  | @ -34,7 +34,7 @@ describe "ParamParser" do | |||
|   end | ||||
| 
 | ||||
|   it "decodes url params" do | ||||
|     kemal = Kemal::RouteHandler::INSTANCE | ||||
|     kemal = Kemal.application.route_handler | ||||
|     kemal.add_route "POST", "/hello/:email/:money/:spanish" do |env| | ||||
|       email = env.params.url["email"] | ||||
|       money = env.params.url["money"] | ||||
|  |  | |||
|  | @ -17,32 +17,19 @@ end | |||
| 
 | ||||
| describe "Run" do | ||||
|   it "runs a code block after starting" do | ||||
|     run(<<-CR).should eq "started\nstopped\n" | ||||
|       Kemal.config.env = "test" | ||||
|       Kemal.run do | ||||
|         puts "started" | ||||
|         Kemal.stop | ||||
|         puts "stopped" | ||||
|       end | ||||
|       CR | ||||
|     Kemal.config.env = "test" | ||||
|     make_me_true = false | ||||
|     Kemal.run do | ||||
|       make_me_true = true | ||||
|       Kemal.stop | ||||
|     end | ||||
|     make_me_true.should be_true | ||||
|   end | ||||
| 
 | ||||
|   it "runs without a block being specified" do | ||||
|     run(<<-CR).should eq "[test] Kemal is ready to lead at http://0.0.0.0:3000\ntrue\n" | ||||
|       Kemal.config.env = "test" | ||||
|       Kemal.run | ||||
|       puts Kemal.config.running | ||||
|       CR | ||||
|   end | ||||
| 
 | ||||
|   it "allows custom HTTP::Server bind" do | ||||
|     run(<<-CR).should eq "[test] Kemal is ready to lead at http://127.0.0.1:3000, http://0.0.0.0:3001\n" | ||||
|       Kemal.config.env = "test" | ||||
|       Kemal.run do |config| | ||||
|         server = config.server.not_nil! | ||||
|         server.bind_tcp "127.0.0.1", 3000, reuse_port: true | ||||
|         server.bind_tcp "0.0.0.0", 3001, reuse_port: true | ||||
|       end | ||||
|       CR | ||||
|     Kemal.config.env = "test" | ||||
|     Kemal.run | ||||
|     Kemal.application.running?.should be_true | ||||
|     Kemal.stop | ||||
|   end | ||||
| end | ||||
|  |  | |||
|  | @ -1,5 +1,7 @@ | |||
| require "spec" | ||||
| require "../src/*" | ||||
| require "../src/**" | ||||
| require "../src/kemal/base" | ||||
| require "../src/kemal/dsl" | ||||
| 
 | ||||
| include Kemal | ||||
| 
 | ||||
|  | @ -33,6 +35,7 @@ def create_request_and_return_io_and_context(handler, request) | |||
|   io = IO::Memory.new | ||||
|   response = HTTP::Server::Response.new(io) | ||||
|   context = HTTP::Server::Context.new(request, response) | ||||
|   context.app = Kemal.application | ||||
|   handler.call(context) | ||||
|   response.close | ||||
|   io.rewind | ||||
|  | @ -43,6 +46,7 @@ def create_ws_request_and_return_io_and_context(handler, request) | |||
|   io = IO::Memory.new | ||||
|   response = HTTP::Server::Response.new(io) | ||||
|   context = HTTP::Server::Context.new(request, response) | ||||
|   context.app = Kemal.application | ||||
|   begin | ||||
|     handler.call context | ||||
|   rescue IO::Error | ||||
|  | @ -64,10 +68,10 @@ def call_request_on_app(request) | |||
| end | ||||
| 
 | ||||
| def build_main_handler | ||||
|   Kemal.config.setup | ||||
|   main_handler = Kemal.config.handlers.first | ||||
|   Kemal.application.setup | ||||
|   main_handler = Kemal.application.handlers.first | ||||
|   current_handler = main_handler | ||||
|   Kemal.config.handlers.each do |handler| | ||||
|   Kemal.application.handlers.each_with_index do |handler, index| | ||||
|     current_handler.next = handler | ||||
|     current_handler = handler | ||||
|   end | ||||
|  | @ -81,8 +85,5 @@ Spec.before_each do | |||
| end | ||||
| 
 | ||||
| Spec.after_each do | ||||
|   Kemal.config.clear | ||||
|   Kemal::RouteHandler::INSTANCE.routes = Radix::Tree(Route).new | ||||
|   Kemal::RouteHandler::INSTANCE.cached_routes = Hash(String, Radix::Result(Route)).new | ||||
|   Kemal::WebSocketHandler::INSTANCE.routes = Radix::Tree(WebSocket).new | ||||
|   Kemal.application.clear | ||||
| end | ||||
|  |  | |||
|  | @ -2,8 +2,8 @@ require "./spec_helper" | |||
| 
 | ||||
| describe "Kemal::WebSocketHandler" do | ||||
|   it "doesn't match on wrong route" do | ||||
|     handler = Kemal::WebSocketHandler::INSTANCE | ||||
|     handler.next = Kemal::RouteHandler::INSTANCE | ||||
|     handler = Kemal::WebSocketHandler.new | ||||
|     handler.next = Kemal::RouteHandler.new | ||||
|     ws "/" { } | ||||
|     headers = HTTP::Headers{ | ||||
|       "Upgrade"           => "websocket", | ||||
|  | @ -14,6 +14,7 @@ describe "Kemal::WebSocketHandler" do | |||
|     io = IO::Memory.new | ||||
|     response = HTTP::Server::Response.new(io) | ||||
|     context = HTTP::Server::Context.new(request, response) | ||||
|     context.app = Kemal.application | ||||
| 
 | ||||
|     expect_raises(Kemal::Exceptions::RouteNotFound) do | ||||
|       handler.call context | ||||
|  | @ -21,9 +22,9 @@ describe "Kemal::WebSocketHandler" do | |||
|   end | ||||
| 
 | ||||
|   it "matches on given route" do | ||||
|     handler = Kemal::WebSocketHandler::INSTANCE | ||||
|     ws "/" { |socket| socket.send("Match") } | ||||
|     ws "/no_match" { |socket| socket.send "No Match" } | ||||
|     handler = Kemal::WebSocketHandler.new | ||||
|     ws "/" { |socket, context| socket.send("Match") } | ||||
|     ws "/no_match" { |socket, context| socket.send "No Match" } | ||||
|     headers = HTTP::Headers{ | ||||
|       "Upgrade"               => "websocket", | ||||
|       "Connection"            => "Upgrade", | ||||
|  | @ -37,8 +38,8 @@ describe "Kemal::WebSocketHandler" do | |||
|   end | ||||
| 
 | ||||
|   it "fetches named url parameters" do | ||||
|     handler = Kemal::WebSocketHandler::INSTANCE | ||||
|     ws "/:id" { |_, c| c.ws_route_lookup.params["id"] } | ||||
|     handler = Kemal::WebSocketHandler.new | ||||
|     ws "/:id" { |s, c| c.params.url["id"] } | ||||
|     headers = HTTP::Headers{ | ||||
|       "Upgrade"               => "websocket", | ||||
|       "Connection"            => "Upgrade", | ||||
|  | @ -51,14 +52,15 @@ describe "Kemal::WebSocketHandler" do | |||
|   end | ||||
| 
 | ||||
|   it "matches correct verb" do | ||||
|     handler = Kemal::WebSocketHandler::INSTANCE | ||||
|     handler.next = Kemal::RouteHandler::INSTANCE | ||||
|     handler = Kemal::WebSocketHandler.new | ||||
|     handler.next = Kemal::RouteHandler.new | ||||
|     ws "/" { } | ||||
|     get "/" { "get" } | ||||
|     request = HTTP::Request.new("GET", "/") | ||||
|     io = IO::Memory.new | ||||
|     response = HTTP::Server::Response.new(io) | ||||
|     context = HTTP::Server::Context.new(request, response) | ||||
|     context.app = Kemal.application | ||||
|     handler.call(context) | ||||
|     response.close | ||||
|     io.rewind | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue