Skip to content
This repository was archived by the owner on Dec 3, 2019. It is now read-only.

Commit 7787996

Browse files
committed
Verify preconditions before revalidating. Closes #13
1 parent 1520b25 commit 7787996

6 files changed

Lines changed: 43 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## 0.5.0 (2015-05-04)
4+
5+
* Add a check which verifies if a precondition exists before revalidating
6+
* Fixes issue #13
7+
* Add regression tests
8+
* Adjust one broken test
9+
310
## 0.4.1 (2015-05-04)
411

512
* Update README.md

cache_rules.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require 'date'
66

77
Gem::Specification.new do |s|
88
s.name = 'cache_rules'
9-
s.version = '0.4.1'
9+
s.version = '0.5.0'
1010

1111
s.date = Date.today.to_s
1212

lib/cache_rules.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,16 @@ def validate(url, request_headers, cached_headers = {})
121121
# Revalidates a response by fetching headers from the origin server
122122
def revalidate_response(*args)
123123
url, request, cached = *args
124-
res_headers = helper_response_headers.(helper_make_request_timer.(args))
124+
has_preconditions = helper_has_preconditions.(request, cached)
125125

126126
# 1. get the column
127-
column = RESPONSE_MAP[helper_run_validate.call(RESPONSE_TABLE[:conditions], request, cached, res_headers).join]
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
128134

129135
# 2. return the response
130136
helper_response url, RESPONSE_TABLE[:actions], column, cached, res_headers

lib/helpers.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,4 +360,11 @@ def helper_response_headers
360360
}
361361
end
362362

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
363370
end

test/test_cache_rules.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def test_revalidate_response_column2_5xx
174174
end
175175

176176
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'})
178178

179179
assert_equal result[:code], 504
180180
assert_equal result[:body], 'Gateway Timeout'

test/test_regressions.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,23 @@ def test_bugfix_10_request_header_max_age_is_checked
6666
assert_equal 1, current
6767
assert_equal 1, cached_max_age
6868
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+
6988
end

0 commit comments

Comments
 (0)