3
3
require 'rack'
4
4
require 'rack/contrib/static_cache'
5
5
require 'rack/mock'
6
+ require 'timecop'
6
7
7
8
class DummyApp
8
9
def call ( env )
@@ -16,8 +17,12 @@ def static_root
16
17
end
17
18
18
19
def request ( options )
20
+ Rack ::MockRequest . new ( build_middleware ( options ) )
21
+ end
22
+
23
+ def build_middleware ( options )
19
24
options = { :root => static_root } . merge ( options )
20
- Rack ::MockRequest . new ( Rack :: StaticCache . new ( DummyApp . new , options ) )
25
+ Rack ::StaticCache . new ( DummyApp . new , options )
21
26
end
22
27
23
28
describe "with a default app request" do
@@ -52,6 +57,32 @@ def get_request(path)
52
57
)
53
58
end
54
59
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
+
55
86
it "should return 404s if url root is known but it can't find the file" do
56
87
_ ( get_request ( "/statics/non-existent" ) . not_found? ) . must_equal ( true )
57
88
end
0 commit comments