Set default exception Content-Type to text/html. Fixes #202

This commit is contained in:
sdogruyol 2016-11-06 11:58:18 +03:00
parent 87d88318de
commit 4267a5eea9
2 changed files with 27 additions and 3 deletions

View file

@ -34,11 +34,12 @@ describe "Kemal::CommonExceptionHandler" do
io.rewind
response = HTTP::Client::Response.from_io(io, decompress: false)
response.status_code.should eq 403
response.headers["Content-Type"].should eq "text/html"
response.body.should eq "403 error"
end
it "renders custom 500 error" do
error 500 do
error 500 do |env|
"Something happened"
end
get "/" do |env|
@ -54,6 +55,29 @@ describe "Kemal::CommonExceptionHandler" do
io.rewind
response = HTTP::Client::Response.from_io(io, decompress: false)
response.status_code.should eq 500
response.headers["Content-Type"].should eq "text/html"
response.body.should eq "Something happened"
end
it "keeps the specified error Content-Type" do
error 500 do |env|
"Something happened"
end
get "/" do |env|
env.response.content_type = "application/json"
env.response.status_code = 500
end
request = HTTP::Request.new("GET", "/")
io = MemoryIO.new
response = HTTP::Server::Response.new(io)
context = HTTP::Server::Context.new(request, response)
Kemal::CommonExceptionHandler::INSTANCE.next = Kemal::RouteHandler::INSTANCE
Kemal::CommonExceptionHandler::INSTANCE.call(context)
response.close
io.rewind
response = HTTP::Client::Response.from_io(io, decompress: false)
response.status_code.should eq 500
response.headers["Content-Type"].should eq "application/json"
response.body.should eq "Something happened"
end
end

View file

@ -20,9 +20,9 @@ module Kemal
def call_exception_with_status_code(context, status_code)
if Kemal.config.error_handlers.has_key?(status_code)
context.response.status_code = status_code
context.response.content_type = "text/html" unless context.response.headers.has_key?("Content-Type")
context.response.print Kemal.config.error_handlers[status_code].call(context)
return context
context
end
end
end