2015-01-31 14:39:50 +00:00
|
|
|
require "./spec_helper"
|
2014-07-30 22:58:48 +00:00
|
|
|
|
2016-02-03 20:08:54 +00:00
|
|
|
describe "Kemal::RouteHandler" do
|
2014-07-30 22:58:48 +00:00
|
|
|
it "routes" do
|
2016-02-03 20:08:54 +00:00
|
|
|
kemal = Kemal::RouteHandler.new
|
2015-10-23 18:33:26 +00:00
|
|
|
kemal.add_route "GET", "/" do
|
2014-07-30 22:58:48 +00:00
|
|
|
"hello"
|
|
|
|
end
|
|
|
|
request = HTTP::Request.new("GET", "/")
|
2016-01-24 10:22:25 +00:00
|
|
|
io_with_context = create_request_and_return_io(kemal, request)
|
|
|
|
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
|
|
|
|
client_response.body.should eq("hello")
|
2014-07-30 22:58:48 +00:00
|
|
|
end
|
2014-07-30 23:50:14 +00:00
|
|
|
|
2015-05-29 21:21:15 +00:00
|
|
|
it "routes request with query string" do
|
2016-02-03 20:08:54 +00:00
|
|
|
kemal = Kemal::RouteHandler.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")
|
2016-01-24 10:22:25 +00:00
|
|
|
io_with_context = create_request_and_return_io(kemal, request)
|
|
|
|
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
|
|
|
|
client_response.body.should eq("hello world")
|
2015-05-29 21:21:15 +00:00
|
|
|
end
|
|
|
|
|
2015-10-26 18:49:28 +00:00
|
|
|
it "routes request with multiple query strings" do
|
2016-02-03 20:08:54 +00:00
|
|
|
kemal = Kemal::RouteHandler.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")
|
2016-01-24 10:22:25 +00:00
|
|
|
io_with_context = create_request_and_return_io(kemal, request)
|
|
|
|
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
|
|
|
|
client_response.body.should eq("hello world time now")
|
2015-10-26 18:49:28 +00:00
|
|
|
end
|
|
|
|
|
2015-05-29 21:24:31 +00:00
|
|
|
it "route parameter has more precedence than query string arguments" do
|
2016-02-03 20:08:54 +00:00
|
|
|
kemal = Kemal::RouteHandler.new
|
2015-10-29 09:49:58 +00:00
|
|
|
kemal.add_route "GET", "/:message" do |env|
|
|
|
|
"hello #{env.params["message"]}"
|
2015-05-29 21:24:31 +00:00
|
|
|
end
|
|
|
|
request = HTTP::Request.new("GET", "/world?message=coco")
|
2016-01-24 10:22:25 +00:00
|
|
|
io_with_context = create_request_and_return_io(kemal, request)
|
|
|
|
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
|
|
|
|
client_response.body.should eq("hello world")
|
2015-05-29 21:24:31 +00:00
|
|
|
end
|
2015-11-06 18:24:38 +00:00
|
|
|
|
|
|
|
it "parses simple JSON body" do
|
2016-02-03 20:08:54 +00:00
|
|
|
kemal = Kemal::RouteHandler.new
|
2015-11-06 18:24:38 +00:00
|
|
|
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"},
|
|
|
|
)
|
2016-01-24 10:22:25 +00:00
|
|
|
io_with_context = create_request_and_return_io(kemal, request)
|
|
|
|
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
|
|
|
|
client_response.body.should eq("Hello Serdar Age 26")
|
2015-11-06 18:24:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "parses JSON with string array" do
|
2016-02-03 20:08:54 +00:00
|
|
|
kemal = Kemal::RouteHandler.new
|
2015-11-06 18:24:38 +00:00
|
|
|
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"},
|
|
|
|
)
|
2016-01-24 10:22:25 +00:00
|
|
|
io_with_context = create_request_and_return_io(kemal, request)
|
|
|
|
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
|
|
|
|
client_response.body.should eq("Skills ruby,crystal")
|
2015-11-06 18:24:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "parses JSON with json object array" do
|
2016-02-03 20:08:54 +00:00
|
|
|
kemal = Kemal::RouteHandler.new
|
2015-11-06 18:24:38 +00:00
|
|
|
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"},
|
|
|
|
)
|
|
|
|
|
2016-01-24 10:22:25 +00:00
|
|
|
io_with_context = create_request_and_return_io(kemal, request)
|
|
|
|
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
|
|
|
|
client_response.body.should eq("Skills ruby,crystal")
|
2015-11-06 18:24:38 +00:00
|
|
|
end
|
2015-11-27 20:45:13 +00:00
|
|
|
|
2016-02-14 10:43:25 +00:00
|
|
|
# Removed until there is a way to test multiple middlewares
|
2016-02-14 13:15:28 +00:00
|
|
|
# it "renders 404 on not found" do
|
2016-02-14 10:43:25 +00:00
|
|
|
# kemal = Kemal::RouteHandler.new
|
|
|
|
# request = HTTP::Request.new("GET", "/?message=world")
|
|
|
|
# io_with_context = create_request_and_return_io(kemal, request)
|
|
|
|
# client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
|
|
|
|
# client_response.status_code.should eq 404
|
2016-02-14 13:15:28 +00:00
|
|
|
# end
|
2015-12-02 19:57:23 +00:00
|
|
|
|
2016-01-24 10:22:25 +00:00
|
|
|
# it "renders 500 on exception" do
|
2016-02-03 20:08:54 +00:00
|
|
|
# kemal = Kemal::RouteHandler.new
|
2016-01-24 10:22:25 +00:00
|
|
|
# kemal.add_route "GET", "/" do
|
|
|
|
# raise "Exception"
|
|
|
|
# end
|
|
|
|
# request = HTTP::Request.new("GET", "/?message=world")
|
|
|
|
# io_with_context = create_request_and_return_io(kemal, request)
|
|
|
|
# client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
|
|
|
|
# client_response.status_code.should eq 500
|
|
|
|
# client_response.body.includes?("Exception").should eq true
|
|
|
|
# end
|
|
|
|
#
|
2015-12-02 19:57:23 +00:00
|
|
|
it "checks for _method param in POST request to simulate PUT" do
|
2016-02-03 20:08:54 +00:00
|
|
|
kemal = Kemal::RouteHandler.new
|
2015-12-06 15:55:21 +00:00
|
|
|
kemal.add_route "PUT", "/" do |env|
|
2015-12-02 19:57:23 +00:00
|
|
|
"Hello World from PUT"
|
|
|
|
end
|
|
|
|
request = HTTP::Request.new(
|
|
|
|
"POST",
|
|
|
|
"/",
|
|
|
|
body: "_method=PUT",
|
|
|
|
headers: HTTP::Headers{"Content-Type": "application/x-www-form-urlencoded"}
|
|
|
|
)
|
2016-01-24 10:22:25 +00:00
|
|
|
io_with_context = create_request_and_return_io(kemal, request)
|
|
|
|
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
|
|
|
|
client_response.body.should eq("Hello World from PUT")
|
2015-12-02 19:57:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "checks for _method param in POST request to simulate PATCH" do
|
2016-02-03 20:08:54 +00:00
|
|
|
kemal = Kemal::RouteHandler.new
|
2015-12-06 15:55:21 +00:00
|
|
|
kemal.add_route "PATCH", "/" do |env|
|
2015-12-02 19:57:23 +00:00
|
|
|
"Hello World from PATCH"
|
|
|
|
end
|
|
|
|
request = HTTP::Request.new(
|
|
|
|
"POST",
|
|
|
|
"/",
|
|
|
|
body: "_method=PATCH",
|
|
|
|
headers: HTTP::Headers{"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"}
|
|
|
|
)
|
2016-01-24 10:22:25 +00:00
|
|
|
io_with_context = create_request_and_return_io(kemal, request)
|
|
|
|
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
|
|
|
|
client_response.body.should eq("Hello World from PATCH")
|
2015-12-02 19:57:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "checks for _method param in POST request to simulate DELETE" do
|
2016-02-03 20:08:54 +00:00
|
|
|
kemal = Kemal::RouteHandler.new
|
2015-12-06 15:55:21 +00:00
|
|
|
kemal.add_route "DELETE", "/" do |env|
|
2015-12-02 19:57:23 +00:00
|
|
|
"Hello World from DELETE"
|
|
|
|
end
|
|
|
|
json_payload = {"_method": "DELETE"}
|
|
|
|
request = HTTP::Request.new(
|
|
|
|
"POST",
|
|
|
|
"/",
|
|
|
|
body: json_payload.to_json,
|
|
|
|
headers: HTTP::Headers{"Content-Type": "application/json"}
|
|
|
|
)
|
2016-01-24 10:22:25 +00:00
|
|
|
io_with_context = create_request_and_return_io(kemal, request)
|
|
|
|
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
|
|
|
|
client_response.body.should eq("Hello World from DELETE")
|
2015-12-02 19:57:23 +00:00
|
|
|
end
|
|
|
|
|
2015-12-03 19:21:22 +00:00
|
|
|
it "can process HTTP HEAD requests for defined GET routes" do
|
2016-02-03 20:08:54 +00:00
|
|
|
kemal = Kemal::RouteHandler.new
|
2015-12-03 19:21:22 +00:00
|
|
|
kemal.add_route "GET", "/" do |env|
|
|
|
|
"Hello World from GET"
|
|
|
|
end
|
|
|
|
request = HTTP::Request.new("HEAD", "/")
|
2016-01-24 10:22:25 +00:00
|
|
|
io_with_context = create_request_and_return_io(kemal, request)
|
|
|
|
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
|
|
|
|
client_response.status_code.should eq(200)
|
2015-12-03 19:21:22 +00:00
|
|
|
end
|
|
|
|
|
2016-02-14 10:43:25 +00:00
|
|
|
# Removed until there is a way to test multiple middlewares
|
2016-02-14 13:15:28 +00:00
|
|
|
# it "can't process HTTP HEAD requests for undefined GET routes" do
|
2016-02-14 10:43:25 +00:00
|
|
|
# kemal = Kemal::RouteHandler.new
|
|
|
|
# request = HTTP::Request.new("HEAD", "/")
|
|
|
|
# io_with_context = create_request_and_return_io(kemal, request)
|
|
|
|
# client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
|
|
|
|
# client_response.status_code.should eq(404)
|
2016-02-14 13:15:28 +00:00
|
|
|
# end
|
2015-12-03 19:21:22 +00:00
|
|
|
|
2016-01-24 10:52:41 +00:00
|
|
|
it "redirects user to provided url" do
|
2016-02-03 20:08:54 +00:00
|
|
|
kemal = Kemal::RouteHandler.new
|
2016-01-24 10:52:41 +00:00
|
|
|
kemal.add_route "GET", "/" do |env|
|
|
|
|
env.redirect "/login"
|
|
|
|
end
|
|
|
|
request = HTTP::Request.new("GET", "/")
|
|
|
|
io_with_context = create_request_and_return_io(kemal, request)
|
|
|
|
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
|
2016-02-09 15:25:01 +00:00
|
|
|
client_response.status_code.should eq(302)
|
2016-01-24 10:52:41 +00:00
|
|
|
client_response.headers.has_key?("Location").should eq(true)
|
|
|
|
end
|
2014-07-30 22:58:48 +00:00
|
|
|
end
|