Skip to content

Commit 143055e

Browse files
authored
Fix StaticCache middleware (#167)
* StaticCache. Send Date header in response * StaticCahe. Use current time in UTC * StaticCache. Don't cache Expires header
1 parent 750cece commit 143055e

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

lib/rack/contrib/static_cache.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'time'
2+
13
module Rack
24

35
#
@@ -68,7 +70,6 @@ def initialize(app, options={})
6870
@version_regex = options.fetch(:version_regex, /-[\d.]+([.][a-zA-Z][\w]+)?$/)
6971
end
7072
@duration_in_seconds = self.duration_in_seconds
71-
@duration_in_words = self.duration_in_words
7273
end
7374

7475
def call(env)
@@ -83,14 +84,15 @@ def call(env)
8384
status, headers, body = @file_server.call(env)
8485
if @no_cache[url].nil?
8586
headers['Cache-Control'] ="max-age=#{@duration_in_seconds}, public"
86-
headers['Expires'] = @duration_in_words
87+
headers['Expires'] = duration_in_words
8788
end
89+
headers['Date'] = Time.now.httpdate
8890
[status, headers, body]
8991
end
9092
end
9193

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

9698
def duration_in_seconds

rack-contrib.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Gem::Specification.new do |s|
5151
s.add_development_dependency 'rake', '~> 10.4', '>= 10.4.2'
5252
s.add_development_dependency 'rdoc', '~> 5.0'
5353
s.add_development_dependency 'ruby-prof', '~> 0.17'
54+
s.add_development_dependency 'timecop', '~> 0.9'
5455

5556
s.homepage = "https://github.com/rack/rack-contrib/"
5657
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "rack-contrib", "--main", "README"]

test/gemfiles/minimum_versions

+1
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ gem 'nbio-csshttprequest', '= 1.0.2'
1717
gem 'rdoc', '= 5.0.0'
1818
gem 'rake', '= 10.4.2'
1919
gem 'ruby-prof', '= 0.17.0'
20+
gem 'timecop', '= 0.4.1'

test/spec_rack_static_cache.rb

+32-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'rack'
44
require 'rack/contrib/static_cache'
55
require 'rack/mock'
6+
require 'timecop'
67

78
class DummyApp
89
def call(env)
@@ -16,8 +17,12 @@ def static_root
1617
end
1718

1819
def request(options)
20+
Rack::MockRequest.new(build_middleware(options))
21+
end
22+
23+
def build_middleware(options)
1924
options = { :root => static_root }.merge(options)
20-
Rack::MockRequest.new(Rack::StaticCache.new(DummyApp.new, options))
25+
Rack::StaticCache.new(DummyApp.new, options)
2126
end
2227

2328
describe "with a default app request" do
@@ -52,6 +57,32 @@ def get_request(path)
5257
)
5358
end
5459

60+
it "should set Expires header based on current UTC time" do
61+
Timecop.freeze(DateTime.parse("2020-03-28 23:51 UTC")) do
62+
_(get_request("/statics/test").headers['Expires']).must_match("Sun, 28 Mar 2021 23:51:00 GMT") # now + 1 year
63+
end
64+
end
65+
66+
it "should not cache expiration date between requests" do
67+
middleware = build_middleware(:urls => ["/statics"])
68+
69+
Timecop.freeze(DateTime.parse("2020-03-28 23:41 UTC")) do
70+
r = Rack::MockRequest.new(middleware)
71+
_(r.get("/statics/test").headers["Expires"]).must_equal "Sun, 28 Mar 2021 23:41:00 GMT" # time now + 1 year
72+
end
73+
74+
Timecop.freeze(DateTime.parse("2020-03-28 23:51 UTC")) do
75+
r = Rack::MockRequest.new(middleware)
76+
_(r.get("/statics/test").headers["Expires"]).must_equal "Sun, 28 Mar 2021 23:51:00 GMT" # time now + 1 year
77+
end
78+
end
79+
80+
it "should set Date header with current GMT time" do
81+
Timecop.freeze(DateTime.parse('2020-03-28 22:51 UTC')) do
82+
_(get_request("/statics/test").headers['Date']).must_equal 'Sat, 28 Mar 2020 22:51:00 GMT'
83+
end
84+
end
85+
5586
it "should return 404s if url root is known but it can't find the file" do
5687
_(get_request("/statics/non-existent").not_found?).must_equal(true)
5788
end

0 commit comments

Comments
 (0)