Decouple specs from global state to isolated tests

This commit is contained in:
Johannes Müller 2017-07-18 00:42:05 +02:00 committed by sdogruyol
parent 29b18c927c
commit 2e42b3f48c
19 changed files with 336 additions and 304 deletions

View file

@ -1,18 +1,13 @@
require "./spec_helper"
private INSTANCE = Kemal::ExceptionHandler.new
require "./dsl_helper"
describe "Kemal::ExceptionHandler" do
it "renders 404 on route not found" do
get "/" do
"Hello"
end
request = HTTP::Request.new("GET", "/asd")
io = IO::Memory.new
response = HTTP::Server::Response.new(io)
context = HTTP::Server::Context.new(request, response)
INSTANCE.call(context)
subject = Kemal::ExceptionHandler.new(Kemal::Base.new)
subject.call(context)
response.close
io.rewind
response = HTTP::Client::Response.from_io(io, decompress: false)
@ -20,19 +15,21 @@ describe "Kemal::ExceptionHandler" do
end
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", "/")
io = IO::Memory.new
response = HTTP::Server::Response.new(io)
context = HTTP::Server::Context.new(request, response)
context.app = Kemal.application
INSTANCE.next = Kemal::RouteHandler.new
INSTANCE.call(context)
app = Kemal::Base.new
app.error 403 do
"403 error"
end
app.get "/" do |env|
env.response.status_code = 403
end
context.app = app
subject = Kemal::ExceptionHandler.new(app)
subject.next = Kemal::RouteHandler.new
subject.call(context)
response.close
io.rewind
response = HTTP::Client::Response.from_io(io, decompress: false)
@ -42,19 +39,21 @@ describe "Kemal::ExceptionHandler" do
end
it "renders custom 500 error" do
error 500 do
"Something happened"
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)
context.app = Kemal.application
INSTANCE.next = Kemal::RouteHandler.new
INSTANCE.call(context)
app = Kemal::Base.new
app.error 500 do |env|
"Something happened"
end
app.get "/" do |env|
env.response.status_code = 500
end
context.app = app
subject = Kemal::ExceptionHandler.new(app)
subject.next = Kemal::RouteHandler.new
subject.call(context)
response.close
io.rewind
response = HTTP::Client::Response.from_io(io, decompress: false)
@ -64,20 +63,22 @@ describe "Kemal::ExceptionHandler" do
end
it "keeps the specified error Content-Type" do
error 500 do
"Something happened"
end
get "/" do |env|
env.response.content_type = "application/json"
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)
context.app = Kemal.application
INSTANCE.next = Kemal::RouteHandler.new
INSTANCE.call(context)
app = Kemal::Base.new
app.error 500 do |env|
"Something happened"
end
app.get "/" do |env|
env.response.content_type = "application/json"
env.response.status_code = 500
end
context.app = app
subject = Kemal::ExceptionHandler.new(app)
subject.next = Kemal::RouteHandler.new
subject.call(context)
response.close
io.rewind
response = HTTP::Client::Response.from_io(io, decompress: false)
@ -87,20 +88,22 @@ describe "Kemal::ExceptionHandler" do
end
it "renders custom error with env and error" do
error 500 do |_, err|
err.message
end
get "/" do |env|
env.response.content_type = "application/json"
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)
context.app = Kemal.application
INSTANCE.next = Kemal::RouteHandler.new
INSTANCE.call(context)
app = Kemal::Base.new
app.error 500 do |env, err|
err.message
end
app.get "/" do |env|
env.response.content_type = "application/json"
env.response.status_code = 500
end
context.app = app
subject = Kemal::ExceptionHandler.new(Kemal::Base.new)
subject.next = Kemal::RouteHandler.new
subject.call(context)
response.close
io.rewind
response = HTTP::Client::Response.from_io(io, decompress: false)