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-03-21 16:29:48 +00:00
|
|
|
get "/" do
|
2014-07-30 22:58:48 +00:00
|
|
|
"hello"
|
|
|
|
end
|
|
|
|
request = HTTP::Request.new("GET", "/")
|
2016-03-21 16:29:48 +00:00
|
|
|
client_response = call_request_on_app(request)
|
2016-01-24 10:22:25 +00:00
|
|
|
client_response.body.should eq("hello")
|
2014-07-30 22:58:48 +00:00
|
|
|
end
|
2014-07-30 23:50:14 +00:00
|
|
|
|
2020-07-08 11:41:16 +00:00
|
|
|
it "routes with long response body" do
|
|
|
|
long_response_body = "string" * 10_000
|
|
|
|
|
|
|
|
get "/" do
|
|
|
|
long_response_body
|
|
|
|
end
|
|
|
|
request = HTTP::Request.new("GET", "/")
|
|
|
|
client_response = call_request_on_app(request)
|
|
|
|
client_response.body.should eq(long_response_body)
|
|
|
|
end
|
|
|
|
|
2017-10-02 20:47:43 +00:00
|
|
|
it "routes should only return strings" do
|
|
|
|
get "/" do
|
|
|
|
100
|
|
|
|
end
|
|
|
|
request = HTTP::Request.new("GET", "/")
|
|
|
|
client_response = call_request_on_app(request)
|
|
|
|
client_response.body.should eq("")
|
|
|
|
end
|
|
|
|
|
2015-05-29 21:21:15 +00:00
|
|
|
it "routes request with query string" do
|
2016-03-21 16:29:48 +00:00
|
|
|
get "/" do |env|
|
2016-03-06 11:22:24 +00:00
|
|
|
"hello #{env.params.query["message"]}"
|
2015-05-29 21:21:15 +00:00
|
|
|
end
|
|
|
|
request = HTTP::Request.new("GET", "/?message=world")
|
2016-03-21 16:29:48 +00:00
|
|
|
client_response = call_request_on_app(request)
|
2016-01-24 10:22:25 +00:00
|
|
|
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-04-12 11:41:09 +00:00
|
|
|
get "/" do |env|
|
2016-03-06 11:22:24 +00:00
|
|
|
"hello #{env.params.query["message"]} time #{env.params.query["time"]}"
|
2015-10-26 18:49:28 +00:00
|
|
|
end
|
|
|
|
request = HTTP::Request.new("GET", "/?message=world&time=now")
|
2016-03-21 16:29:48 +00:00
|
|
|
client_response = call_request_on_app(request)
|
2016-01-24 10:22:25 +00:00
|
|
|
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-03-21 16:29:48 +00:00
|
|
|
get "/:message" do |env|
|
2016-03-06 11:22:24 +00:00
|
|
|
"hello #{env.params.url["message"]}"
|
2015-05-29 21:24:31 +00:00
|
|
|
end
|
|
|
|
request = HTTP::Request.new("GET", "/world?message=coco")
|
2016-03-21 16:29:48 +00:00
|
|
|
client_response = call_request_on_app(request)
|
2016-01-24 10:22:25 +00:00
|
|
|
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-03-21 16:29:48 +00:00
|
|
|
post "/" do |env|
|
2016-03-06 11:22:24 +00:00
|
|
|
name = env.params.json["name"]
|
|
|
|
age = env.params.json["age"]
|
2015-11-06 18:24:38 +00:00
|
|
|
"Hello #{name} Age #{age}"
|
|
|
|
end
|
|
|
|
|
|
|
|
json_payload = {"name": "Serdar", "age": 26}
|
|
|
|
request = HTTP::Request.new(
|
|
|
|
"POST",
|
|
|
|
"/",
|
|
|
|
body: json_payload.to_json,
|
2016-06-14 21:18:00 +00:00
|
|
|
headers: HTTP::Headers{"Content-Type" => "application/json"},
|
2015-11-06 18:24:38 +00:00
|
|
|
)
|
2016-03-21 16:29:48 +00:00
|
|
|
client_response = call_request_on_app(request)
|
2016-01-24 10:22:25 +00:00
|
|
|
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-03-21 16:29:48 +00:00
|
|
|
post "/" do |env|
|
2016-06-14 21:18:00 +00:00
|
|
|
skills = env.params.json["skills"].as(Array)
|
2015-11-06 18:24:38 +00:00
|
|
|
"Skills #{skills.each.join(',')}"
|
|
|
|
end
|
|
|
|
|
|
|
|
json_payload = {"skills": ["ruby", "crystal"]}
|
|
|
|
request = HTTP::Request.new(
|
|
|
|
"POST",
|
|
|
|
"/",
|
|
|
|
body: json_payload.to_json,
|
2016-06-14 21:18:00 +00:00
|
|
|
headers: HTTP::Headers{"Content-Type" => "application/json"},
|
2015-11-06 18:24:38 +00:00
|
|
|
)
|
2016-03-21 16:29:48 +00:00
|
|
|
client_response = call_request_on_app(request)
|
2016-01-24 10:22:25 +00:00
|
|
|
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-03-21 16:29:48 +00:00
|
|
|
post "/" do |env|
|
2016-06-14 21:18:00 +00:00
|
|
|
skills = env.params.json["skills"].as(Array)
|
2015-11-06 18:24:38 +00:00
|
|
|
skills_from_languages = skills.map do |skill|
|
|
|
|
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,
|
2016-06-14 21:18:00 +00:00
|
|
|
headers: HTTP::Headers{"Content-Type" => "application/json"},
|
2015-11-06 18:24:38 +00:00
|
|
|
)
|
|
|
|
|
2016-03-21 16:29:48 +00:00
|
|
|
client_response = call_request_on_app(request)
|
2016-01-24 10:22:25 +00:00
|
|
|
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
|
|
|
|
2015-12-03 19:21:22 +00:00
|
|
|
it "can process HTTP HEAD requests for defined GET routes" do
|
2018-05-17 08:07:40 +00:00
|
|
|
get "/" do
|
2015-12-03 19:21:22 +00:00
|
|
|
"Hello World from GET"
|
|
|
|
end
|
|
|
|
request = HTTP::Request.new("HEAD", "/")
|
2016-03-21 16:29:48 +00:00
|
|
|
client_response = call_request_on_app(request)
|
2016-01-24 10:22:25 +00:00
|
|
|
client_response.status_code.should eq(200)
|
2015-12-03 19:21:22 +00:00
|
|
|
end
|
|
|
|
|
2016-01-24 10:52:41 +00:00
|
|
|
it "redirects user to provided url" do
|
2016-03-21 16:29:48 +00:00
|
|
|
get "/" do |env|
|
2016-01-24 10:52:41 +00:00
|
|
|
env.redirect "/login"
|
|
|
|
end
|
|
|
|
request = HTTP::Request.new("GET", "/")
|
2016-03-21 16:29:48 +00:00
|
|
|
client_response = call_request_on_app(request)
|
2016-02-09 15:25:01 +00:00
|
|
|
client_response.status_code.should eq(302)
|
2020-04-04 18:25:35 +00:00
|
|
|
client_response.body.should eq("")
|
|
|
|
client_response.headers.has_key?("Location").should eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "redirects with body" do
|
|
|
|
get "/" do |env|
|
|
|
|
env.redirect "/login", body: "Redirecting to /login"
|
|
|
|
end
|
|
|
|
request = HTTP::Request.new("GET", "/")
|
|
|
|
client_response = call_request_on_app(request)
|
|
|
|
client_response.status_code.should eq(302)
|
|
|
|
client_response.body.should eq("Redirecting to /login")
|
2016-01-24 10:52:41 +00:00
|
|
|
client_response.headers.has_key?("Location").should eq(true)
|
|
|
|
end
|
2022-06-29 10:25:40 +00:00
|
|
|
|
|
|
|
it "redirects and closes response in before filter" do
|
|
|
|
filter_handler = Kemal::FilterHandler.new
|
|
|
|
filter_handler._add_route_filter("GET", "/", :before) do |env|
|
|
|
|
env.redirect "/login"
|
|
|
|
end
|
|
|
|
Kemal.config.add_filter_handler(filter_handler)
|
|
|
|
|
2022-06-29 10:29:50 +00:00
|
|
|
get "/" do
|
2022-06-29 10:25:40 +00:00
|
|
|
"home page"
|
|
|
|
end
|
|
|
|
|
|
|
|
request = HTTP::Request.new("GET", "/")
|
|
|
|
client_response = call_request_on_app(request)
|
|
|
|
client_response.status_code.should eq(302)
|
|
|
|
client_response.body.should eq("")
|
|
|
|
client_response.headers.has_key?("Location").should eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "redirects in before filter without closing response" do
|
|
|
|
filter_handler = Kemal::FilterHandler.new
|
|
|
|
filter_handler._add_route_filter("GET", "/", :before) do |env|
|
|
|
|
env.redirect "/login", close: false
|
|
|
|
end
|
|
|
|
Kemal.config.add_filter_handler(filter_handler)
|
|
|
|
|
2022-06-29 10:29:50 +00:00
|
|
|
get "/" do
|
2022-06-29 10:25:40 +00:00
|
|
|
"home page"
|
|
|
|
end
|
|
|
|
|
|
|
|
request = HTTP::Request.new("GET", "/")
|
|
|
|
client_response = call_request_on_app(request)
|
|
|
|
client_response.status_code.should eq(302)
|
|
|
|
client_response.body.should eq("home page")
|
|
|
|
client_response.headers.has_key?("Location").should eq(true)
|
|
|
|
end
|
2014-07-30 22:58:48 +00:00
|
|
|
end
|