/
http_cache_responder_test.rb
104 lines (86 loc) · 3.1 KB
/
http_cache_responder_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
require File.dirname(__FILE__) + '/test_helper'
class HttpCacheResponder < ActionController::Responder
include Responders::HttpCacheResponder
end
class HttpCacheController < ApplicationController
self.responder = HttpCacheResponder
def single
options = params.slice(:http_cache)
response.last_modified = Time.utc(2008) if params[:last_modified]
respond_with(Model.new(Time.utc(2009)), options)
end
def collection
respond_with [Model.new(Time.utc(2009)), Model.new(Time.utc(2008))]
end
def empty
respond_with []
end
end
class HttpCacheResponderTest < ActionController::TestCase
tests HttpCacheController
def setup
@request.accept = "application/xml"
@controller.stubs(:polymorphic_url).returns("/")
end
def test_last_modified_at_is_set_with_single_resource_on_get
get :single
assert_equal Time.utc(2009).httpdate, @response.headers["Last-Modified"]
assert_equal "<xml />", @response.body
assert_equal 200, @response.status
end
def test_returns_not_modified_if_return_is_cache_is_still_valid
@request.env["HTTP_IF_MODIFIED_SINCE"] = Time.utc(2009, 6).httpdate
get :single
assert_equal 304, @response.status
assert_equal " ", @response.body
end
def test_refreshes_last_modified_if_cache_is_expired
@request.env["HTTP_IF_MODIFIED_SINCE"] = Time.utc(2008, 6).httpdate
get :single
assert_equal Time.utc(2009).httpdate, @response.headers["Last-Modified"]
assert_equal "<xml />", @response.body
assert_equal 200, @response.status
end
def test_does_not_set_cache_unless_get_requests
put :single
assert_nil @response.headers["Last-Modified"]
assert_equal 200, @response.status
end
def test_does_not_use_cache_unless_get_requests
@request.env["HTTP_IF_MODIFIED_SINCE"] = Time.utc(2009, 6).httpdate
put :single
assert_equal 200, @response.status
end
def test_does_not_set_cache_if_http_cache_is_false
get :single, :http_cache => false
assert_nil @response.headers["Last-Modified"]
assert_equal 200, @response.status
end
def test_does_not_use_cache_if_http_cache_is_false
@request.env["HTTP_IF_MODIFIED_SINCE"] = Time.utc(2009, 6).httpdate
get :single, :http_cache => false
assert_equal 200, @response.status
end
def test_does_not_set_cache_if_last_modified_already_set_in_response
get :single, :last_modified => true
assert_equal Time.utc(2008).httpdate, @response.headers["Last-Modified"]
assert_equal 200, @response.status
end
def test_does_not_use_cache_if_last_modified_already_set_in_response
@request.env["HTTP_IF_MODIFIED_SINCE"] = Time.utc(2009, 6).httpdate
get :single, :last_modified => true
assert_equal 200, @response.status
end
def test_collection_chooses_the_latest_timestamp
get :collection
assert_equal Time.utc(2009).httpdate, @response.headers["Last-Modified"]
assert_match /xml/, @response.body
assert_equal 200, @response.status
end
def test_work_with_an_empty_array
get :empty
assert_nil @response.headers["Last-Modified"]
assert_match /xml/, @response.body
assert_equal 200, @response.status
end
end