cache_rules 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +7 -0
- data/cache_rules.gemspec +1 -1
- data/lib/cache_rules.rb +8 -2
- data/lib/helpers.rb +7 -0
- data/test/test_cache_rules.rb +1 -1
- data/test/test_regressions.rb +19 -0
- metadata +1 -1
data/CHANGELOG.md
CHANGED
data/cache_rules.gemspec
CHANGED
data/lib/cache_rules.rb
CHANGED
@@ -121,10 +121,16 @@ module CacheRules
|
|
121
121
|
# Revalidates a response by fetching headers from the origin server
|
122
122
|
def revalidate_response(*args)
|
123
123
|
url, request, cached = *args
|
124
|
-
|
124
|
+
has_preconditions = helper_has_preconditions.(request, cached)
|
125
125
|
|
126
126
|
# 1. get the column
|
127
|
-
column =
|
127
|
+
column = if has_preconditions
|
128
|
+
res_headers = helper_response_headers.(helper_make_request_timer.(args))
|
129
|
+
RESPONSE_MAP[helper_run_validate.call(RESPONSE_TABLE[:conditions], request, cached, res_headers).join]
|
130
|
+
else
|
131
|
+
res_headers = {}
|
132
|
+
2 # return column 2 (504 EXPIRED)
|
133
|
+
end
|
128
134
|
|
129
135
|
# 2. return the response
|
130
136
|
helper_response url, RESPONSE_TABLE[:actions], column, cached, res_headers
|
data/lib/helpers.rb
CHANGED
@@ -360,4 +360,11 @@ module CacheRules
|
|
360
360
|
}
|
361
361
|
end
|
362
362
|
|
363
|
+
# The validators are required for revalidation
|
364
|
+
# source: https://tools.ietf.org/html/rfc7232#section-2
|
365
|
+
def helper_has_preconditions
|
366
|
+
Proc.new {|request, cached|
|
367
|
+
request['If-None-Match'] || cached['ETag'] || cached['Last-Modified']
|
368
|
+
}
|
369
|
+
end
|
363
370
|
end
|
data/test/test_cache_rules.rb
CHANGED
@@ -174,7 +174,7 @@ class TestCacheRules < MiniTest::Test
|
|
174
174
|
end
|
175
175
|
|
176
176
|
def test_revalidate_response_column2_error
|
177
|
-
result = CacheRules.revalidate_response('ftp://test.url/test1', {}, {})
|
177
|
+
result = CacheRules.revalidate_response('ftp://test.url/test1', {}, {'Last-Modified'=>'Sat, 03 Jan 2015 07:03:45 GMT'})
|
178
178
|
|
179
179
|
assert_equal result[:code], 504
|
180
180
|
assert_equal result[:body], 'Gateway Timeout'
|
data/test/test_regressions.rb
CHANGED
@@ -66,4 +66,23 @@ class TestRegressions < MiniTest::Test
|
|
66
66
|
assert_equal 1, current
|
67
67
|
assert_equal 1, cached_max_age
|
68
68
|
end
|
69
|
+
|
70
|
+
# https://github.com/aw/CacheRules/issues/13
|
71
|
+
def test_bugfix_13_revalidate_without_preconditions
|
72
|
+
if_none_match = CacheRules.helper_has_preconditions.({'If-None-Match'=>'*'},{})
|
73
|
+
etag = CacheRules.helper_has_preconditions.({}, {'ETag'=>["abcdefg"]})
|
74
|
+
last_modified = CacheRules.helper_has_preconditions.({}, {'Last-Modified'=>{"httpdate"=>"Fri, 02 Jan 2015 11:03:45 GMT", "timestamp"=>1420196625}})
|
75
|
+
empty = CacheRules.helper_has_preconditions.({}, {})
|
76
|
+
no_precond = CacheRules.revalidate_response('ftp://test.url/test1', {}, {})
|
77
|
+
|
78
|
+
assert_equal "*", if_none_match
|
79
|
+
assert_equal ["abcdefg"], etag
|
80
|
+
assert_equal({"httpdate"=>"Fri, 02 Jan 2015 11:03:45 GMT", "timestamp"=>1420196625}, last_modified)
|
81
|
+
assert_nil empty
|
82
|
+
|
83
|
+
assert_equal no_precond[:code], 504
|
84
|
+
assert_equal no_precond[:body], 'Gateway Timeout'
|
85
|
+
assert_nil no_precond[:error]
|
86
|
+
end
|
87
|
+
|
69
88
|
end
|