From 893b2f33fd92a320146e6abf172e21b1b340ae24 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Mon, 16 Oct 2023 11:52:06 -0400 Subject: [PATCH] Extract shared example for cacheable response in specs (#25388) --- spec/controllers/accounts_controller_spec.rb | 25 +++---------------- .../collections_controller_spec.rb | 16 ------------ .../activitypub/outboxes_controller_spec.rb | 16 ------------ .../activitypub/replies_controller_spec.rb | 16 ------------ spec/controllers/statuses_controller_spec.rb | 23 ++--------------- spec/support/examples/cache.rb | 22 ++++++++++++++++ 6 files changed, 27 insertions(+), 91 deletions(-) create mode 100644 spec/support/examples/cache.rb diff --git a/spec/controllers/accounts_controller_spec.rb b/spec/controllers/accounts_controller_spec.rb index 5a8585b069..d46fdb108d 100644 --- a/spec/controllers/accounts_controller_spec.rb +++ b/spec/controllers/accounts_controller_spec.rb @@ -7,25 +7,6 @@ RSpec.describe AccountsController do let(:account) { Fabricate(:account) } - shared_examples 'cacheable response' do - it 'does not set cookies' do - expect(response.cookies).to be_empty - expect(response.headers['Set-Cookies']).to be_nil - end - - it 'does not set sessions' do - expect(session).to be_empty - end - - it 'returns Vary header' do - expect(response.headers['Vary']).to include 'Accept' - end - - it 'returns public Cache-Control header' do - expect(response.headers['Cache-Control']).to include 'public' - end - end - describe 'GET #show' do let(:format) { 'html' } @@ -186,7 +167,7 @@ RSpec.describe AccountsController do expect(response.media_type).to eq 'application/activity+json' end - it_behaves_like 'cacheable response' + it_behaves_like 'cacheable response', expects_vary: 'Accept, Accept-Language, Cookie' it 'renders account' do json = body_as_json @@ -244,7 +225,7 @@ RSpec.describe AccountsController do expect(response.media_type).to eq 'application/activity+json' end - it_behaves_like 'cacheable response' + it_behaves_like 'cacheable response', expects_vary: 'Accept, Accept-Language, Cookie' it 'renders account' do json = body_as_json @@ -311,7 +292,7 @@ RSpec.describe AccountsController do expect(response).to have_http_status(200) end - it_behaves_like 'cacheable response' + it_behaves_like 'cacheable response', expects_vary: 'Accept, Accept-Language, Cookie' end context 'with a normal account in an RSS request' do diff --git a/spec/controllers/activitypub/collections_controller_spec.rb b/spec/controllers/activitypub/collections_controller_spec.rb index e2802cf565..cf484ff5a4 100644 --- a/spec/controllers/activitypub/collections_controller_spec.rb +++ b/spec/controllers/activitypub/collections_controller_spec.rb @@ -7,22 +7,6 @@ RSpec.describe ActivityPub::CollectionsController do let!(:private_pinned) { Fabricate(:status, account: account, text: 'secret private stuff', visibility: :private) } let(:remote_account) { nil } - shared_examples 'cacheable response' do - it 'does not set cookies' do - expect(response.cookies).to be_empty - expect(response.headers['Set-Cookies']).to be_nil - end - - it 'does not set sessions' do - response - expect(session).to be_empty - end - - it 'returns public Cache-Control header' do - expect(response.headers['Cache-Control']).to include 'public' - end - end - before do allow(controller).to receive(:signed_request_actor).and_return(remote_account) diff --git a/spec/controllers/activitypub/outboxes_controller_spec.rb b/spec/controllers/activitypub/outboxes_controller_spec.rb index 6946fdfcff..53c4f0c09c 100644 --- a/spec/controllers/activitypub/outboxes_controller_spec.rb +++ b/spec/controllers/activitypub/outboxes_controller_spec.rb @@ -5,22 +5,6 @@ require 'rails_helper' RSpec.describe ActivityPub::OutboxesController do let!(:account) { Fabricate(:account) } - shared_examples 'cacheable response' do - it 'does not set cookies' do - expect(response.cookies).to be_empty - expect(response.headers['Set-Cookies']).to be_nil - end - - it 'does not set sessions' do - response - expect(session).to be_empty - end - - it 'returns public Cache-Control header' do - expect(response.headers['Cache-Control']).to include 'public' - end - end - before do Fabricate(:status, account: account, visibility: :public) Fabricate(:status, account: account, visibility: :unlisted) diff --git a/spec/controllers/activitypub/replies_controller_spec.rb b/spec/controllers/activitypub/replies_controller_spec.rb index c7b65f004d..7e6e0ffb0d 100644 --- a/spec/controllers/activitypub/replies_controller_spec.rb +++ b/spec/controllers/activitypub/replies_controller_spec.rb @@ -8,22 +8,6 @@ RSpec.describe ActivityPub::RepliesController do let(:remote_reply_id) { 'https://foobar.com/statuses/1234' } let(:remote_querier) { nil } - shared_examples 'cacheable response' do - it 'does not set cookies' do - expect(response.cookies).to be_empty - expect(response.headers['Set-Cookies']).to be_nil - end - - it 'does not set sessions' do - response - expect(session).to be_empty - end - - it 'returns public Cache-Control header' do - expect(response.headers['Cache-Control']).to include 'public' - end - end - shared_examples 'common behavior' do context 'when status is private' do let(:parent_visibility) { :private } diff --git a/spec/controllers/statuses_controller_spec.rb b/spec/controllers/statuses_controller_spec.rb index bd98929c02..8b715824b8 100644 --- a/spec/controllers/statuses_controller_spec.rb +++ b/spec/controllers/statuses_controller_spec.rb @@ -5,25 +5,6 @@ require 'rails_helper' describe StatusesController do render_views - shared_examples 'cacheable response' do - it 'does not set cookies' do - expect(response.cookies).to be_empty - expect(response.headers['Set-Cookies']).to be_nil - end - - it 'does not set sessions' do - expect(session).to be_empty - end - - it 'returns Vary header' do - expect(response.headers['Vary']).to include 'Accept, Accept-Language, Cookie' - end - - it 'returns public Cache-Control header' do - expect(response.headers['Cache-Control']).to include 'public' - end - end - describe 'GET #show' do let(:account) { Fabricate(:account) } let(:status) { Fabricate(:status, account: account) } @@ -88,7 +69,7 @@ describe StatusesController do context 'with JSON' do let(:format) { 'json' } - it_behaves_like 'cacheable response' + it_behaves_like 'cacheable response', expects_vary: 'Accept, Accept-Language, Cookie' it 'renders ActivityPub Note object successfully', :aggregate_failures do expect(response).to have_http_status(200) @@ -371,7 +352,7 @@ describe StatusesController do context 'with JSON' do let(:format) { 'json' } - it_behaves_like 'cacheable response' + it_behaves_like 'cacheable response', expects_vary: 'Accept, Accept-Language, Cookie' it 'renders ActivityPub Note object successfully', :aggregate_failures do expect(response).to have_http_status(200) diff --git a/spec/support/examples/cache.rb b/spec/support/examples/cache.rb new file mode 100644 index 0000000000..43cfbade82 --- /dev/null +++ b/spec/support/examples/cache.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +shared_examples 'cacheable response' do |expects_vary: false| + it 'does not set cookies' do + expect(response.cookies).to be_empty + expect(response.headers['Set-Cookies']).to be_nil + end + + it 'does not set sessions' do + expect(session).to be_empty + end + + if expects_vary + it 'returns Vary header' do + expect(response.headers['Vary']).to include(expects_vary) + end + end + + it 'returns public Cache-Control header' do + expect(response.headers['Cache-Control']).to include('public') + end +end