How to test rails ETag caching?
Here's how you can test if second request returns 304 response:
get action, params
assert_response 200, @response.body
etag = @response.headers["ETag"]
@request.env["HTTP_IF_NONE_MATCH"] = etag
get action, params
assert_response 304, @response.body
Rails hashes the :etag you provide:
headers['ETag'] = %("#{Digest::MD5.hexdigest(ActiveSupport::Cache.expand_cache_key(etag))}")
so setting something simple as
frash_when(:etag => 'foo')
would only be triggered by the right digest (the double quotes are necessary)
def with_etag
if stale?(:etag => 'foo')
render :text => 'OK'
end
end
... tested by ...
@request.env['HTTP_IF_NONE_MATCH'] = '"acbd18db4cc2f85cedef654fccc4a4d8"'
get :with_etag
assert_equal 304, @response.status.to_i
same for modified:
def with_modified
if stale?(:last_modified => 1.minute.ago)
render :text => 'OK'
end
end
... tested by ...
@request.env['HTTP_IF_MODIFIED_SINCE'] = 2.minutes.ago.rfc2822
get :with_modified
assert_equal 304, @response.status.to_i
Ok, here's a point:
Before hitting the request, read up everything that's related to ETags in Rails code and Don't forget to set:
request.env["HTTP_IF_MODIFIED_SINCE"]
request.env["HTTP_IF_NONE_MATCH"]
Since they're required for ETag testing.