http_caching_test.rb
2.31 KB
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
105
106
107
108
109
110
111
112
require 'test_helper'
class HttpCachingTest < ActionController::IntegrationTest
def setup
create_user('joao', password: 'test', password_confirmation: 'test').activate
end
test 'home page, default configuration' do
get '/'
assert_cache(5.minutes)
end
test 'home page, custom config' do
set_env_config(home_cache_in_minutes: 10)
get '/'
assert_cache(10.minutes)
end
test 'search results, default config' do
get '/search', query: 'anything'
assert_cache(15.minutes)
end
test 'search results, custom config' do
set_env_config(general_cache_in_minutes: 30)
get '/search', query: 'anything'
assert_cache(30.minutes)
end
test 'profile page, default config' do
get "/profile/joao"
assert_cache(15.minutes)
end
test 'profile page, custom config' do
set_env_config(profile_cache_in_minutes: 60)
get "/profile/joao"
assert_cache(60.minutes)
end
test 'account controller' do
get '/account/login'
assert_no_cache
end
test 'profile admin' do
login 'joao', 'test'
get "/myprofile/joao"
assert_no_cache
end
test 'environment admin' do
Environment.default.add_admin(Profile['joao'])
get '/admin'
assert_no_cache
end
test 'logged in, home page' do
login 'joao', 'test'
get '/'
assert_no_cache
end
test 'logged in, profile home' do
login 'joao', 'test'
get '/joao'
assert_no_cache
end
test 'logged in, profile page' do
login 'joao', 'test'
get '/profile/joao'
assert_no_cache
end
protected
def set_env_config(data)
env = Environment.default
data.each do |key, value|
env.send("#{key}=", value)
end
env.save!
end
def assert_no_cache
assert(cache_parts == ['max-age=0', 'must-revalidate', 'private'] || cache_parts == ['no-cache'], "should not set cache headers (found #{cache_parts.inspect})")
end
def assert_public_cache
assert_includes cache_parts, 'public'
end
def cache_parts
@cache_parts ||= response.headers['Cache-Control'].split(/\s*,\s*/).sort
end
def assert_cache(valid_for)
assert_includes cache_parts, "max-age=#{valid_for}"
end
def assert_no_cookies
assert_equal({}, response.cookies.to_hash)
end
def assert_cookie(cookie_name)
assert_includes response.cookies.keys, cookie_name
end
end