cache_rules 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ # 0.4.0 (2015-05-03)
4
+
5
+ * Modify 'revalidation' table so it doesn't check if the response is expired
6
+ * Ensure validator matching works with '*'
7
+ * Fix tests
8
+
3
9
  # 0.3.0 (2015-05-03)
4
10
 
5
11
  * Make cache rules consistent based RFC spec
data/README.md CHANGED
@@ -66,19 +66,18 @@ HTTP Caching request.
66
66
  ### Revalidation Table
67
67
 
68
68
  | Revalidation Conditions | | | | | | |
69
- | :------------------------| :---: | :---: | :---: | :---: | :---: | :---: |
70
- | **Did we get an error or 5xx code?** | 0 | 0 | 1 | 1 | 1 | 0 |
71
- | **Is the cached data expired?** | 0 | 0 | - | - | - | 1 |
72
- | **Is there an if-only-cached header?** | - | - | 0 | 1 | 1 | - |
73
- | **Do Etags/If-None-Match match?** | 0 | 1 | - | 0 | 1 | - |
69
+ | :------------------------| :---: | :---: | :---: | :---: | :---: |
70
+ | **Did we get an error or 5xx code?** | 0 | 0 | 1 | 1 | 1 |
71
+ | **Is there an if-only-cached header?** | - | - | 0 | 1 | 1 |
72
+ | **Do Etags/If-None-Match match?** | 0 | 1 | - | 0 | 1 |
74
73
  | |
75
74
  | **Actions** | |
76
- | **Revalidate** | | | | | | |
77
- | **Add Age header (regeneratte**) | 1 | 1 | | 1 | 1 | |
78
- | **Add Cache-Lookup header** | REVALIDATED | REVALIDATED | EXPIRED | STALE | STALE | EXPIRED |
79
- | **Add Warning header** | | | | 111 | 111 | |
80
- | **Return Status Code** | 200 | 304 | 504 | 200 | 304 | 307 |
81
- | **Return Body** | cached | | | stale | | |
75
+ | **Revalidate** | | | | | |
76
+ | **Add Age header (regeneratte**) | 1 | 1 | | 1 | 1 |
77
+ | **Add Cache-Lookup header** | REVALIDATED | REVALIDATED | EXPIRED | STALE | STALE |
78
+ | **Add Warning header** | | | | 111 | 111 |
79
+ | **Return Status Code** | 200 | 304 | 504 | 200 | 304 |
80
+ | **Return Body** | cached | | | stale | |
82
81
 
83
82
  ## RFC compliance
84
83
 
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.3.0'
9
+ s.version = '0.4.0'
10
10
 
11
11
  s.date = Date.today.to_s
12
12
 
data/lib/cache_rules.rb CHANGED
@@ -84,18 +84,17 @@ module CacheRules
84
84
  # Decision table for revalidated responses
85
85
  RESPONSE_TABLE = {
86
86
  :conditions => {
87
- 'is_error' => [0, 0, 1, 1, 1, 0],
88
- 'expired' => [0, 0, X, X, X, 1],
89
- 'allow_stale' => [X, X, 0, 1, 1, X],
90
- 'validator_match' => [0, 1, X, 0, 1, X]
87
+ 'is_error' => [0, 0, 1, 1, 1],
88
+ 'allow_stale' => [X, X, 0, 1, 1],
89
+ 'validator_match' => [0, 1, X, 0, 1]
91
90
  },
92
91
  :actions => {
93
92
  'revalidate' => [],
94
93
  'add_age' => [1, 1, X, 1, 1],
95
- 'add_x_cache' => %w(REVALIDATED REVALIDATED EXPIRED STALE STALE EXPIRED),
94
+ 'add_x_cache' => %w(REVALIDATED REVALIDATED EXPIRED STALE STALE),
96
95
  'add_warning' => [X, X, X, '111 - "Revalidation Failed"', '111 - "Revalidation Failed"'],
97
- 'add_status' => [200, 304, 504, 200, 304, 307],
98
- 'return_body' => ['cached', X, 'Gateway Timeout', 'stale', X]
96
+ 'add_status' => [200, 304, 504, 200, 304],
97
+ 'return_body' => ['cached', X, 'Gateway Timeout', 'stale']
99
98
  }
100
99
  }
101
100
 
data/lib/validations.rb CHANGED
@@ -123,7 +123,7 @@ module CacheRules
123
123
 
124
124
  def validate_validator_match?(headers)
125
125
  request, response = headers.values_at :request, :response
126
- to_bit { response['ETag'] && request['If-None-Match'] && request['If-None-Match'].include?(response['ETag']) }
126
+ to_bit { response['ETag'] && request['If-None-Match'] && (request['If-None-Match'].include?(response['ETag']) || request['If-None-Match'].include?("*")) }
127
127
  end
128
128
 
129
129
  end
@@ -80,7 +80,7 @@ class TestCacheRules < MiniTest::Test
80
80
 
81
81
  result = CacheRules.validate('http://test.url/test1', request, cached)
82
82
 
83
- assert_equal result[:code], 200
83
+ assert_equal 200, result[:code]
84
84
  assert_equal result[:body], 'stale'
85
85
  assert_equal result[:headers]['Cache-Lookup'], 'STALE'
86
86
  assert_equal result[:headers]['Warning'], "110 - \"Response is Stale\""
@@ -110,29 +110,27 @@ class TestCacheRules < MiniTest::Test
110
110
  end
111
111
 
112
112
  def test_validate_column7
113
- request = {"Host"=>"test.url", "Cache-Control"=>"max-stale=0", "If-None-Match"=>"*"}
113
+ request = {"Host"=>"test.url", "Cache-Control"=>"max-stale=0", "If-None-Match"=>["*"]}
114
114
  cached = {"Date"=>{"httpdate"=>"Sat, 03 Jan 2015 07:03:45 GMT", "timestamp"=>1420268625}, "X-Cache-Req-Date"=>{"httpdate"=>"Sat, 03 Jan 2015 07:03:45 GMT", "timestamp"=>1420268625}, "X-Cache-Res-Date"=>{"httpdate"=>"Sat, 03 Jan 2015 07:03:45 GMT", "timestamp"=>1420268625}, "Last-Modified" => {"httpdate"=>"Sat, 03 Jan 2015 07:03:45 GMT", "timestamp"=>1420268625}, "Last-Modified" => {"httpdate"=>"Sat, 03 Jan 2015 07:03:45 GMT", "timestamp"=>1420268625}, "ETag" => "\"validEtag\"", "Cache-Control"=>{"max-age"=>{"token"=>"100", "quoted_string" => nil}, "must-revalidate"=>{"token"=>nil, "quoted_string"=>nil}}}
115
115
 
116
116
  FakeWeb.register_uri(:head, "http://test.url/test1", :status => ["304", "Not Modified"], :date => "Sat, 03 Jan 2015 07:15:45 GMT")
117
117
  result = CacheRules.validate('http://test.url/test1', request, cached)
118
118
 
119
- assert_equal result[:code], 307
120
- assert_nil result[:body]
121
- assert_equal result[:headers]['Cache-Lookup'], 'EXPIRED'
122
- assert_equal result[:headers]['Location'], "http://test.url/test1"
119
+ assert_equal result[:code], 200
120
+ assert_equal result[:body], "cached"
121
+ assert_equal result[:headers]['Cache-Lookup'], 'REVALIDATED'
123
122
  end
124
123
 
125
124
  def test_validate_column8
126
- request = {"Host"=>"test.url", "Cache-Control"=>"max-stale=0", "If-None-Match"=>"*"}
125
+ request = {"Host"=>"test.url", "Cache-Control"=>"max-stale=0", "If-None-Match"=>["*"]}
127
126
  cached = {"Date"=>{"httpdate"=>"Sat, 03 Jan 2015 07:03:45 GMT", "timestamp"=>1420268625}, "X-Cache-Req-Date"=>{"httpdate"=>"Sat, 03 Jan 2015 07:03:45 GMT", "timestamp"=>1420268625}, "X-Cache-Res-Date"=>{"httpdate"=>"Sat, 03 Jan 2015 07:03:45 GMT", "timestamp"=>1420268625}, "Last-Modified" => {"httpdate"=>"Sat, 03 Jan 2015 07:03:45 GMT", "timestamp"=>1420268625}, "Last-Modified" => {"httpdate"=>"Sat, 03 Jan 2015 07:03:45 GMT", "timestamp"=>1420268625}, "ETag" => "\"validEtag\"", "Cache-Control"=>{"max-age"=>{"token"=>"100", "quoted_string" => nil}, "no-cache"=>{"token"=>nil, "quoted_string"=>nil}}}
128
127
 
129
- FakeWeb.register_uri(:head, "http://test.url/test1", :status => ["304", "Not Modified"], :date => "Sat, 03 Jan 2015 07:15:45 GMT")
130
- result = CacheRules.validate('http://test.url/test1', request, cached)
128
+ FakeWeb.register_uri(:head, "http://test.url/test2", :status => ["304", "Not Modified"], :date => "Sat, 03 Jan 2015 07:15:45 GMT")
129
+ result = CacheRules.validate('http://test.url/test2', request, cached)
131
130
 
132
- assert_equal result[:code], 307
133
- assert_nil result[:body]
134
- assert_equal result[:headers]['Cache-Lookup'], 'EXPIRED'
135
- assert_equal result[:headers]['Location'], "http://test.url/test1"
131
+ assert_equal 200, result[:code]
132
+ assert_equal result[:body], "cached"
133
+ assert_equal result[:headers]['Cache-Lookup'], 'REVALIDATED'
136
134
  end
137
135
 
138
136
  def test_revalidate_response_column0
@@ -144,7 +142,7 @@ class TestCacheRules < MiniTest::Test
144
142
 
145
143
  assert_equal result[:code], 200
146
144
  assert_equal result[:body], 'cached'
147
- assert_equal result[:headers]['Cache-Lookup'], 'REVALIDATED'
145
+ assert_equal 'REVALIDATED', result[:headers]['Cache-Lookup']
148
146
  assert_equal result[:headers]['Warning'], "299 - \"Hello World\""
149
147
  end
150
148
 
@@ -210,19 +208,6 @@ class TestCacheRules < MiniTest::Test
210
208
  assert_equal result[:headers]['ETag'], "\"validEtag\""
211
209
  end
212
210
 
213
- def test_revalidate_response_column5
214
- request = {"Host"=>"test.url", "If-None-Match"=>["*"], "Cache-Control"=>{"max-stale"=>{"token"=>"100000000", "quoted_string"=>nil}}}
215
- cached = {"Date"=>{"httpdate"=>"Thu, 01 Jan 2015 07:03:45 GMT", "timestamp"=>1420095825}, "Cache-Control"=>{"public"=>{"token"=>nil, "quoted_string"=>nil}}, "X-Cache-Req-Date"=>{"httpdate"=>"Sat, 03 Jan 2015 07:03:45 GMT", "timestamp"=>1420268625}, "X-Cache-Res-Date"=>{"httpdate"=>"Sat, 03 Jan 2015 07:03:45 GMT", "timestamp"=>1420268625}}
216
-
217
- FakeWeb.register_uri(:head, "http://test.url/test1", :status => ["200", "OK"])
218
- result = CacheRules.revalidate_response('http://test.url/test1', request, cached)
219
-
220
- assert_equal result[:code], 307
221
- assert_nil result[:body]
222
- assert_equal result[:headers]['Cache-Lookup'], 'EXPIRED'
223
- assert_equal result[:headers]['Location'], "http://test.url/test1"
224
- end
225
-
226
211
  def test_make_http_request_with_entity_tag
227
212
  result = CacheRules.make_request.call('http://test.url', @request_if_none_match, @cached_headers)
228
213
  http = eval "http", result.binding
data/test/test_helpers.rb CHANGED
@@ -125,7 +125,6 @@ class TestHelpers < MiniTest::Test
125
125
  column2 = CacheRules.helper_response url, act, 2, @cached_headers
126
126
  column3 = CacheRules.helper_response url, act, 3, @cached_headers
127
127
  column4 = CacheRules.helper_response url, act, 4, @cached_headers
128
- column5 = CacheRules.helper_response url, act, 5, @cached_headers
129
128
 
130
129
  assert_equal column0[:body], 'cached'
131
130
  assert_equal column0[:code], 200
@@ -157,8 +156,6 @@ class TestHelpers < MiniTest::Test
157
156
  assert_equal column4[:headers]['ETag'], "\"validEtag\""
158
157
  assert_includes column4[:headers], 'Age'
159
158
 
160
- assert_equal column5, {:body=>nil, :code=>307, :headers=>{"Cache-Lookup"=>"EXPIRED", "Location"=>"http://test.url"}}
161
-
162
159
  assert_kind_of String, column0[:headers]['Age']
163
160
  end
164
161
 
data/test/test_tables.rb CHANGED
@@ -2,7 +2,7 @@ class TestTables < MiniTest::Test
2
2
 
3
3
  def setup
4
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}
5
+ @response_map = {"000"=>0, "001"=>1, "010"=>0, "011"=>1, "100"=>2, "101"=>2, "110"=>3, "111"=>4}
6
6
  end
7
7
 
8
8
  def test_x_value
@@ -32,12 +32,12 @@ class TestTables < MiniTest::Test
32
32
  assert_includes CacheRules::RESPONSE_TABLE, :conditions
33
33
  assert_includes CacheRules::RESPONSE_TABLE, :actions
34
34
 
35
- assert_equal CacheRules::RESPONSE_TABLE[:conditions].length, 4
35
+ assert_equal CacheRules::RESPONSE_TABLE[:conditions].length, 3
36
36
  assert_equal CacheRules::RESPONSE_TABLE[:actions].length, 6
37
37
 
38
38
  conditions = CacheRules::RESPONSE_TABLE[:conditions].keys
39
39
  actions = CacheRules::RESPONSE_TABLE[:actions].keys
40
- assert_equal conditions, %w(is_error expired allow_stale validator_match)
40
+ assert_equal conditions, %w(is_error allow_stale validator_match)
41
41
  assert_equal actions, %w(revalidate add_age add_x_cache add_warning add_status return_body)
42
42
  end
43
43
 
@@ -61,15 +61,14 @@ class TestTables < MiniTest::Test
61
61
  def test_response_map_is_correct
62
62
  assert_kind_of Hash, CacheRules::RESPONSE_MAP
63
63
 
64
- assert_equal CacheRules::RESPONSE_MAP.length, 16
64
+ assert_equal CacheRules::RESPONSE_MAP.length, 8
65
65
  assert_equal CacheRules::RESPONSE_MAP, @response_map
66
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
67
+ assert_equal CacheRules::RESPONSE_MAP["000"], 0
68
+ assert_equal CacheRules::RESPONSE_MAP["001"], 1
69
+ assert_equal CacheRules::RESPONSE_MAP["100"], 2
70
+ assert_equal CacheRules::RESPONSE_MAP["110"], 3
71
+ assert_equal CacheRules::RESPONSE_MAP["111"], 4
73
72
  end
74
73
 
75
74
  end
@@ -222,9 +222,11 @@ class TestValidations < MiniTest::Test
222
222
 
223
223
  def test_validator_match
224
224
  match = CacheRules.validate_validator_match?({:request => {'If-None-Match'=>["\"myetag\""]}, :response => {'ETag'=>"\"myetag\""}})
225
+ match1 = CacheRules.validate_validator_match?({:request => {'If-None-Match'=>["*"]}, :response => {'ETag'=>"\"myetag\""}})
225
226
  nomatch = CacheRules.validate_validator_match?({:request => {'If-None-Match'=>["\"myetag\""]}, :response => {}})
226
227
 
227
228
  assert_equal match, 1
229
+ assert_equal 1, match1
228
230
  assert_equal nomatch, 0
229
231
  end
230
232
 
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.3.0
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: