cache_rules 0.4.1 → 0.5.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
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
+
3
10
  ## 0.4.1 (2015-05-04)
4
11
 
5
12
  * Update README.md
data/cache_rules.gemspec CHANGED
@@ -6,7 +6,7 @@ require 'date'
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = 'cache_rules'
9
- s.version = '0.4.1'
9
+ s.version = '0.5.0'
10
10
 
11
11
  s.date = Date.today.to_s
12
12
 
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
- res_headers = helper_response_headers.(helper_make_request_timer.(args))
124
+ has_preconditions = helper_has_preconditions.(request, cached)
125
125
 
126
126
  # 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
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
@@ -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'
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cache_rules
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: