kemal/spec/kemal_handler_spec.cr

57 lines
1.7 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
kemal.add_route "GET", "/" do |ctx|
2015-05-29 21:21:15 +00:00
"hello #{ctx.params["message"]}"
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
it "route parameter has more precedence than query string arguments" do
2015-10-23 18:33:26 +00:00
kemal = Kemal::Handler.new
kemal.add_route "GET", "/:message" do |ctx|
"hello #{ctx.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
it "sets content type" do
2015-10-23 18:33:26 +00:00
kemal = Kemal::Handler.new
kemal.add_route "GET", "/" do |env|
env.response.content_type = "application/json"
end
request = HTTP::Request.new("GET", "/")
2015-10-23 18:33:26 +00:00
response = kemal.call(request)
response.headers["Content-Type"].should eq("application/json")
end
2015-10-26 18:22:31 +00:00
it "parses POST body" do
kemal = Kemal::Handler.new
kemal.add_route "POST", "/" do |env|
name = env.request.params["name"]
age = env.request.params["age"]
hasan = env.request.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