Permalink
Browse files

Merge pull request #7649 from steveklabnik/issue_2520

Support for multiple etags in an If-None-Match header
  • Loading branch information...
rafaelfranca committed Sep 15, 2012
2 parents 9cd831a + cd461c3 commit a086ef6f17b5baa15da0234ac682f1d4f1ff4d2a
View
@@ -1,5 +1,7 @@
## Rails 4.0.0 (unreleased) ##
+* Support multiple etags in If-None-Match header. *Travis Warlick*
+
* Allow to configure how unverified request will be handled using `:with`
option in `protect_from_forgery` method.
@@ -17,12 +17,18 @@ def if_none_match
env[HTTP_IF_NONE_MATCH]
end
+ def if_none_match_etags
+ (if_none_match ? if_none_match.split(/\s*,\s*/) : []).collect do |etag|
+ etag.gsub(/^\"|\"$/, "")
+ end
+ end
+
def not_modified?(modified_at)
if_modified_since && modified_at && if_modified_since >= modified_at
end
def etag_matches?(etag)
- if_none_match && if_none_match == etag
+ if_none_match_etags.include?(etag)
end
# Check response freshness (Last-Modified and ETag) against request
@@ -746,6 +746,45 @@ def url_for(options = {})
assert_equal "/foo?bar", path
end
+ test "if_none_match_etags none" do
+ request = stub_request
+
+ assert_equal nil, request.if_none_match
+ assert_equal [], request.if_none_match_etags
+ assert !request.etag_matches?("foo")
+ assert !request.etag_matches?(nil)
+ end
+
+ test "if_none_match_etags single" do
+ header = 'the-etag'
+ request = stub_request('HTTP_IF_NONE_MATCH' => header)
+
+ assert_equal header, request.if_none_match
+ assert_equal [header], request.if_none_match_etags
+ assert request.etag_matches?("the-etag")
+ end
+
+ test "if_none_match_etags quoted single" do
+ header = '"the-etag"'
+ request = stub_request('HTTP_IF_NONE_MATCH' => header)
+
+ assert_equal header, request.if_none_match
+ assert_equal ['the-etag'], request.if_none_match_etags
+ assert request.etag_matches?("the-etag")
+ end
+
+ test "if_none_match_etags multiple" do
+ header = 'etag1, etag2, "third etag", "etag4"'
+ expected = ['etag1', 'etag2', 'third etag', 'etag4']
+ request = stub_request('HTTP_IF_NONE_MATCH' => header)
+
+ assert_equal header, request.if_none_match
+ assert_equal expected, request.if_none_match_etags
+ expected.each do |etag|
+ assert request.etag_matches?(etag), etag
+ end
+ end
+
protected
def stub_request(env = {})

0 comments on commit a086ef6

Please sign in to comment.