1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
require 'isolation/abstract_unit'
module ApplicationTests
class RoutingTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation
def setup
build_app
boot_rails
require 'rack/test'
extend Rack::Test::Methods
end
def teardown
teardown_app
end
def simple_controller
controller :expires, <<-RUBY
class ExpiresController < ApplicationController
def expires_header
expires_in 10, :public => !params[:private]
render :text => SecureRandom.hex(16)
end
def expires_etag
render_conditionally(:etag => "1")
end
def expires_last_modified
$last_modified ||= Time.now.utc
render_conditionally(:last_modified => $last_modified)
end
def keeps_if_modified_since
render :text => request.headers['If-Modified-Since']
end
private
def render_conditionally(headers)
if stale?(headers.merge(:public => !params[:private]))
render :text => SecureRandom.hex(16)
end
end
end
RUBY
app_file 'config/routes.rb', <<-RUBY
AppTemplate::Application.routes.draw do
match ':controller(/:action)'
end
RUBY
end
def test_cache_keeps_if_modified_since
simple_controller
expected = "Wed, 30 May 1984 19:43:31 GMT"
get "/expires/keeps_if_modified_since", {}, "HTTP_IF_MODIFIED_SINCE" => expected
assert_equal 200, last_response.status
assert_equal expected, last_response.body, "cache should have kept If-Modified-Since"
end
def test_cache_is_disabled_in_dev_mode
simple_controller
app("development")
get "/expires/expires_header"
assert_nil last_response.headers['X-Rack-Cache']
body = last_response.body
get "/expires/expires_header"
assert_nil last_response.headers['X-Rack-Cache']
assert_not_equal body, last_response.body
end
def test_cache_works_with_expires
simple_controller
get "/expires/expires_header"
assert_equal "miss, store", last_response.headers["X-Rack-Cache"]
assert_equal "max-age=10, public", last_response.headers["Cache-Control"]
body = last_response.body
get "/expires/expires_header"
assert_equal "fresh", last_response.headers["X-Rack-Cache"]
assert_equal body, last_response.body
end
def test_cache_works_with_expires_private
simple_controller
get "/expires/expires_header", :private => true
assert_equal "miss", last_response.headers["X-Rack-Cache"]
assert_equal "private, max-age=10", last_response.headers["Cache-Control"]
body = last_response.body
get "/expires/expires_header", :private => true
assert_equal "miss", last_response.headers["X-Rack-Cache"]
assert_not_equal body, last_response.body
end
def test_cache_works_with_etags
simple_controller
get "/expires/expires_etag"
assert_equal "miss, store", last_response.headers["X-Rack-Cache"]
assert_equal "public", last_response.headers["Cache-Control"]
body = last_response.body
etag = last_response.headers["ETag"]
get "/expires/expires_etag", {}, "If-None-Match" => etag
assert_equal "stale, valid, store", last_response.headers["X-Rack-Cache"]
assert_equal body, last_response.body
end
def test_cache_works_with_etags_private
simple_controller
get "/expires/expires_etag", :private => true
assert_equal "miss", last_response.headers["X-Rack-Cache"]
assert_equal "must-revalidate, private, max-age=0", last_response.headers["Cache-Control"]
body = last_response.body
etag = last_response.headers["ETag"]
get "/expires/expires_etag", {:private => true}, "If-None-Match" => etag
assert_equal "miss", last_response.headers["X-Rack-Cache"]
assert_not_equal body, last_response.body
end
def test_cache_works_with_last_modified
simple_controller
get "/expires/expires_last_modified"
assert_equal "miss, store", last_response.headers["X-Rack-Cache"]
assert_equal "public", last_response.headers["Cache-Control"]
body = last_response.body
last = last_response.headers["Last-Modified"]
get "/expires/expires_last_modified", {}, "If-Modified-Since" => last
assert_equal "stale, valid, store", last_response.headers["X-Rack-Cache"]
assert_equal body, last_response.body
end
def test_cache_works_with_last_modified_private
simple_controller
get "/expires/expires_last_modified", :private => true
assert_equal "miss", last_response.headers["X-Rack-Cache"]
assert_equal "must-revalidate, private, max-age=0", last_response.headers["Cache-Control"]
body = last_response.body
last = last_response.headers["Last-Modified"]
get "/expires/expires_last_modified", {:private => true}, "If-Modified-Since" => last
assert_equal "miss", last_response.headers["X-Rack-Cache"]
assert_not_equal body, last_response.body
end
end
end
|