Skip to content

Commit

Permalink
Simplify request cache spec shared examples (mastodon#27673)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjankowski authored and vmstan committed Dec 14, 2023
1 parent 47046ca commit d2855a7
Showing 1 changed file with 32 additions and 76 deletions.
108 changes: 32 additions & 76 deletions spec/requests/cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,24 +119,26 @@ module DisabledAnonymousAPI
end

describe 'Caching behavior' do
shared_examples 'cachable response' do
it 'does not set cookies' do
shared_examples 'cachable response' do |http_success: false|
it 'does not set cookies or set public cache control', :aggregate_failures do
expect(response.cookies).to be_empty
end

it 'sets public cache control', :aggregate_failures do
# expect(response.cache_control[:max_age]&.to_i).to be_positive
expect(response.cache_control[:public]).to be_truthy
expect(response.cache_control[:private]).to be_falsy
expect(response.cache_control[:no_store]).to be_falsy
expect(response.cache_control[:no_cache]).to be_falsy

expect(response).to have_http_status(200) if http_success
end
end

shared_examples 'non-cacheable response' do
shared_examples 'non-cacheable response' do |http_success: false|
it 'sets private cache control' do
expect(response.cache_control[:private]).to be_truthy
expect(response.cache_control[:no_store]).to be_truthy

expect(response).to have_http_status(200) if http_success
end
end

Expand All @@ -149,7 +151,7 @@ module DisabledAnonymousAPI

shared_examples 'language-dependent' do
it 'has a Vary on Accept-Language' do
expect(response.headers['Vary']&.split(',')&.map { |x| x.strip.downcase }).to include('accept-language')
expect(response_vary_headers).to include('accept-language')
end
end

Expand Down Expand Up @@ -202,7 +204,7 @@ module DisabledAnonymousAPI
it_behaves_like 'cachable response'

it 'has a Vary on Cookie' do
expect(response.headers['Vary']&.split(',')&.map { |x| x.strip.downcase }).to include('cookie')
expect(response_vary_headers).to include('cookie')
end

it_behaves_like 'language-dependent' if TestEndpoints::LANGUAGE_DEPENDENT.include?(endpoint)
Expand All @@ -216,7 +218,7 @@ module DisabledAnonymousAPI
it_behaves_like 'cachable response'

it 'has a Vary on Authorization' do
expect(response.headers['Vary']&.split(',')&.map { |x| x.strip.downcase }).to include('authorization')
expect(response_vary_headers).to include('authorization')
end

it_behaves_like 'language-dependent' if TestEndpoints::LANGUAGE_DEPENDENT.include?(endpoint)
Expand Down Expand Up @@ -302,7 +304,7 @@ module DisabledAnonymousAPI
it_behaves_like 'non-cacheable response'

it 'has a Vary on Cookie' do
expect(response.headers['Vary']&.split(',')&.map { |x| x.strip.downcase }).to include('cookie')
expect(response_vary_headers).to include('cookie')
end
end
end
Expand All @@ -311,11 +313,7 @@ module DisabledAnonymousAPI
describe endpoint do
before { get endpoint }

it_behaves_like 'non-cacheable response'

it 'returns HTTP success' do
expect(response).to have_http_status(200)
end
it_behaves_like 'non-cacheable response', http_success: true
end
end

Expand Down Expand Up @@ -351,7 +349,7 @@ module DisabledAnonymousAPI
it_behaves_like 'non-cacheable response'

it 'has a Vary on Authorization' do
expect(response.headers['Vary']&.split(',')&.map { |x| x.strip.downcase }).to include('authorization')
expect(response_vary_headers).to include('authorization')
end
end
end
Expand All @@ -362,11 +360,7 @@ module DisabledAnonymousAPI
get endpoint, headers: { 'Authorization' => "Bearer #{token.token}" }
end

it_behaves_like 'non-cacheable response'

it 'returns HTTP success' do
expect(response).to have_http_status(200)
end
it_behaves_like 'non-cacheable response', http_success: true
end
end

Expand All @@ -393,11 +387,7 @@ module DisabledAnonymousAPI
context 'when allowed for local users only' do
let(:show_domain_blocks) { 'users' }

it_behaves_like 'non-cacheable response'

it 'returns HTTP success' do
expect(response).to have_http_status(200)
end
it_behaves_like 'non-cacheable response', http_success: true
end

context 'when disabled' do
Expand All @@ -421,11 +411,7 @@ module DisabledAnonymousAPI
get '/actor', sign_with: remote_actor, headers: { 'Accept' => 'application/activity+json' }
end

it_behaves_like 'cachable response'

it 'returns HTTP success' do
expect(response).to have_http_status(200)
end
it_behaves_like 'cachable response', http_success: true
end

TestEndpoints::REQUIRE_SIGNATURE.each do |endpoint|
Expand All @@ -434,11 +420,7 @@ module DisabledAnonymousAPI
get endpoint, sign_with: remote_actor, headers: { 'Accept' => 'application/activity+json' }
end

it_behaves_like 'non-cacheable response'

it 'returns HTTP success' do
expect(response).to have_http_status(200)
end
it_behaves_like 'non-cacheable response', http_success: true
end
end
end
Expand All @@ -456,11 +438,7 @@ module DisabledAnonymousAPI
get '/actor', headers: { 'Accept' => 'application/activity+json' }
end

it_behaves_like 'cachable response'

it 'returns HTTP success' do
expect(response).to have_http_status(200)
end
it_behaves_like 'cachable response', http_success: true
end

(TestEndpoints::REQUIRE_SIGNATURE + TestEndpoints::AuthorizedFetch::REQUIRE_SIGNATURE).each do |endpoint|
Expand All @@ -487,11 +465,7 @@ module DisabledAnonymousAPI
get '/actor', sign_with: remote_actor, headers: { 'Accept' => 'application/activity+json' }
end

it_behaves_like 'cachable response'

it 'returns HTTP success' do
expect(response).to have_http_status(200)
end
it_behaves_like 'cachable response', http_success: true
end

(TestEndpoints::REQUIRE_SIGNATURE + TestEndpoints::AuthorizedFetch::REQUIRE_SIGNATURE).each do |endpoint|
Expand All @@ -500,11 +474,7 @@ module DisabledAnonymousAPI
get endpoint, sign_with: remote_actor, headers: { 'Accept' => 'application/activity+json' }
end

it_behaves_like 'non-cacheable response'

it 'returns HTTP success' do
expect(response).to have_http_status(200)
end
it_behaves_like 'non-cacheable response', http_success: true
end
end
end
Expand All @@ -528,11 +498,7 @@ module DisabledAnonymousAPI
get '/actor', headers: { 'Accept' => 'application/activity+json' }
end

it_behaves_like 'cachable response'

it 'returns HTTP success' do
expect(response).to have_http_status(200)
end
it_behaves_like 'cachable response', http_success: true
end

(TestEndpoints::REQUIRE_SIGNATURE + TestEndpoints::AuthorizedFetch::REQUIRE_SIGNATURE).each do |endpoint|
Expand Down Expand Up @@ -560,11 +526,7 @@ module DisabledAnonymousAPI
get '/actor', sign_with: remote_actor, headers: { 'Accept' => 'application/activity+json' }
end

it_behaves_like 'cachable response'

it 'returns HTTP success' do
expect(response).to have_http_status(200)
end
it_behaves_like 'cachable response', http_success: true
end

(TestEndpoints::REQUIRE_SIGNATURE + TestEndpoints::AuthorizedFetch::REQUIRE_SIGNATURE).each do |endpoint|
Expand All @@ -573,11 +535,7 @@ module DisabledAnonymousAPI
get endpoint, sign_with: remote_actor, headers: { 'Accept' => 'application/activity+json' }
end

it_behaves_like 'non-cacheable response'

it 'returns HTTP success' do
expect(response).to have_http_status(200)
end
it_behaves_like 'non-cacheable response', http_success: true
end
end
end
Expand All @@ -591,11 +549,7 @@ module DisabledAnonymousAPI
get '/actor', sign_with: remote_actor, headers: { 'Accept' => 'application/activity+json' }
end

it_behaves_like 'cachable response'

it 'returns HTTP success' do
expect(response).to have_http_status(200)
end
it_behaves_like 'cachable response', http_success: true
end

(TestEndpoints::REQUIRE_SIGNATURE + TestEndpoints::AuthorizedFetch::REQUIRE_SIGNATURE).each do |endpoint|
Expand Down Expand Up @@ -667,7 +621,7 @@ module DisabledAnonymousAPI
it_behaves_like 'non-cacheable response'

it 'has a Vary on Authorization' do
expect(response.headers['Vary']&.split(',')&.map { |x| x.strip.downcase }).to include('authorization')
expect(response_vary_headers).to include('authorization')
end
end
end
Expand All @@ -678,13 +632,15 @@ module DisabledAnonymousAPI
get endpoint, headers: { 'Authorization' => "Bearer #{token.token}" }
end

it_behaves_like 'non-cacheable response'

it 'returns HTTP success' do
expect(response).to have_http_status(200)
end
it_behaves_like 'non-cacheable response', http_success: true
end
end
end
end

private

def response_vary_headers
response.headers['Vary']&.split(',')&.map { |x| x.strip.downcase }
end
end

0 comments on commit d2855a7

Please sign in to comment.