From eb4c82043a5c1a7a683975981bd4be31f1ab5589 Mon Sep 17 00:00:00 2001 From: sdogruyol Date: Tue, 5 Jan 2016 14:20:40 +0200 Subject: [PATCH] Add add_handler macro --- spec/config_spec.cr | 6 ------ spec/macros_spec.cr | 7 +++++++ spec/spec_helper.cr | 6 ++++++ src/kemal/macros.cr | 6 +++++- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/spec/config_spec.cr b/spec/config_spec.cr index f9a5f5b..56441d4 100644 --- a/spec/config_spec.cr +++ b/spec/config_spec.cr @@ -1,11 +1,5 @@ require "./spec_helper" -class CustomTestHandler < HTTP::Handler - def call(request) - call_next request - end -end - describe "Config" do it "sets default port to 3000" do config = Kemal.config diff --git a/spec/macros_spec.cr b/spec/macros_spec.cr index 1e663ab..cceed89 100644 --- a/spec/macros_spec.cr +++ b/spec/macros_spec.cr @@ -14,4 +14,11 @@ describe "Macros" do Kemal.config.public_folder.should eq("/some/path/to/folder") end end + + describe "#add_handler" do + it "adds a custom handler" do + add_handler CustomTestHandler.new + Kemal.config.handlers.size.should eq 1 + end + end end diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index ae32780..ed07232 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -4,6 +4,12 @@ require "../src/kemal/middleware/*" include Kemal +class CustomTestHandler < HTTP::Handler + def call(request) + call_next request + end +end + Spec.before_each do Kemal.config.env = "development" Kemal.config.handlers.clear diff --git a/src/kemal/macros.cr b/src/kemal/macros.cr index 4622afe..972d2aa 100644 --- a/src/kemal/macros.cr +++ b/src/kemal/macros.cr @@ -21,10 +21,14 @@ macro redirect(url) env.response.status_code = 301 end +macro add_handler(handler) + Kemal.config.add_handler {{handler}} +end + # Uses Kemal::Middleware::HTTPBasicAuth to easily add HTTP Basic Auth support. macro basic_auth(username, password) auth_handler = Kemal::Middleware::HTTPBasicAuth.new({{username}}, {{password}}) - Kemal.config.add_handler auth_handler + add_handler auth_handler end macro public_folder(path)