Add Kemal::Context.get? to safely access context storage. Fixes #428
This commit is contained in:
parent
17bd3dce37
commit
4034dab952
2 changed files with 98 additions and 58 deletions
|
@ -1,71 +1,107 @@
|
||||||
require "./spec_helper"
|
require "./spec_helper"
|
||||||
|
|
||||||
describe "Context" do
|
describe "Context" do
|
||||||
it "sets content type" do
|
context "headers" do
|
||||||
get "/" do |env|
|
it "sets content type" do
|
||||||
env.response.content_type = "application/json"
|
get "/" do |env|
|
||||||
"Hello"
|
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")
|
||||||
|
end
|
||||||
|
|
||||||
|
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"
|
||||||
|
end
|
||||||
|
|
||||||
|
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"
|
||||||
end
|
end
|
||||||
request = HTTP::Request.new("GET", "/")
|
|
||||||
client_response = call_request_on_app(request)
|
|
||||||
client_response.headers["Content-Type"].should eq("application/json")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "parses headers" do
|
context "storage" do
|
||||||
get "/" do |env|
|
it "can store primitive types" do
|
||||||
name = env.request.headers["name"]
|
before_get "/" do |env|
|
||||||
"Hello #{name}"
|
env.set "before_get", "Kemal"
|
||||||
end
|
env.set "before_get_int", 123
|
||||||
headers = HTTP::Headers.new
|
env.set "before_get_float", 3.5
|
||||||
headers["name"] = "kemal"
|
end
|
||||||
request = HTTP::Request.new("GET", "/", headers)
|
|
||||||
client_response = call_request_on_app(request)
|
|
||||||
client_response.body.should eq "Hello kemal"
|
|
||||||
end
|
|
||||||
|
|
||||||
it "sets response headers" do
|
get "/" do |env|
|
||||||
get "/" do |env|
|
{
|
||||||
env.response.headers.add "Accept-Language", "tr"
|
before_get: env.get("before_get"),
|
||||||
end
|
before_get_int: env.get("before_get_int"),
|
||||||
request = HTTP::Request.new("GET", "/")
|
before_get_float: env.get("before_get_float")
|
||||||
client_response = call_request_on_app(request)
|
}
|
||||||
client_response.headers["Accept-Language"].should eq "tr"
|
end
|
||||||
end
|
|
||||||
|
|
||||||
it "can store variables" do
|
request = HTTP::Request.new("GET", "/")
|
||||||
before_get "/" do |env|
|
io = IO::Memory.new
|
||||||
t = TestContextStorageType.new
|
response = HTTP::Server::Response.new(io)
|
||||||
t.id = 32
|
context = HTTP::Server::Context.new(request, response)
|
||||||
a = AnotherContextStorageType.new
|
Kemal::FilterHandler::INSTANCE.call(context)
|
||||||
env.set "key", "value"
|
Kemal::RouteHandler::INSTANCE.call(context)
|
||||||
env.set "before_get", "Kemal"
|
|
||||||
env.set "before_get_int", 123
|
context.get("before_get").should eq "Kemal"
|
||||||
env.set "before_get_context_test", t
|
context.get("before_get_int").should eq 123
|
||||||
env.set "another_context_test", a
|
context.get("before_get_float").should eq 3.5
|
||||||
env.set "before_get_float", 3.5
|
|
||||||
end
|
end
|
||||||
|
|
||||||
get "/" do |env|
|
it "can store custom types" do
|
||||||
env.set "key", "value"
|
before_get "/" do |env|
|
||||||
{
|
t = TestContextStorageType.new
|
||||||
key: env.get("key"),
|
t.id = 32
|
||||||
before_get: env.get("before_get"),
|
a = AnotherContextStorageType.new
|
||||||
before_get_int: env.get("before_get_int"),
|
|
||||||
before_get_float: env.get("before_get_float"),
|
env.set "before_get_context_test", t
|
||||||
before_get_context_test: env.get("before_get_context_test"),
|
env.set "another_context_test", a
|
||||||
}
|
end
|
||||||
|
|
||||||
|
get "/" do |env|
|
||||||
|
{
|
||||||
|
before_get_context_test: env.get("before_get_context_test"),
|
||||||
|
another_context_test: env.get("another_context_test"),
|
||||||
|
}
|
||||||
|
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"
|
||||||
end
|
end
|
||||||
|
|
||||||
request = HTTP::Request.new("GET", "/")
|
it "fetches non-existent keys from store with get?" do
|
||||||
io = IO::Memory.new
|
get "/" {}
|
||||||
response = HTTP::Server::Response.new(io)
|
|
||||||
context = HTTP::Server::Context.new(request, response)
|
request = HTTP::Request.new("GET", "/")
|
||||||
Kemal::FilterHandler::INSTANCE.call(context)
|
io = IO::Memory.new
|
||||||
Kemal::RouteHandler::INSTANCE.call(context)
|
response = HTTP::Server::Response.new(io)
|
||||||
context.store["key"].should eq "value"
|
context = HTTP::Server::Context.new(request, response)
|
||||||
context.store["before_get"].should eq "Kemal"
|
Kemal::FilterHandler::INSTANCE.call(context)
|
||||||
context.store["before_get_int"].should eq 123
|
Kemal::RouteHandler::INSTANCE.call(context)
|
||||||
context.store["before_get_float"].should eq 3.5
|
|
||||||
context.store["before_get_context_test"].as(TestContextStorageType).id.should eq 32
|
context.get?("non_existent_key").should eq nil
|
||||||
|
context.get?("another_non_existent_key").should eq nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,7 +10,7 @@ class HTTP::Server
|
||||||
|
|
||||||
macro finished
|
macro finished
|
||||||
alias StoreTypes = Union({{ *STORE_MAPPINGS }})
|
alias StoreTypes = Union({{ *STORE_MAPPINGS }})
|
||||||
getter store = {} of String => StoreTypes
|
@store = {} of String => StoreTypes
|
||||||
end
|
end
|
||||||
|
|
||||||
def params
|
def params
|
||||||
|
@ -58,5 +58,9 @@ class HTTP::Server
|
||||||
def set(name : String, value : StoreTypes)
|
def set(name : String, value : StoreTypes)
|
||||||
@store[name] = value
|
@store[name] = value
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def get?(name : String)
|
||||||
|
@store[name]?
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue