Added param parser

This commit is contained in:
Sdogruyol 2015-10-28 20:30:27 +02:00
parent a8ad95aa67
commit 3cc50e0a7e
8 changed files with 72 additions and 52 deletions

View file

@ -50,17 +50,4 @@ describe "Kemal::Handler" do
response = kemal.call(request)
response.headers["Content-Type"].should eq("application/json")
end
it "parses POST body" do
kemal = Kemal::Handler.new
kemal.add_route "POST", "/" do |env|
name = env.params["name"]
age = env.params["age"]
hasan = env.params["hasan"]
"Hello #{name} #{hasan} #{age}"
end
request = HTTP::Request.new("POST", "/?hasan=cemal", body: "name=kemal&age=99")
response = kemal.call(request)
response.body.should eq("Hello kemal cemal 99")
end
end

View file

@ -1,13 +1,11 @@
require "./spec_helper"
describe "Logger" do
it "logs stuff" do
IO.pipe do |r,w|
IO.pipe do |r, w|
logger = Kemal::Logger.new(w)
logger.info "Info from logger"
r.gets.should match(/Info from logger/)
end
end
end

16
spec/param_parser_spec.cr Normal file
View file

@ -0,0 +1,16 @@
require "./spec_helper"
describe "ParamParser" do
it "parses params" do
kemal = Kemal::Handler.new
kemal.add_route "POST", "/" do |env|
name = env.params["name"]
age = env.params["age"]
hasan = env.params["hasan"]
"Hello #{name} #{hasan} #{age}"
end
request = HTTP::Request.new("POST", "/?hasan=cemal", body: "name=kemal&age=99")
response = kemal.call(request)
response.body.should eq("Hello kemal cemal 99")
end
end

View file

@ -4,18 +4,21 @@ describe "Route" do
describe "match" do
it "doesn't match because of route" do
route = Route.new("GET", "/foo/bar") { "" }
route.match("GET", "/foo/baz".split("/")).should be_nil
request = HTTP::Request.new("GET", "/world?message=coco")
route.match?(request).should be_nil
end
it "doesn't match because of method" do
route = Route.new("GET", "/foo/bar") { "" }
route.match("POST", "/foo/bar".split("/")).should be_nil
request = HTTP::Request.new("POST", "/foo/bar")
route.match?(request).should be_nil
end
it "matches" do
route = Route.new("GET", "/foo/:one/path/:two") { "" }
params = route.match("GET", "/foo/uno/path/dos".split("/"))
params.should eq({"one" => "uno", "two" => "dos"})
request = HTTP::Request.new("GET", "/foo/uno/path/dos")
match = route.match?(request)
match.should eq true
end
end
end