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

Commit

Permalink
Verify preconditions before revalidating. Closes #13
Browse files Browse the repository at this point in the history
  • Loading branch information
aw committed May 4, 2015
1 parent 1520b25 commit 7787996
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,12 @@
# Changelog

## 0.5.0 (2015-05-04)

* Add a check which verifies if a precondition exists before revalidating
* Fixes issue #13
* Add regression tests
* Adjust one broken test

## 0.4.1 (2015-05-04)

* Update README.md
Expand Down
2 changes: 1 addition & 1 deletion cache_rules.gemspec
Expand Up @@ -6,7 +6,7 @@ require 'date'

Gem::Specification.new do |s|
s.name = 'cache_rules'
s.version = '0.4.1'
s.version = '0.5.0'

s.date = Date.today.to_s

Expand Down
10 changes: 8 additions & 2 deletions lib/cache_rules.rb
Expand Up @@ -121,10 +121,16 @@ def validate(url, request_headers, cached_headers = {})
# Revalidates a response by fetching headers from the origin server
def revalidate_response(*args)
url, request, cached = *args
res_headers = helper_response_headers.(helper_make_request_timer.(args))
has_preconditions = helper_has_preconditions.(request, cached)

# 1. get the column
column = RESPONSE_MAP[helper_run_validate.call(RESPONSE_TABLE[:conditions], request, cached, res_headers).join]
column = if has_preconditions
res_headers = helper_response_headers.(helper_make_request_timer.(args))
RESPONSE_MAP[helper_run_validate.call(RESPONSE_TABLE[:conditions], request, cached, res_headers).join]
else
res_headers = {}
2 # return column 2 (504 EXPIRED)
end

# 2. return the response
helper_response url, RESPONSE_TABLE[:actions], column, cached, res_headers
Expand Down
7 changes: 7 additions & 0 deletions lib/helpers.rb
Expand Up @@ -360,4 +360,11 @@ def helper_response_headers
}
end

# The validators are required for revalidation
# source: https://tools.ietf.org/html/rfc7232#section-2
def helper_has_preconditions
Proc.new {|request, cached|
request['If-None-Match'] || cached['ETag'] || cached['Last-Modified']
}
end
end
2 changes: 1 addition & 1 deletion test/test_cache_rules.rb
Expand Up @@ -174,7 +174,7 @@ def test_revalidate_response_column2_5xx
end

def test_revalidate_response_column2_error
result = CacheRules.revalidate_response('ftp://test.url/test1', {}, {})
result = CacheRules.revalidate_response('ftp://test.url/test1', {}, {'Last-Modified'=>'Sat, 03 Jan 2015 07:03:45 GMT'})

assert_equal result[:code], 504
assert_equal result[:body], 'Gateway Timeout'
Expand Down
19 changes: 19 additions & 0 deletions test/test_regressions.rb
Expand Up @@ -66,4 +66,23 @@ def test_bugfix_10_request_header_max_age_is_checked
assert_equal 1, current
assert_equal 1, cached_max_age
end

# https://github.com/aw/CacheRules/issues/13
def test_bugfix_13_revalidate_without_preconditions
if_none_match = CacheRules.helper_has_preconditions.({'If-None-Match'=>'*'},{})
etag = CacheRules.helper_has_preconditions.({}, {'ETag'=>["abcdefg"]})
last_modified = CacheRules.helper_has_preconditions.({}, {'Last-Modified'=>{"httpdate"=>"Fri, 02 Jan 2015 11:03:45 GMT", "timestamp"=>1420196625}})
empty = CacheRules.helper_has_preconditions.({}, {})
no_precond = CacheRules.revalidate_response('ftp://test.url/test1', {}, {})

assert_equal "*", if_none_match
assert_equal ["abcdefg"], etag
assert_equal({"httpdate"=>"Fri, 02 Jan 2015 11:03:45 GMT", "timestamp"=>1420196625}, last_modified)
assert_nil empty

assert_equal no_precond[:code], 504
assert_equal no_precond[:body], 'Gateway Timeout'
assert_nil no_precond[:error]
end

end

0 comments on commit 7787996

Please sign in to comment.