33require 'rack'
44require 'rack/contrib/static_cache'
55require 'rack/mock'
6+ require 'timecop'
67
78class 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