Introduce Kemal::InitHandler to initialize HTTP::Server::Context with defaults

This commit is contained in:
Sdogruyol 2016-07-17 18:11:26 +03:00
parent 725e051723
commit 09d9e708f1
10 changed files with 48 additions and 40 deletions

View file

@ -26,7 +26,7 @@ describe "Config" do
it "adds a custom handler" do
config = Kemal.config
config.add_handler CustomTestHandler.new
config.handlers.size.should eq(5)
config.handlers.size.should eq(6)
end
it "adds custom options" do

View file

@ -1,15 +1,6 @@
require "./spec_helper"
describe "Context" do
it "has a default content type" do
get "/" do |env|
"Hello"
end
request = HTTP::Request.new("GET", "/")
client_response = call_request_on_app(request)
client_response.headers["Content-Type"].should eq("text/html")
end
it "sets content type" do
get "/" do |env|
env.response.content_type = "application/json"

View file

@ -4,7 +4,7 @@ describe "Macros" do
describe "#basic_auth" do
it "adds HTTPBasicAuthHandler" do
basic_auth "serdar", "123"
Kemal.config.handlers.size.should eq 5
Kemal.config.handlers.size.should eq 6
end
end
@ -18,7 +18,7 @@ describe "Macros" do
describe "#add_handler" do
it "adds a custom handler" do
add_handler CustomTestHandler.new
Kemal.config.handlers.size.should eq 5
Kemal.config.handlers.size.should eq 6
end
end

21
spec/init_handler_spec.cr Normal file
View file

@ -0,0 +1,21 @@
require "./spec_helper"
describe "Kemal::InitHandler" do
it "initializes context with Content-Type: text/html" do
request = HTTP::Request.new("GET", "/")
io = MemoryIO.new
response = HTTP::Server::Response.new(io)
context = HTTP::Server::Context.new(request, response)
Kemal::InitHandler::INSTANCE.call(context)
context.response.headers["Content-Type"].should eq "text/html"
end
it "initializes context with X-Powered-By: Kemal" do
request = HTTP::Request.new("GET", "/")
io = MemoryIO.new
response = HTTP::Server::Response.new(io)
context = HTTP::Server::Context.new(request, response)
Kemal::InitHandler::INSTANCE.call(context)
context.response.headers["X-Powered-By"].should eq "Kemal"
end
end

View file

@ -155,20 +155,4 @@ describe "Kemal::RouteHandler" do
client_response.status_code.should eq(302)
client_response.headers.has_key?("Location").should eq(true)
end
it "sets default Content-Type to context html" do
get "/" do |env|
"Hello World from GET"
end
request = HTTP::Request.new("GET", "/")
client_response = call_request_on_app(request)
client_response.content_type.should eq("text/html")
end
it "sets X-Powered-By to Kemal" do
get "/" { }
request = HTTP::Request.new("GET", "/")
client_response = call_request_on_app(request)
client_response.headers["X-Powered-By"].should eq("Kemal")
end
end