2015-10-28 18:52:34 +00:00
|
|
|
require "./spec_helper"
|
|
|
|
|
|
|
|
describe "Context" do
|
2018-01-26 15:30:09 +00:00
|
|
|
context "headers" do
|
|
|
|
it "sets content type" do
|
|
|
|
get "/" do |env|
|
|
|
|
env.response.content_type = "application/json"
|
|
|
|
"Hello"
|
|
|
|
end
|
|
|
|
request = HTTP::Request.new("GET", "/")
|
|
|
|
client_response = call_request_on_app(request)
|
|
|
|
client_response.headers["Content-Type"].should eq("application/json")
|
2015-10-28 18:52:34 +00:00
|
|
|
end
|
2015-10-30 15:06:25 +00:00
|
|
|
|
2018-01-26 15:30:09 +00:00
|
|
|
it "parses headers" do
|
|
|
|
get "/" do |env|
|
|
|
|
name = env.request.headers["name"]
|
|
|
|
"Hello #{name}"
|
|
|
|
end
|
|
|
|
headers = HTTP::Headers.new
|
|
|
|
headers["name"] = "kemal"
|
|
|
|
request = HTTP::Request.new("GET", "/", headers)
|
|
|
|
client_response = call_request_on_app(request)
|
|
|
|
client_response.body.should eq "Hello kemal"
|
2015-10-30 15:06:25 +00:00
|
|
|
end
|
2015-10-30 20:01:18 +00:00
|
|
|
|
2018-01-26 15:30:09 +00:00
|
|
|
it "sets response headers" do
|
|
|
|
get "/" do |env|
|
|
|
|
env.response.headers.add "Accept-Language", "tr"
|
|
|
|
end
|
|
|
|
request = HTTP::Request.new("GET", "/")
|
|
|
|
client_response = call_request_on_app(request)
|
|
|
|
client_response.headers["Accept-Language"].should eq "tr"
|
2015-10-30 20:01:18 +00:00
|
|
|
end
|
|
|
|
end
|
2016-07-14 18:56:01 +00:00
|
|
|
|
2018-01-26 15:30:09 +00:00
|
|
|
context "storage" do
|
|
|
|
it "can store primitive types" do
|
|
|
|
before_get "/" do |env|
|
|
|
|
env.set "before_get", "Kemal"
|
|
|
|
env.set "before_get_int", 123
|
|
|
|
env.set "before_get_float", 3.5
|
|
|
|
end
|
|
|
|
|
|
|
|
get "/" do |env|
|
|
|
|
{
|
2018-01-26 15:32:24 +00:00
|
|
|
before_get: env.get("before_get"),
|
|
|
|
before_get_int: env.get("before_get_int"),
|
|
|
|
before_get_float: env.get("before_get_float"),
|
2018-01-26 15:30:09 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
request = HTTP::Request.new("GET", "/")
|
|
|
|
io = IO::Memory.new
|
|
|
|
response = HTTP::Server::Response.new(io)
|
|
|
|
context = HTTP::Server::Context.new(request, response)
|
|
|
|
Kemal::FilterHandler::INSTANCE.call(context)
|
|
|
|
Kemal::RouteHandler::INSTANCE.call(context)
|
|
|
|
|
|
|
|
context.get("before_get").should eq "Kemal"
|
|
|
|
context.get("before_get_int").should eq 123
|
|
|
|
context.get("before_get_float").should eq 3.5
|
2016-07-14 18:56:01 +00:00
|
|
|
end
|
|
|
|
|
2018-01-26 15:30:09 +00:00
|
|
|
it "can store custom types" do
|
|
|
|
before_get "/" do |env|
|
|
|
|
t = TestContextStorageType.new
|
|
|
|
t.id = 32
|
|
|
|
a = AnotherContextStorageType.new
|
|
|
|
|
|
|
|
env.set "before_get_context_test", t
|
|
|
|
env.set "another_context_test", a
|
|
|
|
end
|
|
|
|
|
|
|
|
get "/" do |env|
|
|
|
|
{
|
|
|
|
before_get_context_test: env.get("before_get_context_test"),
|
2018-01-26 15:32:24 +00:00
|
|
|
another_context_test: env.get("another_context_test"),
|
2018-01-26 15:30:09 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
request = HTTP::Request.new("GET", "/")
|
|
|
|
io = IO::Memory.new
|
|
|
|
response = HTTP::Server::Response.new(io)
|
|
|
|
context = HTTP::Server::Context.new(request, response)
|
|
|
|
Kemal::FilterHandler::INSTANCE.call(context)
|
|
|
|
Kemal::RouteHandler::INSTANCE.call(context)
|
|
|
|
|
|
|
|
context.get("before_get_context_test").as(TestContextStorageType).id.should eq 32
|
|
|
|
context.get("another_context_test").as(AnotherContextStorageType).name.should eq "kemal-context"
|
2016-07-14 18:56:01 +00:00
|
|
|
end
|
2017-08-20 17:01:49 +00:00
|
|
|
|
2018-01-26 15:30:09 +00:00
|
|
|
it "fetches non-existent keys from store with get?" do
|
2018-01-26 15:32:24 +00:00
|
|
|
get "/" { }
|
2018-01-26 15:30:09 +00:00
|
|
|
|
|
|
|
request = HTTP::Request.new("GET", "/")
|
|
|
|
io = IO::Memory.new
|
|
|
|
response = HTTP::Server::Response.new(io)
|
|
|
|
context = HTTP::Server::Context.new(request, response)
|
|
|
|
Kemal::FilterHandler::INSTANCE.call(context)
|
|
|
|
Kemal::RouteHandler::INSTANCE.call(context)
|
|
|
|
|
2018-01-26 15:35:34 +00:00
|
|
|
context.get?("non_existent_key").should be_nil
|
|
|
|
context.get?("another_non_existent_key").should be_nil
|
2018-01-26 15:30:09 +00:00
|
|
|
end
|
2016-07-14 18:56:01 +00:00
|
|
|
end
|
2016-07-17 11:31:45 +00:00
|
|
|
end
|