2016-03-18 18:03:04 +00:00
|
|
|
require "./spec_helper"
|
|
|
|
|
2017-10-06 17:34:18 +00:00
|
|
|
describe "Kemal::ExceptionHandler" do
|
2016-07-03 09:13:11 +00:00
|
|
|
it "renders 404 on route not found" do
|
2018-05-17 08:07:40 +00:00
|
|
|
get "/" do
|
2016-07-03 09:13:11 +00:00
|
|
|
"Hello"
|
|
|
|
end
|
|
|
|
|
|
|
|
request = HTTP::Request.new("GET", "/asd")
|
2016-11-22 20:29:10 +00:00
|
|
|
io = IO::Memory.new
|
2016-07-03 09:13:11 +00:00
|
|
|
response = HTTP::Server::Response.new(io)
|
|
|
|
context = HTTP::Server::Context.new(request, response)
|
2017-10-06 17:34:18 +00:00
|
|
|
Kemal::ExceptionHandler::INSTANCE.call(context)
|
2016-07-03 09:13:11 +00:00
|
|
|
response.close
|
|
|
|
io.rewind
|
|
|
|
response = HTTP::Client::Response.from_io(io, decompress: false)
|
|
|
|
response.status_code.should eq 404
|
|
|
|
end
|
|
|
|
|
2016-07-19 20:29:00 +00:00
|
|
|
it "renders custom error" do
|
|
|
|
error 403 do
|
|
|
|
"403 error"
|
|
|
|
end
|
|
|
|
get "/" do |env|
|
|
|
|
env.response.status_code = 403
|
|
|
|
end
|
|
|
|
request = HTTP::Request.new("GET", "/")
|
2016-11-22 20:29:10 +00:00
|
|
|
io = IO::Memory.new
|
2016-07-19 20:29:00 +00:00
|
|
|
response = HTTP::Server::Response.new(io)
|
|
|
|
context = HTTP::Server::Context.new(request, response)
|
2017-10-06 17:34:18 +00:00
|
|
|
Kemal::ExceptionHandler::INSTANCE.next = Kemal::RouteHandler::INSTANCE
|
|
|
|
Kemal::ExceptionHandler::INSTANCE.call(context)
|
2016-07-19 20:29:00 +00:00
|
|
|
response.close
|
|
|
|
io.rewind
|
|
|
|
response = HTTP::Client::Response.from_io(io, decompress: false)
|
|
|
|
response.status_code.should eq 403
|
2016-11-06 08:58:18 +00:00
|
|
|
response.headers["Content-Type"].should eq "text/html"
|
2016-07-19 20:29:00 +00:00
|
|
|
response.body.should eq "403 error"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "renders custom 500 error" do
|
2018-05-17 08:07:40 +00:00
|
|
|
error 500 do
|
2016-07-19 20:29:00 +00:00
|
|
|
"Something happened"
|
|
|
|
end
|
|
|
|
get "/" do |env|
|
|
|
|
env.response.status_code = 500
|
|
|
|
end
|
|
|
|
request = HTTP::Request.new("GET", "/")
|
2016-11-22 20:29:10 +00:00
|
|
|
io = IO::Memory.new
|
2016-07-19 20:29:00 +00:00
|
|
|
response = HTTP::Server::Response.new(io)
|
|
|
|
context = HTTP::Server::Context.new(request, response)
|
2017-10-06 17:34:18 +00:00
|
|
|
Kemal::ExceptionHandler::INSTANCE.next = Kemal::RouteHandler::INSTANCE
|
|
|
|
Kemal::ExceptionHandler::INSTANCE.call(context)
|
2016-07-19 20:29:00 +00:00
|
|
|
response.close
|
|
|
|
io.rewind
|
|
|
|
response = HTTP::Client::Response.from_io(io, decompress: false)
|
|
|
|
response.status_code.should eq 500
|
2016-11-06 08:58:18 +00:00
|
|
|
response.headers["Content-Type"].should eq "text/html"
|
|
|
|
response.body.should eq "Something happened"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "keeps the specified error Content-Type" do
|
2018-05-17 08:07:40 +00:00
|
|
|
error 500 do
|
2016-11-06 08:58:18 +00:00
|
|
|
"Something happened"
|
|
|
|
end
|
|
|
|
get "/" do |env|
|
|
|
|
env.response.content_type = "application/json"
|
|
|
|
env.response.status_code = 500
|
|
|
|
end
|
|
|
|
request = HTTP::Request.new("GET", "/")
|
2016-11-22 20:29:10 +00:00
|
|
|
io = IO::Memory.new
|
2016-11-06 08:58:18 +00:00
|
|
|
response = HTTP::Server::Response.new(io)
|
|
|
|
context = HTTP::Server::Context.new(request, response)
|
2017-10-06 17:34:18 +00:00
|
|
|
Kemal::ExceptionHandler::INSTANCE.next = Kemal::RouteHandler::INSTANCE
|
|
|
|
Kemal::ExceptionHandler::INSTANCE.call(context)
|
2016-11-06 08:58:18 +00:00
|
|
|
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"
|
2016-07-19 20:29:00 +00:00
|
|
|
response.body.should eq "Something happened"
|
|
|
|
end
|
2016-11-16 17:27:01 +00:00
|
|
|
|
|
|
|
it "renders custom error with env and error" do
|
2018-05-17 08:07:40 +00:00
|
|
|
error 500 do |_, err|
|
2016-11-16 17:27:01 +00:00
|
|
|
err.message
|
|
|
|
end
|
|
|
|
get "/" do |env|
|
|
|
|
env.response.content_type = "application/json"
|
|
|
|
env.response.status_code = 500
|
|
|
|
end
|
|
|
|
request = HTTP::Request.new("GET", "/")
|
2016-11-22 20:29:10 +00:00
|
|
|
io = IO::Memory.new
|
2016-11-16 17:27:01 +00:00
|
|
|
response = HTTP::Server::Response.new(io)
|
|
|
|
context = HTTP::Server::Context.new(request, response)
|
2017-10-06 17:34:18 +00:00
|
|
|
Kemal::ExceptionHandler::INSTANCE.next = Kemal::RouteHandler::INSTANCE
|
|
|
|
Kemal::ExceptionHandler::INSTANCE.call(context)
|
2016-11-16 17:27:01 +00:00
|
|
|
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 "Rendered error with 500"
|
|
|
|
end
|
2016-03-18 18:03:04 +00:00
|
|
|
end
|