Skip to content

Commit

Permalink
Fix StaticCache middleware (#167)
Browse files Browse the repository at this point in the history
* StaticCache. Send Date header in response

* StaticCahe. Use current time in UTC

* StaticCache. Don't cache Expires header
  • Loading branch information
andrykonchin committed Mar 28, 2020
1 parent 750cece commit 143055e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
8 changes: 5 additions & 3 deletions lib/rack/contrib/static_cache.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'time'

module Rack

#
Expand Down Expand Up @@ -68,7 +70,6 @@ def initialize(app, options={})
@version_regex = options.fetch(:version_regex, /-[\d.]+([.][a-zA-Z][\w]+)?$/)
end
@duration_in_seconds = self.duration_in_seconds
@duration_in_words = self.duration_in_words
end

def call(env)
Expand All @@ -83,14 +84,15 @@ def call(env)
status, headers, body = @file_server.call(env)
if @no_cache[url].nil?
headers['Cache-Control'] ="max-age=#{@duration_in_seconds}, public"
headers['Expires'] = @duration_in_words
headers['Expires'] = duration_in_words
end
headers['Date'] = Time.now.httpdate
[status, headers, body]
end
end

def duration_in_words
(Time.now + self.duration_in_seconds).strftime '%a, %d %b %Y %H:%M:%S GMT'
(Time.now.utc + self.duration_in_seconds).httpdate
end

def duration_in_seconds
Expand Down
1 change: 1 addition & 0 deletions rack-contrib.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'rake', '~> 10.4', '>= 10.4.2'
s.add_development_dependency 'rdoc', '~> 5.0'
s.add_development_dependency 'ruby-prof', '~> 0.17'
s.add_development_dependency 'timecop', '~> 0.9'

s.homepage = "https://github.com/rack/rack-contrib/"
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "rack-contrib", "--main", "README"]
Expand Down
1 change: 1 addition & 0 deletions test/gemfiles/minimum_versions
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ gem 'nbio-csshttprequest', '= 1.0.2'
gem 'rdoc', '= 5.0.0'
gem 'rake', '= 10.4.2'
gem 'ruby-prof', '= 0.17.0'
gem 'timecop', '= 0.4.1'
33 changes: 32 additions & 1 deletion test/spec_rack_static_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'rack'
require 'rack/contrib/static_cache'
require 'rack/mock'
require 'timecop'

class DummyApp
def call(env)
Expand All @@ -16,8 +17,12 @@ def static_root
end

def request(options)
Rack::MockRequest.new(build_middleware(options))
end

def build_middleware(options)
options = { :root => static_root }.merge(options)
Rack::MockRequest.new(Rack::StaticCache.new(DummyApp.new, options))
Rack::StaticCache.new(DummyApp.new, options)
end

describe "with a default app request" do
Expand Down Expand Up @@ -52,6 +57,32 @@ def get_request(path)
)
end

it "should set Expires header based on current UTC time" do
Timecop.freeze(DateTime.parse("2020-03-28 23:51 UTC")) do
_(get_request("/statics/test").headers['Expires']).must_match("Sun, 28 Mar 2021 23:51:00 GMT") # now + 1 year
end
end

it "should not cache expiration date between requests" do
middleware = build_middleware(:urls => ["/statics"])

Timecop.freeze(DateTime.parse("2020-03-28 23:41 UTC")) do
r = Rack::MockRequest.new(middleware)
_(r.get("/statics/test").headers["Expires"]).must_equal "Sun, 28 Mar 2021 23:41:00 GMT" # time now + 1 year
end

Timecop.freeze(DateTime.parse("2020-03-28 23:51 UTC")) do
r = Rack::MockRequest.new(middleware)
_(r.get("/statics/test").headers["Expires"]).must_equal "Sun, 28 Mar 2021 23:51:00 GMT" # time now + 1 year
end
end

it "should set Date header with current GMT time" do
Timecop.freeze(DateTime.parse('2020-03-28 22:51 UTC')) do
_(get_request("/statics/test").headers['Date']).must_equal 'Sat, 28 Mar 2020 22:51:00 GMT'
end
end

it "should return 404s if url root is known but it can't find the file" do
_(get_request("/statics/non-existent").not_found?).must_equal(true)
end
Expand Down

0 comments on commit 143055e

Please sign in to comment.