mirror of
https://gitea.invidious.io/iv-org/shard-kemal.git
synced 2024-08-15 00:53:36 +00:00
parent
5d71c76554
commit
e43b2a3a01
3 changed files with 23 additions and 2 deletions
|
@ -33,6 +33,23 @@ describe "ParamParser" do
|
|||
url_params["hasan"].should eq "cemal"
|
||||
end
|
||||
|
||||
it "decodes url params" do
|
||||
kemal = Kemal::RouteHandler::INSTANCE
|
||||
kemal.add_route "POST", "/hello/:email/:money/:spanish" do |env|
|
||||
email = env.params.url["email"]
|
||||
money = env.params.url["money"]
|
||||
spanish = env.params.url["spanish"]
|
||||
"Hello, #{email}. You have #{money}. The spanish word of the day is #{spanish}."
|
||||
end
|
||||
request = HTTP::Request.new("POST", "/hello/sam%2Bspec%40gmail.com/%2419.99/a%C3%B1o")
|
||||
# Radix tree MUST be run to parse url params.
|
||||
io_with_context = create_request_and_return_io(kemal, request)
|
||||
url_params = Kemal::ParamParser.new(request).url
|
||||
url_params["email"].should eq "sam+spec@gmail.com"
|
||||
url_params["money"].should eq "$19.99"
|
||||
url_params["spanish"].should eq "año"
|
||||
end
|
||||
|
||||
it "parses request body" do
|
||||
route = Route.new "POST", "/" do |env|
|
||||
name = env.params.query["name"]
|
||||
|
|
|
@ -5,7 +5,7 @@ HTTP_METHODS = %w(get post put patch delete options)
|
|||
|
||||
{% for method in HTTP_METHODS %}
|
||||
def {{method.id}}(path, &block : HTTP::Server::Context -> _)
|
||||
Kemal::RouteHandler::INSTANCE.add_route({{method}}.upcase, path, &block)
|
||||
Kemal::RouteHandler::INSTANCE.add_route({{method}}.upcase, path, &block)
|
||||
end
|
||||
{% end %}
|
||||
|
||||
|
|
|
@ -21,6 +21,10 @@ module Kemal
|
|||
@json_parsed = false
|
||||
end
|
||||
|
||||
private def decode_url_param(value : String)
|
||||
value.size == 0 ? value : HTTP::Params.parse(value).first[0]?
|
||||
end
|
||||
|
||||
{% for method in %w(url query body json) %}
|
||||
def {{method.id}}
|
||||
# check memoization
|
||||
|
@ -45,7 +49,7 @@ module Kemal
|
|||
def parse_url
|
||||
if params = @request.url_params
|
||||
params.each do |key, value|
|
||||
@url[key.as(String)] = value.as(String)
|
||||
@url[key.as(String)] = decode_url_param(value).as(String)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue