cache_rules 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +3 -0
- data/.travis.yml +8 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +47 -0
- data/LICENSE +340 -0
- data/NOTICE +26 -0
- data/README.md +145 -0
- data/Rakefile +14 -0
- data/cache_rules.gemspec +34 -0
- data/lib/actions.rb +40 -0
- data/lib/cache_rules.rb +188 -0
- data/lib/formatting.rb +161 -0
- data/lib/helpers.rb +326 -0
- data/lib/validations.rb +111 -0
- data/test/helper.rb +18 -0
- data/test/test_cache_rules.rb +253 -0
- data/test/test_formatting.rb +135 -0
- data/test/test_helpers.rb +396 -0
- data/test/test_tables.rb +75 -0
- data/test/test_validations.rb +206 -0
- metadata +132 -0
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
class TestHelpers < MiniTest::Test
|
|
2
|
+
|
|
3
|
+
def setup
|
|
4
|
+
cache_control = {"max-stale"=>{"token"=>"1000", "quoted_string"=>nil}, "no-cache"=>{"token"=>nil, "quoted_string"=>nil}}
|
|
5
|
+
if_modified = {"httpdate"=>"Thu, 01 Jan 2015 07:03:42 GMT", "timestamp"=>1420095822}
|
|
6
|
+
date = {"httpdate"=>"Fri, 02 Jan 2015 11:03:45 GMT", "timestamp"=>1420196625}
|
|
7
|
+
date_minus_60 = {"httpdate"=>"Fri, 02 Jan 2015 11:02:45 GMT", "timestamp"=>1420196565}
|
|
8
|
+
next_date = {"httpdate"=>"Sat, 03 Jan 2015 07:03:45 GMT", "timestamp"=>1420268625}
|
|
9
|
+
if_modified_new = {"httpdate"=>"Sun, 04 Jan 2015 09:03:45 GMT", "timestamp"=>1420362225}
|
|
10
|
+
|
|
11
|
+
@request_headers = {
|
|
12
|
+
"If-Modified-Since" => if_modified,
|
|
13
|
+
"Cache-Control" => cache_control,
|
|
14
|
+
"If-None-Match" => ["*"]
|
|
15
|
+
}
|
|
16
|
+
@request_headers_new = {
|
|
17
|
+
"If-Modified-Since" => if_modified_new,
|
|
18
|
+
"Cache-Control" => cache_control,
|
|
19
|
+
"If-None-Match" => ["*"]
|
|
20
|
+
}
|
|
21
|
+
@request_headers_60 = {
|
|
22
|
+
"If-Modified-Since" => date_minus_60,
|
|
23
|
+
"Cache-Control" => cache_control,
|
|
24
|
+
"If-None-Match" => ["*"]
|
|
25
|
+
}
|
|
26
|
+
@request_headers_combine = {
|
|
27
|
+
"If-Modified-Since" => if_modified,
|
|
28
|
+
"Cache-Control" => cache_control,
|
|
29
|
+
"If-None-Match" => ["\"myetag\"", "\"validEtag\""]
|
|
30
|
+
}
|
|
31
|
+
@request_headers_combine_nothing = {
|
|
32
|
+
"Cache-Control" => cache_control
|
|
33
|
+
}
|
|
34
|
+
@request_headers_nothing = {
|
|
35
|
+
"If-None-Match" => ["\"myetag\""]
|
|
36
|
+
}
|
|
37
|
+
@cached_headers = {
|
|
38
|
+
"Date" => date,
|
|
39
|
+
"Cache-Control" => {
|
|
40
|
+
"public" => {"token"=>nil, "quoted_string"=>nil},
|
|
41
|
+
"max-stale" => {"token"=>"1000", "quoted_string"=>nil},
|
|
42
|
+
"no-cache" => {"token"=>nil, "quoted_string"=>"Cookie"}
|
|
43
|
+
},
|
|
44
|
+
"Last-Modified" => date,
|
|
45
|
+
"X-Cache-Req-Date" => next_date,
|
|
46
|
+
"X-Cache-Res-Date" => next_date,
|
|
47
|
+
"ETag" => "\"validEtag\""
|
|
48
|
+
}
|
|
49
|
+
@cached_rule2 = {
|
|
50
|
+
"Date" => date,
|
|
51
|
+
"X-Cache-Res-Date" => next_date
|
|
52
|
+
}
|
|
53
|
+
@cached_rule3 = {
|
|
54
|
+
"X-Cache-Res-Date" => next_date
|
|
55
|
+
}
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def test_run_validate
|
|
59
|
+
is_proc = CacheRules.helper_run_validate
|
|
60
|
+
result = is_proc.call CacheRules::REQUEST_TABLE[:conditions], @request_headers, @cached_headers, nil
|
|
61
|
+
|
|
62
|
+
assert_kind_of Proc, is_proc
|
|
63
|
+
assert_kind_of Array, result
|
|
64
|
+
assert_equal result, [1, 0, 1, 1, 1, 0, 0]
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def test_response_request
|
|
68
|
+
url = 'http://test.url'
|
|
69
|
+
act = CacheRules::REQUEST_TABLE[:actions]
|
|
70
|
+
|
|
71
|
+
column0 = CacheRules.helper_response url, act, 0, @cached_headers
|
|
72
|
+
column1 = CacheRules.helper_response url, act, 1, @cached_headers
|
|
73
|
+
column2 = CacheRules.helper_response url, act, 2, @cached_headers
|
|
74
|
+
column3 = CacheRules.helper_response url, act, 3, @cached_headers
|
|
75
|
+
column4 = CacheRules.helper_response url, act, 4, @cached_headers
|
|
76
|
+
column5 = CacheRules.helper_response url, act, 5, @cached_headers
|
|
77
|
+
column6 = CacheRules.helper_response url, act, 6, @cached_headers
|
|
78
|
+
column7 = CacheRules.helper_response url, act, 7, @cached_headers
|
|
79
|
+
column8 = CacheRules.helper_response url, act, 8, @cached_headers
|
|
80
|
+
|
|
81
|
+
assert_equal column0, {:body=>nil, :code=>307, :headers=>{"Cache-Lookup"=>"MISS", "Location"=>"http://test.url"}}
|
|
82
|
+
assert_equal column1, {:body=>"Gateway Timeout", :code=>504, :headers=>{"Cache-Lookup"=>"MISS"}}
|
|
83
|
+
|
|
84
|
+
assert_equal column2[:body], 'cached'
|
|
85
|
+
assert_equal column2[:code], 200
|
|
86
|
+
assert_equal column2[:headers]['Cache-Lookup'], 'HIT'
|
|
87
|
+
assert_includes column2[:headers], 'Age'
|
|
88
|
+
|
|
89
|
+
assert_nil column3[:body]
|
|
90
|
+
assert_equal column3[:code], 304
|
|
91
|
+
assert_equal column3[:headers]['Cache-Lookup'], 'HIT'
|
|
92
|
+
assert_equal column3[:headers]['Date'], "Fri, 02 Jan 2015 11:03:45 GMT"
|
|
93
|
+
assert_equal column3[:headers]['Cache-Control'], "public, max-stale=1000, no-cache=\"Cookie\""
|
|
94
|
+
assert_equal column3[:headers]['ETag'], "\"validEtag\""
|
|
95
|
+
assert_includes column3[:headers], 'Age'
|
|
96
|
+
|
|
97
|
+
assert_equal column4[:body], 'stale'
|
|
98
|
+
assert_equal column4[:code], 200
|
|
99
|
+
assert_equal column4[:headers]['Cache-Lookup'], 'STALE'
|
|
100
|
+
assert_equal column4[:headers]['Warning'], "110 - \"Response is Stale\""
|
|
101
|
+
assert_includes column4[:headers], 'Age'
|
|
102
|
+
|
|
103
|
+
assert_nil column5[:body]
|
|
104
|
+
assert_equal column5[:code], 304
|
|
105
|
+
assert_equal column5[:headers]['Cache-Lookup'], 'STALE'
|
|
106
|
+
assert_equal column5[:headers]['Warning'], "110 - \"Response is Stale\""
|
|
107
|
+
assert_equal column5[:headers]['Date'], "Fri, 02 Jan 2015 11:03:45 GMT"
|
|
108
|
+
assert_equal column5[:headers]['Cache-Control'], "public, max-stale=1000, no-cache=\"Cookie\""
|
|
109
|
+
assert_equal column5[:headers]['ETag'], "\"validEtag\""
|
|
110
|
+
assert_includes column5[:headers], 'Age'
|
|
111
|
+
|
|
112
|
+
assert_equal column6, {:body=>"Gateway Timeout", :code=>504, :headers=>{"Cache-Lookup"=>"EXPIRED"}}
|
|
113
|
+
assert_equal column7, {:body=>nil, :code=>nil, :headers=>nil}
|
|
114
|
+
assert_equal column8, {:body=>nil, :code=>nil, :headers=>nil}
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def test_response_revalidate
|
|
118
|
+
url = 'http://test.url'
|
|
119
|
+
act = CacheRules::RESPONSE_TABLE[:actions]
|
|
120
|
+
|
|
121
|
+
column0 = CacheRules.helper_response url, act, 0, @cached_headers
|
|
122
|
+
column1 = CacheRules.helper_response url, act, 1, @cached_headers
|
|
123
|
+
column2 = CacheRules.helper_response url, act, 2, @cached_headers
|
|
124
|
+
column3 = CacheRules.helper_response url, act, 3, @cached_headers
|
|
125
|
+
column4 = CacheRules.helper_response url, act, 4, @cached_headers
|
|
126
|
+
column5 = CacheRules.helper_response url, act, 5, @cached_headers
|
|
127
|
+
|
|
128
|
+
assert_equal column0[:body], 'cached'
|
|
129
|
+
assert_equal column0[:code], 200
|
|
130
|
+
assert_equal column0[:headers]['Cache-Lookup'], 'REVALIDATED'
|
|
131
|
+
assert_includes column0[:headers], 'Age'
|
|
132
|
+
|
|
133
|
+
assert_nil column1[:body]
|
|
134
|
+
assert_equal column1[:code], 304
|
|
135
|
+
assert_equal column1[:headers]['Cache-Lookup'], 'REVALIDATED'
|
|
136
|
+
assert_equal column1[:headers]['Date'], "Fri, 02 Jan 2015 11:03:45 GMT"
|
|
137
|
+
assert_equal column1[:headers]['Cache-Control'], "public, max-stale=1000, no-cache=\"Cookie\""
|
|
138
|
+
assert_equal column1[:headers]['ETag'], "\"validEtag\""
|
|
139
|
+
assert_includes column1[:headers], 'Age'
|
|
140
|
+
|
|
141
|
+
assert_equal column2, {:body=>"Gateway Timeout", :code=>504, :headers=>{"Cache-Lookup"=>"EXPIRED"}}
|
|
142
|
+
|
|
143
|
+
assert_equal column3[:body], 'stale'
|
|
144
|
+
assert_equal column3[:code], 200
|
|
145
|
+
assert_equal column3[:headers]['Cache-Lookup'], 'STALE'
|
|
146
|
+
assert_equal column3[:headers]['Warning'], "111 - \"Revalidation Failed\""
|
|
147
|
+
assert_includes column3[:headers], 'Age'
|
|
148
|
+
|
|
149
|
+
assert_nil column4[:body]
|
|
150
|
+
assert_equal column4[:code], 304
|
|
151
|
+
assert_equal column4[:headers]['Cache-Lookup'], 'STALE'
|
|
152
|
+
assert_equal column4[:headers]['Warning'], "111 - \"Revalidation Failed\""
|
|
153
|
+
assert_equal column4[:headers]['Date'], "Fri, 02 Jan 2015 11:03:45 GMT"
|
|
154
|
+
assert_equal column4[:headers]['Cache-Control'], "public, max-stale=1000, no-cache=\"Cookie\""
|
|
155
|
+
assert_equal column4[:headers]['ETag'], "\"validEtag\""
|
|
156
|
+
assert_includes column4[:headers], 'Age'
|
|
157
|
+
|
|
158
|
+
assert_equal column5, {:body=>nil, :code=>307, :headers=>{"Cache-Lookup"=>"EXPIRED", "Location"=>"http://test.url"}}
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def test_is_if_modified_error
|
|
162
|
+
not_error = CacheRules.helper_is_if_modified_error? 'If-Modified-Since', "Fri, 02 Jan 2015 11:03:45 GMT"
|
|
163
|
+
is_error = CacheRules.helper_is_if_modified_error? 'If-Modified-Since', "invalid date"
|
|
164
|
+
noop = CacheRules.helper_is_if_modified_error? 'Not-Even-A-Key', "test"
|
|
165
|
+
|
|
166
|
+
assert_kind_of FalseClass, not_error
|
|
167
|
+
assert_kind_of TrueClass, is_error
|
|
168
|
+
assert_nil noop
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
def test_headers_304
|
|
172
|
+
headers = CacheRules.helper_headers_304.call @cached_headers
|
|
173
|
+
|
|
174
|
+
assert_kind_of Hash, headers
|
|
175
|
+
|
|
176
|
+
assert_equal headers, {"Date"=>"Fri, 02 Jan 2015 11:03:45 GMT", "Cache-Control"=>"public, max-stale=1000, no-cache=\"Cookie\"", "ETag"=>"\"validEtag\""}
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
def test_helper_has_star
|
|
180
|
+
ystar = CacheRules.helper_has_star "*"
|
|
181
|
+
nstar = CacheRules.helper_has_star "myetag"
|
|
182
|
+
ymulti = CacheRules.helper_has_star ["test", "*"]
|
|
183
|
+
nmulti = CacheRules.helper_has_star ["test1", "test2"]
|
|
184
|
+
|
|
185
|
+
assert_kind_of TrueClass, ystar
|
|
186
|
+
assert_kind_of FalseClass, nstar
|
|
187
|
+
assert_kind_of TrueClass, ymulti
|
|
188
|
+
assert_kind_of FalseClass, nmulti
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
def test_combine_etags
|
|
192
|
+
star = CacheRules.helper_combine_etags @request_headers, @cached_headers
|
|
193
|
+
match = CacheRules.helper_combine_etags @request_headers_combine, @cached_headers
|
|
194
|
+
nomatch = CacheRules.helper_combine_etags @request_headers_combine_nothing, @cached_headers
|
|
195
|
+
|
|
196
|
+
assert_equal star, "*"
|
|
197
|
+
assert_equal match, "\"myetag\", \"validEtag\""
|
|
198
|
+
assert_equal nomatch, "\"validEtag\""
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
def test_timestamp
|
|
202
|
+
request = CacheRules.helper_timestamp @request_headers, @cached_headers
|
|
203
|
+
cached = CacheRules.helper_timestamp @request_headers_combine_nothing, @cached_headers
|
|
204
|
+
nothing = CacheRules.helper_timestamp({}, {})
|
|
205
|
+
|
|
206
|
+
assert_nil nothing
|
|
207
|
+
|
|
208
|
+
assert_equal request, "Thu, 01 Jan 2015 07:03:42 GMT"
|
|
209
|
+
assert_equal cached, "Fri, 02 Jan 2015 11:03:45 GMT"
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
def test_etag
|
|
213
|
+
etag_match = CacheRules.helper_etag @request_headers, @cached_headers
|
|
214
|
+
etag_nomatch = CacheRules.helper_etag @request_headers_combine_nothing, @cached_headers
|
|
215
|
+
etag_combined = CacheRules.helper_etag @request_headers_combine, @cached_headers
|
|
216
|
+
etag_nothing = CacheRules.helper_etag @request_headers_nothing, @cached_headers
|
|
217
|
+
|
|
218
|
+
assert_kind_of TrueClass, etag_match
|
|
219
|
+
assert_nil etag_nomatch
|
|
220
|
+
assert_kind_of TrueClass, etag_combined
|
|
221
|
+
assert_kind_of FalseClass, etag_nothing
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
def test_last_modified
|
|
225
|
+
guard = CacheRules.helper_last_modified @request_headers_combine_nothing, @cached_headers
|
|
226
|
+
rule1 = CacheRules.helper_last_modified @request_headers_new, @cached_headers
|
|
227
|
+
rule2 = CacheRules.helper_last_modified @request_headers_new, @cached_rule2
|
|
228
|
+
rule3 = CacheRules.helper_last_modified @request_headers_new, @cached_rule3
|
|
229
|
+
rule4 = CacheRules.helper_last_modified @request_headers_60, @cached_headers
|
|
230
|
+
|
|
231
|
+
assert_nil guard
|
|
232
|
+
assert_kind_of TrueClass, rule1
|
|
233
|
+
assert_kind_of TrueClass, rule2
|
|
234
|
+
assert_kind_of TrueClass, rule3
|
|
235
|
+
assert_kind_of TrueClass, rule4
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
def test_validate_allow_stale
|
|
239
|
+
no_cache = CacheRules.helper_validate_allow_stale @request_headers, @cached_headers
|
|
240
|
+
no_store = CacheRules.helper_validate_allow_stale({"Cache-Control" => {"no-store"=>{"token"=>nil, "quoted_string"=>nil}}}, @cached_headers)
|
|
241
|
+
pragma = CacheRules.helper_validate_allow_stale({"Pragma" => "no-cache"}, @cached_rule2)
|
|
242
|
+
|
|
243
|
+
cached_no_cache = CacheRules.helper_validate_allow_stale({}, @cached_headers)
|
|
244
|
+
cached_no_store = CacheRules.helper_validate_allow_stale({}, {"Cache-Control" => {"no-store"=>{"token"=>nil, "quoted_string"=>nil}}})
|
|
245
|
+
cached_must_rev = CacheRules.helper_validate_allow_stale({}, {"Cache-Control" => {"must-revalidate"=>{"token"=>nil, "quoted_string"=>nil}}})
|
|
246
|
+
cached_s_maxage = CacheRules.helper_validate_allow_stale({}, {"Cache-Control" => {"s-maxage"=>{"token"=>nil, "quoted_string"=>nil}}})
|
|
247
|
+
cached_proxy_re = CacheRules.helper_validate_allow_stale({}, {"Cache-Control" => {"proxy-revalidate"=>{"token"=>nil, "quoted_string"=>nil}}})
|
|
248
|
+
|
|
249
|
+
nothing = CacheRules.helper_validate_allow_stale({}, @cached_rule2)
|
|
250
|
+
|
|
251
|
+
assert_kind_of TrueClass, no_cache
|
|
252
|
+
assert_kind_of TrueClass, no_store
|
|
253
|
+
assert_kind_of TrueClass, pragma
|
|
254
|
+
|
|
255
|
+
assert_kind_of TrueClass, cached_no_cache
|
|
256
|
+
assert_kind_of TrueClass, cached_no_store
|
|
257
|
+
assert_kind_of TrueClass, cached_must_rev
|
|
258
|
+
assert_kind_of TrueClass, cached_s_maxage
|
|
259
|
+
assert_kind_of TrueClass, cached_proxy_re
|
|
260
|
+
|
|
261
|
+
assert_nil nothing
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
def test_apparent_age
|
|
265
|
+
older = CacheRules.helper_apparent_age(1420196565, 1420196505).call
|
|
266
|
+
current = CacheRules.helper_apparent_age(1420196565, 1420196565).call
|
|
267
|
+
newer = CacheRules.helper_apparent_age(1420196505, 1420196565).call
|
|
268
|
+
|
|
269
|
+
assert_equal older, 60
|
|
270
|
+
assert_equal current, 0
|
|
271
|
+
assert_equal newer, 0
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
def test_corrected_age_value
|
|
275
|
+
zero = CacheRules.helper_corrected_age_value(1420196565, 1420196565, 0).call
|
|
276
|
+
one_hundred = CacheRules.helper_corrected_age_value(1420196565, 1420196565, 100).call
|
|
277
|
+
sixty = CacheRules.helper_corrected_age_value(1420196565, 1420196505, 0).call
|
|
278
|
+
one_sixty = CacheRules.helper_corrected_age_value(1420196565, 1420196505, 100).call
|
|
279
|
+
impossible = CacheRules.helper_corrected_age_value(1420196505, 1420196565, 0).call
|
|
280
|
+
|
|
281
|
+
assert_equal zero, 0
|
|
282
|
+
assert_equal one_hundred, 100
|
|
283
|
+
assert_equal sixty, 60
|
|
284
|
+
assert_equal one_sixty, 160
|
|
285
|
+
assert_equal impossible, -60
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
def test_corrected_initial_age
|
|
289
|
+
via_good1 = {'Via' => ['HTTP/1.1 test.com'], 'Age' => 100}
|
|
290
|
+
via_good2 = {'Via' => ['1.1 test.com'], 'Age' => 200}
|
|
291
|
+
via_good3 = {'Via' => ['HTTP/1.1'], 'Age' => 300}
|
|
292
|
+
via_good4 = {'Via' => ['1.1'], 'Age' => 400}
|
|
293
|
+
via_bad = {'Via' => ['HTTP/1.1', '1.0'], 'Age' => 500}
|
|
294
|
+
via_noage = {'Via' => ['1.1']}
|
|
295
|
+
|
|
296
|
+
apparent_age = Proc.new { 1000 }
|
|
297
|
+
|
|
298
|
+
good1 = CacheRules.helper_corrected_initial_age(via_good1, Proc.new { 100 }, apparent_age).call
|
|
299
|
+
good2 = CacheRules.helper_corrected_initial_age(via_good2, Proc.new { 200 }, apparent_age).call
|
|
300
|
+
good3 = CacheRules.helper_corrected_initial_age(via_good3, Proc.new { 300 }, apparent_age).call
|
|
301
|
+
good4 = CacheRules.helper_corrected_initial_age(via_good4, Proc.new { 400 }, apparent_age).call
|
|
302
|
+
bad = CacheRules.helper_corrected_initial_age(via_bad, Proc.new { 500 }, apparent_age).call
|
|
303
|
+
noage = CacheRules.helper_corrected_initial_age(via_noage, Proc.new { 42 }, apparent_age).call
|
|
304
|
+
|
|
305
|
+
assert_equal good1, 100
|
|
306
|
+
assert_equal good2, 200
|
|
307
|
+
assert_equal good3, 300
|
|
308
|
+
assert_equal good4, 400
|
|
309
|
+
assert_equal bad, 1000
|
|
310
|
+
assert_equal noage, 1000
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
def test_current_age
|
|
314
|
+
now = @request_headers_new['If-Modified-Since']['timestamp']
|
|
315
|
+
|
|
316
|
+
current_age = CacheRules.helper_current_age now, @cached_headers
|
|
317
|
+
|
|
318
|
+
assert_equal current_age, 165600
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
def test_freshness_lifetime
|
|
322
|
+
cur_time = Time.now.gmtime.to_i
|
|
323
|
+
current_age = CacheRules.helper_current_age cur_time, @cached_headers
|
|
324
|
+
freshness = CacheRules.helper_freshness_lifetime.call @cached_headers
|
|
325
|
+
|
|
326
|
+
assert_kind_of Array, freshness
|
|
327
|
+
assert_equal freshness[0], 0
|
|
328
|
+
assert_in_delta freshness[1], current_age, 2
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
def test_explicit
|
|
332
|
+
s_maxage = CacheRules.helper_explicit({'Cache-Control' => {'s-maxage'=>{'token'=>60}}})
|
|
333
|
+
max_age = CacheRules.helper_explicit({'Cache-Control' => {'max-age'=>{'token'=>100}}})
|
|
334
|
+
expires = CacheRules.helper_explicit({'Expires' => {'timestamp'=>1420196565}, 'Date'=> {'timestamp'=>1420196565}})
|
|
335
|
+
noop = CacheRules.helper_explicit({})
|
|
336
|
+
|
|
337
|
+
assert_equal s_maxage, 60
|
|
338
|
+
assert_equal max_age, 100
|
|
339
|
+
assert_equal expires, 0
|
|
340
|
+
assert_nil noop
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
def test_heuristic
|
|
344
|
+
now = @request_headers_new['If-Modified-Since']['timestamp']
|
|
345
|
+
|
|
346
|
+
last_modified = CacheRules.helper_heuristic now, @cached_headers, 100
|
|
347
|
+
not_public = CacheRules.helper_heuristic now, @cached_rule2, 100
|
|
348
|
+
too_old = CacheRules.helper_heuristic now, @cached_headers, 86401
|
|
349
|
+
noop = CacheRules.helper_heuristic(now, {}, 42)
|
|
350
|
+
|
|
351
|
+
assert_equal last_modified, 16560
|
|
352
|
+
assert_equal not_public, 0
|
|
353
|
+
assert_equal too_old, 0
|
|
354
|
+
assert_equal noop, 0
|
|
355
|
+
end
|
|
356
|
+
|
|
357
|
+
def test_max_stale
|
|
358
|
+
stale = CacheRules.helper_max_stale.call @request_headers['Cache-Control'], 0, 0
|
|
359
|
+
fresh = CacheRules.helper_max_stale.call @request_headers['Cache-Control'], 0, 2000
|
|
360
|
+
noop = CacheRules.helper_max_stale.call @request_headers_nothing, 0, 0
|
|
361
|
+
notoken = CacheRules.helper_max_stale.call({'max-stale'=>{'token'=>nil}}, 0, 0)
|
|
362
|
+
|
|
363
|
+
assert_kind_of TrueClass, stale
|
|
364
|
+
assert_kind_of FalseClass, fresh
|
|
365
|
+
assert_nil noop
|
|
366
|
+
assert_kind_of TrueClass, notoken
|
|
367
|
+
end
|
|
368
|
+
|
|
369
|
+
def test_min_fresh
|
|
370
|
+
min = CacheRules.helper_min_fresh.call({'min-fresh'=>{'token'=>1000}}, 0, 0)
|
|
371
|
+
fresh = CacheRules.helper_min_fresh.call({'min-fresh'=>{'token'=>"1000"}}, 2000, 0)
|
|
372
|
+
noop = CacheRules.helper_min_fresh.call @request_headers_nothing, 0, 0
|
|
373
|
+
|
|
374
|
+
assert_kind_of FalseClass, min
|
|
375
|
+
assert_kind_of TrueClass, fresh
|
|
376
|
+
assert_nil noop
|
|
377
|
+
end
|
|
378
|
+
|
|
379
|
+
def test_no_cache
|
|
380
|
+
quoted = CacheRules.helper_no_cache.call @cached_headers
|
|
381
|
+
not_quoted = CacheRules.helper_no_cache.call({'Cache-Control'=>{'no-cache'=>{'quoted_string'=>""}}})
|
|
382
|
+
noop = CacheRules.helper_no_cache.call({'Cache-Control'=>{'no-cache'=>nil}})
|
|
383
|
+
|
|
384
|
+
assert_kind_of TrueClass, quoted
|
|
385
|
+
assert_kind_of FalseClass, not_quoted
|
|
386
|
+
assert_nil noop
|
|
387
|
+
end
|
|
388
|
+
|
|
389
|
+
def test_make_request
|
|
390
|
+
result = CacheRules.helper_make_request 'fake http object', 'fake request object'
|
|
391
|
+
|
|
392
|
+
assert_kind_of Proc, result
|
|
393
|
+
assert_equal result.arity, 0
|
|
394
|
+
end
|
|
395
|
+
|
|
396
|
+
end
|
data/test/test_tables.rb
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
class TestTables < MiniTest::Test
|
|
2
|
+
|
|
3
|
+
def setup
|
|
4
|
+
@request_map = {"0000000"=>0, "0000001"=>0, "0000010"=>1, "0000011"=>1, "0000100"=>0, "0000101"=>0, "0000110"=>1, "0000111"=>1, "0001000"=>0, "0001001"=>0, "0001010"=>1, "0001011"=>1, "0001100"=>0, "0001101"=>0, "0001110"=>1, "0001111"=>1, "0010000"=>0, "0010001"=>0, "0010010"=>1, "0010011"=>1, "0010100"=>0, "0010101"=>0, "0010110"=>1, "0010111"=>1, "0011000"=>0, "0011001"=>0, "0011010"=>1, "0011011"=>1, "0011100"=>0, "0011101"=>0, "0011110"=>1, "0011111"=>1, "0100000"=>0, "0100001"=>0, "0100010"=>1, "0100011"=>1, "0100100"=>0, "0100101"=>0, "0100110"=>1, "0100111"=>1, "0101000"=>0, "0101001"=>0, "0101010"=>1, "0101011"=>1, "0101100"=>0, "0101101"=>0, "0101110"=>1, "0101111"=>1, "0110000"=>0, "0110001"=>0, "0110010"=>1, "0110011"=>1, "0110100"=>0, "0110101"=>0, "0110110"=>1, "0110111"=>1, "0111000"=>0, "0111001"=>0, "0111010"=>1, "0111011"=>1, "0111100"=>0, "0111101"=>0, "0111110"=>1, "0111111"=>1, "1000000"=>2, "1000001"=>2, "1000010"=>2, "1000011"=>2, "1000100"=>6, "1000101"=>4, "1000110"=>6, "1000111"=>4, "1001000"=>3, "1001001"=>3, "1001010"=>3, "1001011"=>3, "1001100"=>6, "1001101"=>5, "1001110"=>6, "1001111"=>5, "1010000"=>8, "1010001"=>8, "1010010"=>8, "1010011"=>8, "1010100"=>8, "1010101"=>8, "1010110"=>8, "1010111"=>8, "1011000"=>8, "1011001"=>8, "1011010"=>8, "1011011"=>8, "1011100"=>8, "1011101"=>8, "1011110"=>8, "1011111"=>8, "1100000"=>2, "1100001"=>2, "1100010"=>2, "1100011"=>2, "1100100"=>7, "1100101"=>7, "1100110"=>7, "1100111"=>7, "1101000"=>3, "1101001"=>3, "1101010"=>3, "1101011"=>3, "1101100"=>7, "1101101"=>7, "1101110"=>7, "1101111"=>7, "1110000"=>8, "1110001"=>8, "1110010"=>8, "1110011"=>8, "1110100"=>8, "1110101"=>8, "1110110"=>8, "1110111"=>8, "1111000"=>8, "1111001"=>8, "1111010"=>8, "1111011"=>8, "1111100"=>8, "1111101"=>8, "1111110"=>8, "1111111"=>8}
|
|
5
|
+
@response_map = {"0000"=>0, "0001"=>1, "0010"=>0, "0011"=>1, "0100"=>5, "0101"=>5, "0110"=>5, "0111"=>5, "1000"=>2, "1001"=>2, "1010"=>3, "1011"=>4, "1100"=>2, "1101"=>2, "1110"=>3, "1111"=>4}
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def test_x_value
|
|
9
|
+
assert_kind_of NilClass, CacheRules::X
|
|
10
|
+
|
|
11
|
+
assert_nil CacheRules::X
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def test_request_table
|
|
15
|
+
assert_kind_of Hash, CacheRules::REQUEST_TABLE
|
|
16
|
+
|
|
17
|
+
assert_includes CacheRules::REQUEST_TABLE, :conditions
|
|
18
|
+
assert_includes CacheRules::REQUEST_TABLE, :actions
|
|
19
|
+
|
|
20
|
+
assert_equal CacheRules::REQUEST_TABLE[:conditions].length, 7
|
|
21
|
+
assert_equal CacheRules::REQUEST_TABLE[:actions].length, 6
|
|
22
|
+
|
|
23
|
+
conditions = CacheRules::REQUEST_TABLE[:conditions].keys
|
|
24
|
+
actions = CacheRules::REQUEST_TABLE[:actions].keys
|
|
25
|
+
assert_equal conditions, %w(cached must_revalidate no_cache precond_match expired only_if_cached allow_stale)
|
|
26
|
+
assert_equal actions, %w(revalidate add_age add_x_cache add_warning add_status return_body)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def test_response_table
|
|
30
|
+
assert_kind_of Hash, CacheRules::RESPONSE_TABLE
|
|
31
|
+
|
|
32
|
+
assert_includes CacheRules::RESPONSE_TABLE, :conditions
|
|
33
|
+
assert_includes CacheRules::RESPONSE_TABLE, :actions
|
|
34
|
+
|
|
35
|
+
assert_equal CacheRules::RESPONSE_TABLE[:conditions].length, 4
|
|
36
|
+
assert_equal CacheRules::RESPONSE_TABLE[:actions].length, 6
|
|
37
|
+
|
|
38
|
+
conditions = CacheRules::RESPONSE_TABLE[:conditions].keys
|
|
39
|
+
actions = CacheRules::RESPONSE_TABLE[:actions].keys
|
|
40
|
+
assert_equal conditions, %w(is_error expired allow_stale validator_match)
|
|
41
|
+
assert_equal actions, %w(revalidate add_age add_x_cache add_warning add_status return_body)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def test_request_map_is_correct
|
|
45
|
+
assert_kind_of Hash, CacheRules::REQUEST_MAP
|
|
46
|
+
|
|
47
|
+
assert_equal CacheRules::REQUEST_MAP.length, 128
|
|
48
|
+
assert_equal CacheRules::REQUEST_MAP, @request_map
|
|
49
|
+
|
|
50
|
+
assert_equal CacheRules::REQUEST_MAP["0000000"], 0
|
|
51
|
+
assert_equal CacheRules::REQUEST_MAP["0000010"], 1
|
|
52
|
+
assert_equal CacheRules::REQUEST_MAP["1000000"], 2
|
|
53
|
+
assert_equal CacheRules::REQUEST_MAP["1001000"], 3
|
|
54
|
+
assert_equal CacheRules::REQUEST_MAP["1000101"], 4
|
|
55
|
+
assert_equal CacheRules::REQUEST_MAP["1001101"], 5
|
|
56
|
+
assert_equal CacheRules::REQUEST_MAP["1000100"], 6
|
|
57
|
+
assert_equal CacheRules::REQUEST_MAP["1100100"], 7
|
|
58
|
+
assert_equal CacheRules::REQUEST_MAP["1010000"], 8
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def test_response_map_is_correct
|
|
62
|
+
assert_kind_of Hash, CacheRules::RESPONSE_MAP
|
|
63
|
+
|
|
64
|
+
assert_equal CacheRules::RESPONSE_MAP.length, 16
|
|
65
|
+
assert_equal CacheRules::RESPONSE_MAP, @response_map
|
|
66
|
+
|
|
67
|
+
assert_equal CacheRules::RESPONSE_MAP["0000"], 0
|
|
68
|
+
assert_equal CacheRules::RESPONSE_MAP["0001"], 1
|
|
69
|
+
assert_equal CacheRules::RESPONSE_MAP["1000"], 2
|
|
70
|
+
assert_equal CacheRules::RESPONSE_MAP["1010"], 3
|
|
71
|
+
assert_equal CacheRules::RESPONSE_MAP["1011"], 4
|
|
72
|
+
assert_equal CacheRules::RESPONSE_MAP["0100"], 5
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
end
|