Skip to content

Commit

Permalink
Set content type to text/html for 500 exceptions (#616)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdogruyol committed Sep 1, 2021
1 parent 14aabb8 commit 64733c1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
24 changes: 24 additions & 0 deletions spec/exception_handler_spec.cr
Expand Up @@ -59,6 +59,30 @@ describe "Kemal::ExceptionHandler" do
response.body.should eq "Something happened"
end

it "overrides the content type for filters" do
before_get do |env|
env.response.content_type = "application/json"
end
error 500 do |_, err|
err.message
end
get "/" do |env|
env.response.status_code = 500
end
request = HTTP::Request.new("GET", "/")
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)
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 "text/html"
response.body.should eq "Rendered error with 500"
end

it "keeps the specified error Content-Type" do
error 500 do
"Something happened"
Expand Down
1 change: 1 addition & 0 deletions src/kemal/helpers/templates.cr
Expand Up @@ -22,6 +22,7 @@ def render_404
end

def render_500(context, exception, verbosity)
context.response.content_type = "text/html"
context.response.status_code = 500

template = if verbosity
Expand Down

0 comments on commit 64733c1

Please sign in to comment.