2016-01-03 11:22:43 +00:00
|
|
|
require "./spec_helper"
|
2018-12-23 11:24:27 +00:00
|
|
|
require "./handler_spec"
|
2016-01-03 11:22:43 +00:00
|
|
|
|
|
|
|
describe "Macros" do
|
|
|
|
describe "#public_folder" do
|
|
|
|
it "sets public folder" do
|
|
|
|
public_folder "/some/path/to/folder"
|
|
|
|
Kemal.config.public_folder.should eq("/some/path/to/folder")
|
|
|
|
end
|
|
|
|
end
|
2016-01-05 12:20:40 +00:00
|
|
|
|
|
|
|
describe "#add_handler" do
|
|
|
|
it "adds a custom handler" do
|
|
|
|
add_handler CustomTestHandler.new
|
2017-07-11 08:22:35 +00:00
|
|
|
Kemal.config.setup
|
2017-09-10 11:41:07 +00:00
|
|
|
Kemal.config.handlers.size.should eq 7
|
2016-01-05 12:20:40 +00:00
|
|
|
end
|
|
|
|
end
|
2016-01-17 11:08:12 +00:00
|
|
|
|
|
|
|
describe "#logging" do
|
|
|
|
it "sets logging status" do
|
|
|
|
logging false
|
|
|
|
Kemal.config.logging.should eq false
|
|
|
|
end
|
2016-12-03 14:37:58 +00:00
|
|
|
|
2016-02-12 14:09:15 +00:00
|
|
|
it "sets a custom logger" do
|
|
|
|
config = Kemal::Config::INSTANCE
|
2016-07-05 19:14:00 +00:00
|
|
|
logger CustomLogHandler.new
|
2016-02-12 14:09:15 +00:00
|
|
|
config.logger.should be_a(CustomLogHandler)
|
|
|
|
end
|
2016-01-17 11:08:12 +00:00
|
|
|
end
|
2016-03-29 21:48:58 +00:00
|
|
|
|
2016-11-01 08:46:13 +00:00
|
|
|
describe "#halt" do
|
|
|
|
it "can break block with halt macro" do
|
2018-05-17 08:07:40 +00:00
|
|
|
get "/non-breaking" do
|
2016-03-29 21:48:58 +00:00
|
|
|
"hello"
|
|
|
|
"world"
|
|
|
|
end
|
|
|
|
request = HTTP::Request.new("GET", "/non-breaking")
|
|
|
|
client_response = call_request_on_app(request)
|
|
|
|
client_response.status_code.should eq(200)
|
|
|
|
client_response.body.should eq("world")
|
|
|
|
|
|
|
|
get "/breaking" do |env|
|
2016-11-01 08:46:13 +00:00
|
|
|
halt env, 404, "hello"
|
2016-03-29 21:48:58 +00:00
|
|
|
"world"
|
|
|
|
end
|
|
|
|
request = HTTP::Request.new("GET", "/breaking")
|
|
|
|
client_response = call_request_on_app(request)
|
|
|
|
client_response.status_code.should eq(404)
|
|
|
|
client_response.body.should eq("hello")
|
|
|
|
end
|
|
|
|
|
2016-11-01 08:46:13 +00:00
|
|
|
it "can break block with halt macro using default values" do
|
2016-03-29 21:48:58 +00:00
|
|
|
get "/" do |env|
|
2016-11-01 08:46:13 +00:00
|
|
|
halt env
|
2016-03-29 21:48:58 +00:00
|
|
|
"world"
|
|
|
|
end
|
|
|
|
request = HTTP::Request.new("GET", "/")
|
|
|
|
client_response = call_request_on_app(request)
|
|
|
|
client_response.status_code.should eq(200)
|
|
|
|
client_response.body.should eq("")
|
|
|
|
end
|
|
|
|
end
|
2016-06-16 14:05:28 +00:00
|
|
|
|
2019-08-05 15:34:26 +00:00
|
|
|
describe "#callbacks" do
|
|
|
|
it "can break block with halt macro from before_* callback" do
|
|
|
|
filter_middleware = Kemal::FilterHandler.new
|
|
|
|
filter_middleware._add_route_filter("GET", "/", :before) do |env|
|
|
|
|
halt env, status_code: 400, response: "Missing origin."
|
|
|
|
end
|
|
|
|
|
2019-08-30 11:32:23 +00:00
|
|
|
get "/" do |_env|
|
2019-08-05 15:34:26 +00:00
|
|
|
"Hello world"
|
|
|
|
end
|
|
|
|
|
|
|
|
request = HTTP::Request.new("GET", "/")
|
|
|
|
client_response = call_request_on_app(request)
|
|
|
|
client_response.status_code.should eq(400)
|
|
|
|
client_response.body.should eq("Missing origin.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-16 14:05:28 +00:00
|
|
|
describe "#headers" do
|
|
|
|
it "can add headers" do
|
|
|
|
get "/headers" do |env|
|
|
|
|
env.response.headers.add "Content-Type", "image/png"
|
|
|
|
headers env, {
|
|
|
|
"Access-Control-Allow-Origin" => "*",
|
2016-07-17 11:31:45 +00:00
|
|
|
"Content-Type" => "text/plain",
|
2016-06-16 14:05:28 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
request = HTTP::Request.new("GET", "/headers")
|
|
|
|
response = call_request_on_app(request)
|
|
|
|
response.headers["Access-Control-Allow-Origin"].should eq("*")
|
|
|
|
response.headers["Content-Type"].should eq("text/plain")
|
|
|
|
end
|
|
|
|
end
|
2016-07-19 17:58:04 +00:00
|
|
|
|
|
|
|
describe "#send_file" do
|
|
|
|
it "sends file with given path and default mime-type" do
|
|
|
|
get "/" do |env|
|
|
|
|
send_file env, "./spec/asset/hello.ecr"
|
|
|
|
end
|
|
|
|
|
|
|
|
request = HTTP::Request.new("GET", "/")
|
|
|
|
response = call_request_on_app(request)
|
|
|
|
response.status_code.should eq(200)
|
|
|
|
response.headers["Content-Type"].should eq("application/octet-stream")
|
2017-03-29 12:09:25 +00:00
|
|
|
response.headers["Content-Length"].should eq("18")
|
2016-07-19 17:58:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "sends file with given path and given mime-type" do
|
|
|
|
get "/" do |env|
|
|
|
|
send_file env, "./spec/asset/hello.ecr", "image/jpeg"
|
|
|
|
end
|
|
|
|
|
|
|
|
request = HTTP::Request.new("GET", "/")
|
|
|
|
response = call_request_on_app(request)
|
|
|
|
response.status_code.should eq(200)
|
|
|
|
response.headers["Content-Type"].should eq("image/jpeg")
|
2017-03-29 12:09:25 +00:00
|
|
|
response.headers["Content-Length"].should eq("18")
|
2016-07-19 17:58:04 +00:00
|
|
|
end
|
2016-08-08 18:49:47 +00:00
|
|
|
|
|
|
|
it "sends file with binary stream" do
|
|
|
|
get "/" do |env|
|
|
|
|
send_file env, "Serdar".to_slice
|
|
|
|
end
|
|
|
|
|
|
|
|
request = HTTP::Request.new("GET", "/")
|
|
|
|
response = call_request_on_app(request)
|
|
|
|
response.status_code.should eq(200)
|
|
|
|
response.headers["Content-Type"].should eq("application/octet-stream")
|
|
|
|
response.headers["Content-Length"].should eq("6")
|
|
|
|
end
|
2018-12-23 11:24:27 +00:00
|
|
|
|
|
|
|
it "sends file with given path and given filename" do
|
|
|
|
get "/" do |env|
|
|
|
|
send_file env, "./spec/asset/hello.ecr", filename: "image.jpg"
|
|
|
|
end
|
|
|
|
|
|
|
|
request = HTTP::Request.new("GET", "/")
|
|
|
|
response = call_request_on_app(request)
|
|
|
|
response.status_code.should eq(200)
|
|
|
|
response.headers["Content-Disposition"].should eq("attachment; filename=\"image.jpg\"")
|
|
|
|
end
|
2016-07-19 17:58:04 +00:00
|
|
|
end
|
2016-09-15 16:35:34 +00:00
|
|
|
|
|
|
|
describe "#gzip" do
|
2017-02-21 19:24:11 +00:00
|
|
|
it "adds HTTP::CompressHandler to handlers" do
|
2016-09-15 16:35:34 +00:00
|
|
|
gzip true
|
2017-07-11 08:22:35 +00:00
|
|
|
Kemal.config.setup
|
2017-02-21 19:24:11 +00:00
|
|
|
Kemal.config.handlers[4].should be_a(HTTP::CompressHandler)
|
2016-09-15 16:35:34 +00:00
|
|
|
end
|
|
|
|
end
|
2016-09-15 20:45:54 +00:00
|
|
|
|
|
|
|
describe "#serve_static" do
|
2016-10-01 15:18:28 +00:00
|
|
|
it "should disable static file hosting" do
|
2016-09-15 20:45:54 +00:00
|
|
|
serve_static false
|
|
|
|
Kemal.config.serve_static.should eq false
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should disble enable gzip and dir_listing" do
|
|
|
|
serve_static({"gzip" => true, "dir_listing" => true})
|
|
|
|
conf = Kemal.config.serve_static
|
|
|
|
conf.is_a?(Hash).should eq true
|
|
|
|
if conf.is_a?(Hash)
|
|
|
|
conf["gzip"].should eq true
|
|
|
|
conf["dir_listing"].should eq true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-01-03 11:22:43 +00:00
|
|
|
end
|