diff options
Diffstat (limited to 'spec/lib/gitlab/etag_caching/middleware_spec.rb')
-rw-r--r-- | spec/lib/gitlab/etag_caching/middleware_spec.rb | 48 |
1 files changed, 45 insertions, 3 deletions
diff --git a/spec/lib/gitlab/etag_caching/middleware_spec.rb b/spec/lib/gitlab/etag_caching/middleware_spec.rb index 361b2329e15..3122a3b1c07 100644 --- a/spec/lib/gitlab/etag_caching/middleware_spec.rb +++ b/spec/lib/gitlab/etag_caching/middleware_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe Gitlab::EtagCaching::Middleware do +RSpec.describe Gitlab::EtagCaching::Middleware, :clean_gitlab_redis_shared_state do let(:app) { double(:app) } let(:middleware) { described_class.new(app) } let(:app_status_code) { 200 } @@ -10,6 +10,17 @@ RSpec.describe Gitlab::EtagCaching::Middleware do let(:enabled_path) { '/gitlab-org/gitlab-foss/noteable/issue/1/notes' } let(:endpoint) { 'issue_notes' } + describe '.skip!' do + it 'sets the skip header on the response' do + rsp = ActionDispatch::Response.new + rsp.set_header('Anything', 'Else') + + described_class.skip!(rsp) + + expect(rsp.headers.to_h).to eq(described_class::SKIP_HEADER_KEY => '1', 'Anything' => 'Else') + end + end + context 'when ETag caching is not enabled for current route' do let(:path) { '/gitlab-org/gitlab-foss/tree/master/noteable/issue/1/notes' } @@ -17,10 +28,12 @@ RSpec.describe Gitlab::EtagCaching::Middleware do mock_app_response end - it 'does not add ETag header' do + it 'does not add ETag headers' do _, headers, _ = middleware.call(build_request(path, if_none_match)) expect(headers['ETag']).to be_nil + expect(headers['X-Gitlab-From-Cache']).to be_nil + expect(headers[::Gitlab::Metrics::RequestsRackMiddleware::FEATURE_CATEGORY_HEADER]).to be_nil end it 'passes status code from app' do @@ -68,13 +81,35 @@ RSpec.describe Gitlab::EtagCaching::Middleware do mock_value_in_store('123') end - it 'returns this value as header' do + it 'returns the correct headers' do _, headers, _ = middleware.call(build_request(path, if_none_match)) expect(headers['ETag']).to eq 'W/"123"' end end + context 'when the matching route requests that the ETag is skipped' do + let(:path) { enabled_path } + let(:app) do + proc do |_env| + response = ActionDispatch::Response.new + + described_class.skip!(response) + + [200, response.headers.to_h, ''] + end + end + + it 'returns the correct headers' do + expect(app).to receive(:call).and_call_original + + _, headers, _ = middleware.call(build_request(path, if_none_match)) + + expect(headers).not_to have_key('ETag') + expect(headers).not_to have_key(described_class::SKIP_HEADER_KEY) + end + end + shared_examples 'sends a process_action.action_controller notification' do |status_code| let(:expected_items) do { @@ -126,6 +161,13 @@ RSpec.describe Gitlab::EtagCaching::Middleware do expect(status).to eq 304 end + it 'sets correct headers' do + _, headers, _ = middleware.call(build_request(path, if_none_match)) + + expect(headers).to include('X-Gitlab-From-Cache' => 'true', + ::Gitlab::Metrics::RequestsRackMiddleware::FEATURE_CATEGORY_HEADER => 'issue_tracking') + end + it_behaves_like 'sends a process_action.action_controller notification', 304 it 'returns empty body' do |