cache_rules 0.1.4 → 0.1.5

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/.travis.yml CHANGED
@@ -5,6 +5,4 @@ rvm:
5
5
  - 2.0.0
6
6
  - 2.1.5
7
7
  - 2.2.0
8
- script: "bundle exec rake test"
9
- notifications:
10
- email: false
8
+ script: "bundle exec rake test"
data/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # Changelog
2
+
3
+ ## 0.1.5 (2014-02-02)
4
+
5
+ * Closes #3. Returns all cached headers according to RFC 7234 sec4.3.4
6
+
7
+ ## 0.1.4 (2014-02-01)
8
+
9
+ * HTTP `Age` header is now returned as a String, but processed as an Integer
data/Gemfile.lock CHANGED
@@ -36,6 +36,7 @@ GEM
36
36
  tins (1.3.3)
37
37
 
38
38
  PLATFORMS
39
+ java
39
40
  ruby
40
41
 
41
42
  DEPENDENCIES
data/README.md CHANGED
@@ -56,7 +56,7 @@ HTTP Caching request.
56
56
  | **Is a stale response acceptable?** | - | - | - | - | 1 | 1 | 0 | - | - |
57
57
  | |
58
58
  | **Actions** | |
59
- | **Revalidate** | | | | | | | | | 1 | 1 |
59
+ | **Revalidate** | | | | | | | | 1 | 1 |
60
60
  | **Add Age header (regeneratte**) | | | 1 | 1 | 1 | 1 | | | |
61
61
  | **Add Cache-Lookup header** | MISS | MISS | HIT | HIT | STALE | STALE | EXPIRED | | |
62
62
  | **Add Warning header** | | | | | 110 | 110 | | | |
@@ -78,7 +78,7 @@ HTTP Caching request.
78
78
  | **Add Cache-Lookup header** | REVALIDATED | REVALIDATED | EXPIRED | STALE | STALE | EXPIRED |
79
79
  | **Add Warning header** | | | | 111 | 111 | |
80
80
  | **Return Status Code** | 200 | 304 | 504 | 200 | 304 | 307 |
81
- | **Return Body** | cached | | | stale | stale | |
81
+ | **Return Body** | cached | | | stale | | |
82
82
 
83
83
  ## RFC compliance
84
84
 
data/Rakefile CHANGED
@@ -8,7 +8,7 @@ def run_test
8
8
 
9
9
  Rake::TestTask.new do |t|
10
10
  t.libs << "test"
11
- t.test_files = FileList['test/helper.rb', 'test/test_*.rb']
11
+ t.test_files = FileList['test/helper.rb', 'test/test_formatting.rb']
12
12
  end
13
13
 
14
14
  end
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.1.4'
9
+ s.version = '0.1.5'
10
10
 
11
11
  s.date = Date.today.to_s
12
12
 
data/lib/cache_rules.rb CHANGED
@@ -54,8 +54,6 @@ module CacheRules
54
54
  Age Content-Length Max-Forwards
55
55
  )
56
56
 
57
- HEADERS_304 = %w(Cache-Control Content-Location Date ETag Expires Vary)
58
-
59
57
  OPTIONS_CACHE = HEADERS_CSV.select {|header| header == 'Cache-Control' }
60
58
  OPTIONS_CSV = HEADERS_CSV.reject {|header| header == 'Cache-Control' }
61
59
  OPTIONS_RETRY = %w(Retry-After)
@@ -137,7 +135,7 @@ module CacheRules
137
135
  column = RESPONSE_MAP[helper_run_validate.call(RESPONSE_TABLE[:conditions], request_headers, cached_headers, response_headers).join]
138
136
 
139
137
  # 2. return the response
140
- helper_response url, RESPONSE_TABLE[:actions], column, cached_headers
138
+ helper_response url, RESPONSE_TABLE[:actions], column, cached_headers, response_headers
141
139
  rescue => error
142
140
  {:code => 504, :body => 'Gateway Timeout', :headers => [], :error => error.message, :debug => error}
143
141
  end
data/lib/formatting.rb CHANGED
@@ -104,7 +104,7 @@ module CacheRules
104
104
  def httpdate_helper(header)
105
105
  # source: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
106
106
  DateTime.parse(header).to_time.to_i
107
- rescue => e
107
+ rescue ArgumentError => e
108
108
  # If the supplied date is invalid, use a time in the past (5 minutes ago)
109
109
  # source: https://tools.ietf.org/html/rfc7234#section-5.3
110
110
  Time.now.gmtime.to_i - 300
data/lib/helpers.rb CHANGED
@@ -61,11 +61,12 @@ module CacheRules
61
61
  end
62
62
 
63
63
  # Returns the response body, code and headers based on the actions results
64
- def helper_response(url, actions, column, cached)
64
+ def helper_response(url, actions, column, cached, response = {})
65
65
  _, age, x_cache, warning, status, body = helper_run_action actions, column, cached
66
66
 
67
- headers_304 = helper_headers_304.call(cached) if status == 304
68
- headers_url = {'Location' => url} if status == 307
67
+ normalized = normalize.call response
68
+ headers_304 = helper_headers_200_304.call(cached, normalized) if status == 200 || status == 304
69
+ headers_url = {'Location' => url} if status == 307
69
70
 
70
71
  headers = [age, warning, x_cache, headers_304, headers_url].compact.reduce &:merge
71
72
 
@@ -83,11 +84,22 @@ module CacheRules
83
84
  end
84
85
  end
85
86
 
86
- # Generate the same headers if they exist for 304 responses
87
+ # Generate the same headers if they exist for 200/304 responses
87
88
  # source: https://tools.ietf.org/html/rfc7232#section-4.1
88
- def helper_headers_304
89
- Proc.new {|cached|
90
- unnormalize_fields.call cached.select {|x| HEADERS_304.include? x }
89
+ def helper_headers_200_304
90
+ Proc.new {|cached, response|
91
+ new_headers = response.select &helper_remove_warning_1xx
92
+ unnormalize_fields.call cached.merge(new_headers).reject {|key, _|
93
+ key == 'X-Cache-Req-Date' || key == 'X-Cache-Res-Date' || key == 'Status'
94
+ }
95
+ }
96
+ end
97
+
98
+ # delete 1xx Warning headers
99
+ # source: https://tools.ietf.org/html/rfc7234#section-4.3.4
100
+ def helper_remove_warning_1xx
101
+ Proc.new {|key, value|
102
+ {key => value} unless key == 'Warning' && value.reject! {|x| x =~ /^1\d{2}/ } && value.length == 0
91
103
  }
92
104
  end
93
105
 
data/test/helper.rb CHANGED
@@ -2,7 +2,7 @@ require 'coveralls'
2
2
  require 'fakeweb'
3
3
  require 'simplecov'
4
4
 
5
- Coveralls.wear!
5
+ # Coveralls.wear!
6
6
 
7
7
  SimpleCov.start do
8
8
  add_filter '/test/'
@@ -128,25 +128,28 @@ class TestCacheRules < MiniTest::Test
128
128
  request = {"Host"=>"test.url", "If-None-Match"=>["*"], "Cache-Control"=>{"max-age"=>{"token"=>"100000000", "quoted_string"=>nil}}}
129
129
  cached = {"Date"=>{"httpdate"=>"Sat, 03 Jan 2015 07:03:45 GMT", "timestamp"=>1420268625}, "Cache-Control"=>{"s-maxage"=>{"token"=>"100000000", "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}, "Last-Modified" => {"httpdate"=>"Sat, 03 Jan 2015 07:03:45 GMT", "timestamp"=>1420268625}}
130
130
 
131
- FakeWeb.register_uri(:head, "http://test.url/test1", :status => ["304", "Not Modified"], :date => "Sat, 03 Jan 2015 07:15:45 GMT")
131
+ FakeWeb.register_uri(:head, "http://test.url/test1", :status => ["304", "Not Modified"], :date => "Sat, 03 Jan 2015 07:15:45 GMT", :Warning => "299 - \"Hello World\"")
132
132
  result = CacheRules.revalidate_response('http://test.url/test1', request, cached)
133
133
 
134
134
  assert_equal result[:code], 200
135
135
  assert_equal result[:body], 'cached'
136
136
  assert_equal result[:headers]['Cache-Lookup'], 'REVALIDATED'
137
+ assert_equal result[:headers]['Warning'], "299 - \"Hello World\""
137
138
  end
138
139
 
139
140
  def test_revalidate_response_column1
140
141
  request = {"Host"=>"test.url", "If-None-Match"=>["\"validEtag\""], "Cache-Control"=>{"max-age"=>{"token"=>"100000000", "quoted_string"=>nil}}}
141
- cached = {"Date"=>{"httpdate"=>"Sat, 03 Jan 2015 07:03:45 GMT", "timestamp"=>1420268625}, "Cache-Control"=>{"s-maxage"=>{"token"=>"100000000", "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}, "Last-Modified" => {"httpdate"=>"Sat, 03 Jan 2015 07:03:45 GMT", "timestamp"=>1420268625}, "ETag" => "\"validEtag\""}
142
+ cached = {"Date"=>{"httpdate"=>"Sat, 03 Jan 2015 07:03:45 GMT", "timestamp"=>1420268625}, "Cache-Control"=>{"s-maxage"=>{"token"=>"100000000", "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}, "Last-Modified" => {"httpdate"=>"Sat, 03 Jan 2015 07:03:45 GMT", "timestamp"=>1420268625}, "ETag" => "\"validEtag\"", "Content-Type" => "text/html"}
142
143
 
143
- FakeWeb.register_uri(:head, "http://test.url/test1", :status => ["304", "Not Modified"], :date => "Sat, 03 Jan 2015 07:15:45 GMT", :ETag => "\"validEtag\"")
144
+ FakeWeb.register_uri(:head, "http://test.url/test1", :status => ["304", "Not Modified"], :date => "Sat, 03 Jan 2015 07:15:45 GMT", :ETag => "\"validEtag\"", :Warning => "299 - \"Hello World\"")
144
145
  result = CacheRules.revalidate_response('http://test.url/test1', request, cached)
145
146
 
146
147
  assert_equal result[:code], 304
147
148
  assert_nil result[:body]
148
149
  assert_equal result[:headers]['Cache-Lookup'], 'REVALIDATED'
149
150
  assert_equal result[:headers]['ETag'], "\"validEtag\""
151
+ assert_equal result[:headers]['Warning'], "299 - \"Hello World\""
152
+ assert_equal result[:headers]['Content-Type'], "text/html"
150
153
  end
151
154
 
152
155
  def test_revalidate_response_column2_5xx
@@ -126,10 +126,12 @@ class TestFormatting < MiniTest::Test
126
126
  timestamp1 = CacheRules.httpdate_helper httpdate
127
127
  timestamp2 = CacheRules.httpdate_helper rfc850
128
128
  timestamp3 = CacheRules.httpdate_helper ansi_c
129
+ timestamp4 = CacheRules.httpdate_helper 'invalid!(@*&^#%$'
129
130
 
130
131
  assert_equal timestamp1, result
131
132
  assert_equal timestamp2, result
132
133
  assert_equal timestamp3, result
134
+ assert timestamp4
133
135
  end
134
136
 
135
137
  end
data/test/test_helpers.rb CHANGED
@@ -172,12 +172,20 @@ class TestHelpers < MiniTest::Test
172
172
  assert_nil noop
173
173
  end
174
174
 
175
- def test_headers_304
176
- headers = CacheRules.helper_headers_304.call @cached_headers
175
+ def test_headers_200_304
176
+ headers = CacheRules.helper_headers_200_304.call(@cached_headers, {'Warning' => ['199 - Hello World']})
177
177
 
178
178
  assert_kind_of Hash, headers
179
179
 
180
- assert_equal headers, {"Date"=>"Fri, 02 Jan 2015 11:03:45 GMT", "Cache-Control"=>"public, max-stale=1000, no-cache=\"Cookie\"", "ETag"=>"\"validEtag\""}
180
+ assert_equal headers, {"Date"=>"Fri, 02 Jan 2015 11:03:45 GMT", "Cache-Control"=>"public, max-stale=1000, no-cache=\"Cookie\"", "Last-Modified"=>"Fri, 02 Jan 2015 11:03:45 GMT", "ETag"=>"\"validEtag\""}
181
+ end
182
+
183
+ def test_remove_warning_1xx
184
+ one_result = CacheRules.helper_remove_warning_1xx.call 'Warning', ["199 - \"This is a test\"", "299 - Hello World"]
185
+ nil_result = CacheRules.helper_remove_warning_1xx.call 'Warning', ["199 - \"This is a test\""]
186
+
187
+ assert_equal one_result, {"Warning"=>["299 - Hello World"]}
188
+ assert_nil nil_result
181
189
  end
182
190
 
183
191
  def test_helper_has_star
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.1.4
4
+ version: 0.1.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -86,6 +86,7 @@ extra_rdoc_files: []
86
86
  files:
87
87
  - .gitignore
88
88
  - .travis.yml
89
+ - CHANGELOG.md
89
90
  - Gemfile
90
91
  - Gemfile.lock
91
92
  - LICENSE