Added param_parser specs
This commit is contained in:
parent
3cc50e0a7e
commit
ad6baba12b
3 changed files with 19 additions and 8 deletions
|
@ -1,16 +1,27 @@
|
||||||
require "./spec_helper"
|
require "./spec_helper"
|
||||||
|
|
||||||
describe "ParamParser" do
|
describe "ParamParser" do
|
||||||
it "parses params" do
|
|
||||||
kemal = Kemal::Handler.new
|
it "parses query params" do
|
||||||
kemal.add_route "POST", "/" do |env|
|
route = Route.new "POST", "/" do |env|
|
||||||
|
hasan = env.params["hasan"]
|
||||||
|
"Hello #{hasan}"
|
||||||
|
end
|
||||||
|
request = HTTP::Request.new("POST", "/?hasan=cemal")
|
||||||
|
params = Kemal::ParamParser.new(route, request).parse
|
||||||
|
params["hasan"].should eq "cemal"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "parses request body" do
|
||||||
|
route = Route.new "POST", "/" do |env|
|
||||||
name = env.params["name"]
|
name = env.params["name"]
|
||||||
age = env.params["age"]
|
age = env.params["age"]
|
||||||
hasan = env.params["hasan"]
|
hasan = env.params["hasan"]
|
||||||
"Hello #{name} #{hasan} #{age}"
|
"Hello #{name} #{hasan} #{age}"
|
||||||
end
|
end
|
||||||
request = HTTP::Request.new("POST", "/?hasan=cemal", body: "name=kemal&age=99")
|
request = HTTP::Request.new("POST", "/?hasan=cemal", body: "name=serdar&age=99")
|
||||||
response = kemal.call(request)
|
params = Kemal::ParamParser.new(route, request).parse
|
||||||
response.body.should eq("Hello kemal cemal 99")
|
params.should eq({"hasan" => "cemal", "name" => "serdar", "age" => "99"})
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -22,7 +22,7 @@ class Kemal::Handler < HTTP::Handler
|
||||||
@routes.each do |route|
|
@routes.each do |route|
|
||||||
match = route.match?(request)
|
match = route.match?(request)
|
||||||
if match
|
if match
|
||||||
params = Kemal::Params.new(route, request).parse
|
params = Kemal::ParamParser.new(route, request).parse
|
||||||
context = Context.new(request, params.not_nil!)
|
context = Context.new(request, params.not_nil!)
|
||||||
begin
|
begin
|
||||||
body = route.handler.call(context).to_s
|
body = route.handler.call(context).to_s
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
class Kemal::Params
|
class Kemal::ParamParser
|
||||||
def initialize(@route, @request)
|
def initialize(@route, @request)
|
||||||
@route_components = route.components
|
@route_components = route.components
|
||||||
@request_components = request.path.not_nil!.split "/"
|
@request_components = request.path.not_nil!.split "/"
|
||||||
|
|
Loading…
Reference in a new issue