Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Date header to HTTP responses #676

Merged
merged 5 commits into from Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions spec/init_handler_spec.cr
Expand Up @@ -11,6 +11,18 @@ describe "Kemal::InitHandler" do
context.response.headers["Content-Type"].should eq "text/html"
end

it "initializes context with Date header" do
request = HTTP::Request.new("GET", "/")
io = IO::Memory.new
response = HTTP::Server::Response.new(io)
context = HTTP::Server::Context.new(request, response)
Kemal::InitHandler::INSTANCE.next = ->(_context : HTTP::Server::Context) {}
Kemal::InitHandler::INSTANCE.call(context)
date = context.response.headers["Date"]?.should_not be_nil
date = HTTP.parse_time(date).should_not be_nil
date.should be_close(Time.utc, 1.second)
end

it "initializes context with X-Powered-By: Kemal" do
request = HTTP::Request.new("GET", "/")
io = IO::Memory.new
Expand Down
3 changes: 3 additions & 0 deletions src/kemal/init_handler.cr
@@ -1,3 +1,5 @@
require "http"

module Kemal
# Initializes the context with default values, such as
# *Content-Type* or *X-Powered-By* headers.
Expand All @@ -9,6 +11,7 @@ module Kemal
def call(context : HTTP::Server::Context)
context.response.headers.add "X-Powered-By", "Kemal" if Kemal.config.powered_by_header?
context.response.content_type = "text/html" unless context.response.headers.has_key?("Content-Type")
context.response.headers.add "Date", HTTP.format_time(Time.utc)
call_next context
end
end
Expand Down