Add context store
This commit is contained in:
parent
c9714faab3
commit
09d82ed74b
2 changed files with 40 additions and 1 deletions
|
@ -40,4 +40,32 @@ 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
|
||||||
|
|
||||||
|
it "can store variables" 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|
|
||||||
|
env.set "key", "value"
|
||||||
|
{
|
||||||
|
key: env.get("key"),
|
||||||
|
before_get: env.get("before_get"),
|
||||||
|
before_get_int: env.get("before_get_int"),
|
||||||
|
before_get_float: env.get("before_get_float")
|
||||||
|
}
|
||||||
|
end
|
||||||
|
request = HTTP::Request.new("GET", "/")
|
||||||
|
io = MemoryIO.new
|
||||||
|
response = HTTP::Server::Response.new(io)
|
||||||
|
context = HTTP::Server::Context.new(request, response)
|
||||||
|
Kemal::Middleware::Filter::INSTANCE.call(context)
|
||||||
|
Kemal::RouteHandler::INSTANCE.call(context)
|
||||||
|
context.store["key"].should eq "value"
|
||||||
|
context.store["before_get"].should eq "Kemal"
|
||||||
|
context.store["before_get_int"].should eq 123
|
||||||
|
context.store["before_get_float"].should eq 3.5
|
||||||
|
end
|
||||||
end
|
end
|
|
@ -2,6 +2,9 @@
|
||||||
# information such as params, content_type e.g
|
# information such as params, content_type e.g
|
||||||
class HTTP::Server
|
class HTTP::Server
|
||||||
class Context
|
class Context
|
||||||
|
alias StoreTypes = Nil | String | Int32 | Float64 | Bool
|
||||||
|
getter store = {} of String => StoreTypes
|
||||||
|
|
||||||
def params
|
def params
|
||||||
@request.url_params ||= route_lookup.params
|
@request.url_params ||= route_lookup.params
|
||||||
@params ||= Kemal::ParamParser.new(@request)
|
@params ||= Kemal::ParamParser.new(@request)
|
||||||
|
@ -24,5 +27,13 @@ class HTTP::Server
|
||||||
@session ||= Kemal::Sessions.new(self)
|
@session ||= Kemal::Sessions.new(self)
|
||||||
@session.not_nil!
|
@session.not_nil!
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def get(name)
|
||||||
|
@store[name]
|
||||||
|
end
|
||||||
|
|
||||||
|
def set(name, value)
|
||||||
|
@store[name] = value
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue