HeadRequestHandler: run GET handler and don't return the body (#655)

This commit is contained in:
Mike Robbins 2023-02-21 23:34:47 -05:00 committed by GitHub
parent 84ea6627ac
commit 8ebe171279
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 115 additions and 8 deletions

View file

@ -0,0 +1,37 @@
require "./spec_helper"
describe "Kemal::HeadRequestHandler" do
it "implicitly handles GET endpoints, with Content-Length header" do
get "/" do
"hello"
end
request = HTTP::Request.new("HEAD", "/")
client_response = call_request_on_app(request)
client_response.body.should eq("")
client_response.headers["Content-Length"].should eq("5")
end
it "prefers explicit HEAD endpoint if specified" do
Kemal::RouteHandler::INSTANCE.add_route("HEAD", "/") { "hello" }
get "/" do
raise "shouldn't be called!"
end
request = HTTP::Request.new("HEAD", "/")
client_response = call_request_on_app(request)
client_response.body.should eq("")
client_response.headers["Content-Length"].should eq("5")
end
it "gives compressed Content-Length when gzip enabled" do
gzip true
get "/" do
"hello"
end
headers = HTTP::Headers{"Accept-Encoding" => "gzip"}
request = HTTP::Request.new("HEAD", "/", headers)
client_response = call_request_on_app(request)
client_response.body.should eq("")
client_response.headers["Content-Encoding"].should eq("gzip")
client_response.headers["Content-Length"].should eq("25")
end
end