http_caching_test.rb
3.15 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
require_relative "../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)
assert_no_cookies
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
test 'private community profile should not return cache headers' do
create_private_community('the-community')
get "/profile/the-community"
assert_response 403
assert_no_cache
end
test 'private community content should not return cache headers' do
community = create_private_community('the-community')
create(Article, profile_id: community.id, name: 'Test page', published: false)
get "/the-community/test-page"
assert_response 403
assert_no_cache
end
test 'user data, not logged in' do
get '/account/user_data', {}, { 'X-Requested-With' => 'XMLHttpRequest'}
assert_no_cookies
end
protected
def set_env_config(data)
env = Environment.default
data.each do |key, value|
env.send("#{key}=", value)
end
env.save!
end
def create_private_community(identifier)
community = fast_create(Community, identifier: identifier)
community.public_profile = false
community.save!
community
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