mirror of
https://gitea.invidious.io/iv-org/shard-kemal.git
synced 2024-08-15 00:53:36 +00:00
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,6 +1,7 @@
|
||||||
require "./spec_helper"
|
require "./spec_helper"
|
||||||
|
|
||||||
describe "Context" do
|
describe "Context" do
|
||||||
|
context "headers" do
|
||||||
it "sets content type" do
|
it "sets content type" do
|
||||||
get "/" do |env|
|
get "/" do |env|
|
||||||
env.response.content_type = "application/json"
|
env.response.content_type = "application/json"
|
||||||
|
@ -31,28 +32,21 @@ describe "Context" do
|
||||||
client_response = call_request_on_app(request)
|
client_response = call_request_on_app(request)
|
||||||
client_response.headers["Accept-Language"].should eq "tr"
|
client_response.headers["Accept-Language"].should eq "tr"
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
it "can store variables" do
|
context "storage" do
|
||||||
|
it "can store primitive types" do
|
||||||
before_get "/" do |env|
|
before_get "/" do |env|
|
||||||
t = TestContextStorageType.new
|
|
||||||
t.id = 32
|
|
||||||
a = AnotherContextStorageType.new
|
|
||||||
env.set "key", "value"
|
|
||||||
env.set "before_get", "Kemal"
|
env.set "before_get", "Kemal"
|
||||||
env.set "before_get_int", 123
|
env.set "before_get_int", 123
|
||||||
env.set "before_get_context_test", t
|
|
||||||
env.set "another_context_test", a
|
|
||||||
env.set "before_get_float", 3.5
|
env.set "before_get_float", 3.5
|
||||||
end
|
end
|
||||||
|
|
||||||
get "/" do |env|
|
get "/" do |env|
|
||||||
env.set "key", "value"
|
|
||||||
{
|
{
|
||||||
key: env.get("key"),
|
|
||||||
before_get: env.get("before_get"),
|
before_get: env.get("before_get"),
|
||||||
before_get_int: env.get("before_get_int"),
|
before_get_int: env.get("before_get_int"),
|
||||||
before_get_float: env.get("before_get_float"),
|
before_get_float: env.get("before_get_float")
|
||||||
before_get_context_test: env.get("before_get_context_test"),
|
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -62,10 +56,52 @@ describe "Context" do
|
||||||
context = HTTP::Server::Context.new(request, response)
|
context = HTTP::Server::Context.new(request, response)
|
||||||
Kemal::FilterHandler::INSTANCE.call(context)
|
Kemal::FilterHandler::INSTANCE.call(context)
|
||||||
Kemal::RouteHandler::INSTANCE.call(context)
|
Kemal::RouteHandler::INSTANCE.call(context)
|
||||||
context.store["key"].should eq "value"
|
|
||||||
context.store["before_get"].should eq "Kemal"
|
context.get("before_get").should eq "Kemal"
|
||||||
context.store["before_get_int"].should eq 123
|
context.get("before_get_int").should eq 123
|
||||||
context.store["before_get_float"].should eq 3.5
|
context.get("before_get_float").should eq 3.5
|
||||||
context.store["before_get_context_test"].as(TestContextStorageType).id.should eq 32
|
end
|
||||||
|
|
||||||
|
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"),
|
||||||
|
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
|
||||||
|
|
||||||
|
it "fetches non-existent keys from store with get?" do
|
||||||
|
get "/" {}
|
||||||
|
|
||||||
|
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?("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