Introduce Kemal::InitHandler to initialize HTTP::Server::Context with defaults
This commit is contained in:
parent
725e051723
commit
09d9e708f1
10 changed files with 48 additions and 40 deletions
|
@ -26,7 +26,7 @@ describe "Config" do
|
|||
it "adds a custom handler" do
|
||||
config = Kemal.config
|
||||
config.add_handler CustomTestHandler.new
|
||||
config.handlers.size.should eq(5)
|
||||
config.handlers.size.should eq(6)
|
||||
end
|
||||
|
||||
it "adds custom options" do
|
||||
|
|
|
@ -1,15 +1,6 @@
|
|||
require "./spec_helper"
|
||||
|
||||
describe "Context" do
|
||||
it "has a default content type" do
|
||||
get "/" do |env|
|
||||
"Hello"
|
||||
end
|
||||
request = HTTP::Request.new("GET", "/")
|
||||
client_response = call_request_on_app(request)
|
||||
client_response.headers["Content-Type"].should eq("text/html")
|
||||
end
|
||||
|
||||
it "sets content type" do
|
||||
get "/" do |env|
|
||||
env.response.content_type = "application/json"
|
||||
|
|
|
@ -4,7 +4,7 @@ describe "Macros" do
|
|||
describe "#basic_auth" do
|
||||
it "adds HTTPBasicAuthHandler" do
|
||||
basic_auth "serdar", "123"
|
||||
Kemal.config.handlers.size.should eq 5
|
||||
Kemal.config.handlers.size.should eq 6
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -18,7 +18,7 @@ describe "Macros" do
|
|||
describe "#add_handler" do
|
||||
it "adds a custom handler" do
|
||||
add_handler CustomTestHandler.new
|
||||
Kemal.config.handlers.size.should eq 5
|
||||
Kemal.config.handlers.size.should eq 6
|
||||
end
|
||||
end
|
||||
|
||||
|
|
21
spec/init_handler_spec.cr
Normal file
21
spec/init_handler_spec.cr
Normal file
|
@ -0,0 +1,21 @@
|
|||
require "./spec_helper"
|
||||
|
||||
describe "Kemal::InitHandler" do
|
||||
it "initializes context with Content-Type: text/html" do
|
||||
request = HTTP::Request.new("GET", "/")
|
||||
io = MemoryIO.new
|
||||
response = HTTP::Server::Response.new(io)
|
||||
context = HTTP::Server::Context.new(request, response)
|
||||
Kemal::InitHandler::INSTANCE.call(context)
|
||||
context.response.headers["Content-Type"].should eq "text/html"
|
||||
end
|
||||
|
||||
it "initializes context with X-Powered-By: Kemal" do
|
||||
request = HTTP::Request.new("GET", "/")
|
||||
io = MemoryIO.new
|
||||
response = HTTP::Server::Response.new(io)
|
||||
context = HTTP::Server::Context.new(request, response)
|
||||
Kemal::InitHandler::INSTANCE.call(context)
|
||||
context.response.headers["X-Powered-By"].should eq "Kemal"
|
||||
end
|
||||
end
|
|
@ -155,20 +155,4 @@ describe "Kemal::RouteHandler" do
|
|||
client_response.status_code.should eq(302)
|
||||
client_response.headers.has_key?("Location").should eq(true)
|
||||
end
|
||||
|
||||
it "sets default Content-Type to context html" do
|
||||
get "/" do |env|
|
||||
"Hello World from GET"
|
||||
end
|
||||
request = HTTP::Request.new("GET", "/")
|
||||
client_response = call_request_on_app(request)
|
||||
client_response.content_type.should eq("text/html")
|
||||
end
|
||||
|
||||
it "sets X-Powered-By to Kemal" do
|
||||
get "/" { }
|
||||
request = HTTP::Request.new("GET", "/")
|
||||
client_response = call_request_on_app(request)
|
||||
client_response.headers["X-Powered-By"].should eq("Kemal")
|
||||
end
|
||||
end
|
||||
|
|
|
@ -7,12 +7,10 @@ module Kemal
|
|||
begin
|
||||
call_next(context)
|
||||
rescue Kemal::Exceptions::RouteNotFound
|
||||
context.response.content_type = "text/html"
|
||||
call_exception_with_status_code(context, 404)
|
||||
rescue Kemal::Exceptions::CustomException
|
||||
call_exception_with_status_code(context, context.response.status_code)
|
||||
rescue ex : Exception
|
||||
context.response.content_type = "text/html"
|
||||
Kemal.config.logger.write("Exception: #{ex.inspect_with_backtrace}\n")
|
||||
return call_exception_with_status_code(context, 500) if Kemal.config.error_handlers.has_key?(500)
|
||||
verbosity = Kemal.config.env == "production" ? false : true
|
||||
|
|
|
@ -58,33 +58,38 @@ module Kemal
|
|||
ERROR_HANDLERS[status_code] = ->(context : HTTP::Server::Context) { handler.call(context).to_s }
|
||||
end
|
||||
|
||||
def extra_options(&@extra_options : OptionParser ->)
|
||||
end
|
||||
|
||||
def setup
|
||||
setup_init_handler
|
||||
setup_log_handler
|
||||
setup_error_handler
|
||||
setup_static_file_handler
|
||||
end
|
||||
|
||||
def setup_log_handler
|
||||
private def setup_init_handler
|
||||
HANDLERS.insert(0, Kemal::InitHandler::INSTANCE)
|
||||
end
|
||||
|
||||
private def setup_log_handler
|
||||
@logger ||= if @logging
|
||||
Kemal::CommonLogHandler.new
|
||||
else
|
||||
Kemal::NullLogHandler.new
|
||||
end
|
||||
HANDLERS.insert(0, @logger.not_nil!)
|
||||
end
|
||||
|
||||
def extra_options(&@extra_options : OptionParser ->)
|
||||
HANDLERS.insert(1, @logger.not_nil!)
|
||||
end
|
||||
|
||||
private def setup_error_handler
|
||||
if @always_rescue
|
||||
@error_handler ||= Kemal::CommonExceptionHandler.new
|
||||
HANDLERS.insert(1, @error_handler.not_nil!)
|
||||
HANDLERS.insert(2, @error_handler.not_nil!)
|
||||
end
|
||||
end
|
||||
|
||||
private def setup_static_file_handler
|
||||
HANDLERS.insert(2, Kemal::StaticFileHandler.new(@public_folder)) if @serve_static
|
||||
HANDLERS.insert(3, Kemal::StaticFileHandler.new(@public_folder)) if @serve_static
|
||||
end
|
||||
end
|
||||
|
||||
|
|
12
src/kemal/init_handler.cr
Normal file
12
src/kemal/init_handler.cr
Normal file
|
@ -0,0 +1,12 @@
|
|||
module Kemal
|
||||
# Kemal::InitHandler is the first handler thus initializes the context with default values such as
|
||||
# Content-Type, X-Powered-By.
|
||||
class InitHandler < HTTP::Handler
|
||||
INSTANCE = new
|
||||
|
||||
def call(context)
|
||||
context.response.content_type = "text/html"
|
||||
context.response.headers.add "X-Powered-By", "Kemal"
|
||||
end
|
||||
end
|
||||
end
|
|
@ -13,8 +13,6 @@ module Kemal
|
|||
end
|
||||
|
||||
def call(context)
|
||||
context.response.headers.add "X-Powered-By", "Kemal"
|
||||
context.response.content_type = "text/html" unless context.response.headers.has_key?("Content-Type")
|
||||
process_request(context)
|
||||
end
|
||||
|
||||
|
|
|
@ -48,7 +48,6 @@ def render_500(context, backtrace, verbosity)
|
|||
</body>
|
||||
</html>
|
||||
HTML
|
||||
context.response.content_type = "text/html"
|
||||
context.response.status_code = 500
|
||||
context.response.print template
|
||||
context
|
||||
|
|
Loading…
Reference in a new issue