From 64733c10e08e7a45fef6a54fc203fad6286c0307 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Serdar=20Dogruyol=20-=20Sedo=20=E3=82=BB=E3=83=89?= <990485+sdogruyol@users.noreply.github.com> Date: Wed, 1 Sep 2021 18:26:38 +0300 Subject: [PATCH] Set content type to text/html for 500 exceptions (#616) --- spec/exception_handler_spec.cr | 24 ++++++++++++++++++++++++ src/kemal/helpers/templates.cr | 1 + 2 files changed, 25 insertions(+) diff --git a/spec/exception_handler_spec.cr b/spec/exception_handler_spec.cr index b9519e9..7064e84 100644 --- a/spec/exception_handler_spec.cr +++ b/spec/exception_handler_spec.cr @@ -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" diff --git a/src/kemal/helpers/templates.cr b/src/kemal/helpers/templates.cr index b343fc8..0468d38 100644 --- a/src/kemal/helpers/templates.cr +++ b/src/kemal/helpers/templates.cr @@ -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