kemal/spec/kemal_handler_spec.cr

113 lines
3.1 KiB
Crystal
Raw Normal View History

require "./spec_helper"
2015-10-23 18:33:26 +00:00
describe "Kemal::Handler" do
it "routes" do
2015-10-23 18:33:26 +00:00
kemal = Kemal::Handler.new
kemal.add_route "GET", "/" do
"hello"
end
request = HTTP::Request.new("GET", "/")
2015-10-23 18:33:26 +00:00
response = kemal.call(request)
response.body.should eq("hello")
end
2015-05-29 21:21:15 +00:00
it "routes request with query string" do
2015-10-23 18:33:26 +00:00
kemal = Kemal::Handler.new
2015-10-29 09:49:58 +00:00
kemal.add_route "GET", "/" do |env|
"hello #{env.params["message"]}"
2015-05-29 21:21:15 +00:00
end
request = HTTP::Request.new("GET", "/?message=world")
2015-10-23 18:33:26 +00:00
response = kemal.call(request)
2015-05-29 21:21:15 +00:00
response.body.should eq("hello world")
end
2015-10-26 18:49:28 +00:00
it "routes request with multiple query strings" do
kemal = Kemal::Handler.new
2015-10-29 09:49:58 +00:00
kemal.add_route "GET", "/" do |env|
"hello #{env.params["message"]} time #{env.params["time"]}"
2015-10-26 18:49:28 +00:00
end
request = HTTP::Request.new("GET", "/?message=world&time=now")
response = kemal.call(request)
response.body.should eq("hello world time now")
end
it "route parameter has more precedence than query string arguments" do
2015-10-23 18:33:26 +00:00
kemal = Kemal::Handler.new
2015-10-29 09:49:58 +00:00
kemal.add_route "GET", "/:message" do |env|
"hello #{env.params["message"]}"
end
request = HTTP::Request.new("GET", "/world?message=coco")
2015-10-23 18:33:26 +00:00
response = kemal.call(request)
response.body.should eq("hello world")
end
2015-11-06 18:24:38 +00:00
it "parses simple JSON body" do
kemal = Kemal::Handler.new
kemal.add_route "POST", "/" do |env|
name = env.params["name"]
age = env.params["age"]
"Hello #{name} Age #{age}"
end
json_payload = {"name": "Serdar", "age": 26}
request = HTTP::Request.new(
"POST",
"/",
body: json_payload.to_json,
headers: HTTP::Headers{"Content-Type": "application/json"},
)
response = kemal.call(request)
response.body.should eq("Hello Serdar Age 26")
end
it "parses JSON with string array" do
kemal = Kemal::Handler.new
kemal.add_route "POST", "/" do |env|
skills = env.params["skills"] as Array
"Skills #{skills.each.join(',')}"
end
json_payload = {"skills": ["ruby", "crystal"]}
request = HTTP::Request.new(
"POST",
"/",
body: json_payload.to_json,
headers: HTTP::Headers{"Content-Type": "application/json"},
)
response = kemal.call(request)
response.body.should eq("Skills ruby,crystal")
end
it "parses JSON with json object array" do
kemal = Kemal::Handler.new
kemal.add_route "POST", "/" do |env|
skills = env.params["skills"] as Array
skills_from_languages = skills.map do |skill|
skill = skill as Hash
skill["language"]
end
"Skills #{skills_from_languages.each.join(',')}"
end
json_payload = {"skills": [{"language": "ruby"}, {"language": "crystal"}]}
request = HTTP::Request.new(
"POST",
"/",
body: json_payload.to_json,
headers: HTTP::Headers{"Content-Type": "application/json"},
)
response = kemal.call(request)
response.body.should eq("Skills ruby,crystal")
end
2015-11-27 20:45:13 +00:00
it "renders 404 on not found" do
kemal = Kemal::Handler.new
request = HTTP::Request.new("GET", "/?message=world")
response = kemal.call(request)
2015-11-28 10:39:58 +00:00
response.status_code.should eq 404
2015-11-27 20:45:13 +00:00
end
end