Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test rails ETag caching?

Is it possible to cover my controller, that is highly depeinding on Etags with unit tests?

Here's what i'm trying to do: in case if page is not stale (meaning that it's fresh), i'm adding some header to response.

When i'm trying to test it all (rspec), no matter how many similar requests i have, i still receive 200 OK instead of 304, and my header doesn't get modified. Furthermore, if i track request.fresh?(response), it's ALWAYS false.

However, it perfectly works in browser. I've already tried to state ActionController::Base.perform_caching = true, it doesn't change the overall situation.

Thank you

like image 742
0100110010101 Avatar asked Apr 15 '10 10:04

0100110010101


1 Answers

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
like image 97
szeryf Avatar answered Sep 21 '22 19:09

szeryf