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

Commit 86213cd

Browse files
committed
Simplify revalidation table
1 parent 2098e94 commit 86213cd

File tree

9 files changed

+47
-60
lines changed

9 files changed

+47
-60
lines changed

CHANGELOG.md

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

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+
39
# 0.3.0 (2015-05-03)
410

511
* Make cache rules consistent based RFC spec

README.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,18 @@ HTTP Caching request.
6666
### Revalidation Table
6767

6868
| 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 || - | 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 || - | 0 | 1 |
7473
| |
7574
| **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 | |
8281

8382
## RFC compliance
8483

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.3.0'
9+
s.version = '0.4.0'
1010

1111
s.date = Date.today.to_s
1212

lib/cache_rules.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,17 @@ module CacheRules
8484
# Decision table for revalidated responses
8585
RESPONSE_TABLE = {
8686
: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]
9190
},
9291
:actions => {
9392
'revalidate' => [],
9493
'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),
9695
'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']
9998
}
10099
}
101100

lib/validations.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def validate_is_error?(headers)
123123

124124
def validate_validator_match?(headers)
125125
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?("*")) }
127127
end
128128

129129
end

test/test_cache_rules.rb

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def test_validate_column4
8080

8181
result = CacheRules.validate('http://test.url/test1', request, cached)
8282

83-
assert_equal result[:code], 200
83+
assert_equal 200, result[:code]
8484
assert_equal result[:body], 'stale'
8585
assert_equal result[:headers]['Cache-Lookup'], 'STALE'
8686
assert_equal result[:headers]['Warning'], "110 - \"Response is Stale\""
@@ -110,29 +110,27 @@ def test_validate_column6
110110
end
111111

112112
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"=>["*"]}
114114
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}}}
115115

116116
FakeWeb.register_uri(:head, "http://test.url/test1", :status => ["304", "Not Modified"], :date => "Sat, 03 Jan 2015 07:15:45 GMT")
117117
result = CacheRules.validate('http://test.url/test1', request, cached)
118118

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'
123122
end
124123

125124
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"=>["*"]}
127126
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}}}
128127

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)
131130

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'
136134
end
137135

138136
def test_revalidate_response_column0
@@ -144,7 +142,7 @@ def test_revalidate_response_column0
144142

145143
assert_equal result[:code], 200
146144
assert_equal result[:body], 'cached'
147-
assert_equal result[:headers]['Cache-Lookup'], 'REVALIDATED'
145+
assert_equal 'REVALIDATED', result[:headers]['Cache-Lookup']
148146
assert_equal result[:headers]['Warning'], "299 - \"Hello World\""
149147
end
150148

@@ -210,19 +208,6 @@ def test_revalidate_response_column4
210208
assert_equal result[:headers]['ETag'], "\"validEtag\""
211209
end
212210

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-
226211
def test_make_http_request_with_entity_tag
227212
result = CacheRules.make_request.call('http://test.url', @request_if_none_match, @cached_headers)
228213
http = eval "http", result.binding

test/test_helpers.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ def test_response_revalidate
125125
column2 = CacheRules.helper_response url, act, 2, @cached_headers
126126
column3 = CacheRules.helper_response url, act, 3, @cached_headers
127127
column4 = CacheRules.helper_response url, act, 4, @cached_headers
128-
column5 = CacheRules.helper_response url, act, 5, @cached_headers
129128

130129
assert_equal column0[:body], 'cached'
131130
assert_equal column0[:code], 200
@@ -157,8 +156,6 @@ def test_response_revalidate
157156
assert_equal column4[:headers]['ETag'], "\"validEtag\""
158157
assert_includes column4[:headers], 'Age'
159158

160-
assert_equal column5, {:body=>nil, :code=>307, :headers=>{"Cache-Lookup"=>"EXPIRED", "Location"=>"http://test.url"}}
161-
162159
assert_kind_of String, column0[:headers]['Age']
163160
end
164161

test/test_tables.rb

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class TestTables < MiniTest::Test
22

33
def setup
44
@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}
66
end
77

88
def test_x_value
@@ -32,12 +32,12 @@ def test_response_table
3232
assert_includes CacheRules::RESPONSE_TABLE, :conditions
3333
assert_includes CacheRules::RESPONSE_TABLE, :actions
3434

35-
assert_equal CacheRules::RESPONSE_TABLE[:conditions].length, 4
35+
assert_equal CacheRules::RESPONSE_TABLE[:conditions].length, 3
3636
assert_equal CacheRules::RESPONSE_TABLE[:actions].length, 6
3737

3838
conditions = CacheRules::RESPONSE_TABLE[:conditions].keys
3939
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)
4141
assert_equal actions, %w(revalidate add_age add_x_cache add_warning add_status return_body)
4242
end
4343

@@ -61,15 +61,14 @@ def test_request_map_is_correct
6161
def test_response_map_is_correct
6262
assert_kind_of Hash, CacheRules::RESPONSE_MAP
6363

64-
assert_equal CacheRules::RESPONSE_MAP.length, 16
64+
assert_equal CacheRules::RESPONSE_MAP.length, 8
6565
assert_equal CacheRules::RESPONSE_MAP, @response_map
6666

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
7372
end
7473

7574
end

test/test_validations.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,11 @@ def test_is_error
222222

223223
def test_validator_match
224224
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\""}})
225226
nomatch = CacheRules.validate_validator_match?({:request => {'If-None-Match'=>["\"myetag\""]}, :response => {}})
226227

227228
assert_equal match, 1
229+
assert_equal 1, match1
228230
assert_equal nomatch, 0
229231
end
230232

0 commit comments

Comments
 (0)