Commit 823b17cf authored by Kamil Trzciński's avatar Kamil Trzciński

Merge branch 'zj-raise-etag-route-regex-miss' into 'master'

Raise etag route regex miss

Closes #33106

See merge request !12084
parents 79dc74b0 cd1a8542
...@@ -209,7 +209,8 @@ class Environment < ActiveRecord::Base ...@@ -209,7 +209,8 @@ class Environment < ActiveRecord::Base
def etag_cache_key def etag_cache_key
Gitlab::Routing.url_helpers.namespace_project_environments_path( Gitlab::Routing.url_helpers.namespace_project_environments_path(
project.namespace, project.namespace,
project) project,
format: :json)
end end
private private
......
---
title: Fix etag route not being a match for environments
merge_request:
author:
...@@ -7,7 +7,7 @@ module Gitlab ...@@ -7,7 +7,7 @@ module Gitlab
def call(env) def call(env)
request = Rack::Request.new(env) request = Rack::Request.new(env)
route = Gitlab::EtagCaching::Router.match(request) route = Gitlab::EtagCaching::Router.match(request.path_info)
return @app.call(env) unless route return @app.call(env) unless route
track_event(:etag_caching_middleware_used, route) track_event(:etag_caching_middleware_used, route)
......
...@@ -53,8 +53,8 @@ module Gitlab ...@@ -53,8 +53,8 @@ module Gitlab
) )
].freeze ].freeze
def self.match(request) def self.match(path)
ROUTES.find { |route| route.regexp.match(request.path_info) } ROUTES.find { |route| route.regexp.match(path) }
end end
end end
end end
......
...@@ -25,6 +25,8 @@ module Gitlab ...@@ -25,6 +25,8 @@ module Gitlab
end end
def redis_key(key) def redis_key(key)
raise 'Invalid key' if !Rails.env.production? && !Gitlab::EtagCaching::Router.match(key)
"#{REDIS_NAMESPACE}#{key}" "#{REDIS_NAMESPACE}#{key}"
end end
end end
......
...@@ -15,13 +15,13 @@ describe Gitlab::EtagCaching::Middleware do ...@@ -15,13 +15,13 @@ describe Gitlab::EtagCaching::Middleware do
end end
it 'does not add ETag header' do it 'does not add ETag header' do
_, headers, _ = middleware.call(build_env(path, if_none_match)) _, headers, _ = middleware.call(build_request(path, if_none_match))
expect(headers['ETag']).to be_nil expect(headers['ETag']).to be_nil
end end
it 'passes status code from app' do it 'passes status code from app' do
status, _, _ = middleware.call(build_env(path, if_none_match)) status, _, _ = middleware.call(build_request(path, if_none_match))
expect(status).to eq app_status_code expect(status).to eq app_status_code
end end
...@@ -39,7 +39,7 @@ describe Gitlab::EtagCaching::Middleware do ...@@ -39,7 +39,7 @@ describe Gitlab::EtagCaching::Middleware do
expect_any_instance_of(Gitlab::EtagCaching::Store) expect_any_instance_of(Gitlab::EtagCaching::Store)
.to receive(:touch).and_return('123') .to receive(:touch).and_return('123')
middleware.call(build_env(path, if_none_match)) middleware.call(build_request(path, if_none_match))
end end
context 'when If-None-Match header was specified' do context 'when If-None-Match header was specified' do
...@@ -51,7 +51,7 @@ describe Gitlab::EtagCaching::Middleware do ...@@ -51,7 +51,7 @@ describe Gitlab::EtagCaching::Middleware do
expect(Gitlab::Metrics).to receive(:add_event) expect(Gitlab::Metrics).to receive(:add_event)
.with(:etag_caching_key_not_found, endpoint: 'issue_notes') .with(:etag_caching_key_not_found, endpoint: 'issue_notes')
middleware.call(build_env(path, if_none_match)) middleware.call(build_request(path, if_none_match))
end end
end end
end end
...@@ -65,7 +65,7 @@ describe Gitlab::EtagCaching::Middleware do ...@@ -65,7 +65,7 @@ describe Gitlab::EtagCaching::Middleware do
end end
it 'returns this value as header' do it 'returns this value as header' do
_, headers, _ = middleware.call(build_env(path, if_none_match)) _, headers, _ = middleware.call(build_request(path, if_none_match))
expect(headers['ETag']).to eq 'W/"123"' expect(headers['ETag']).to eq 'W/"123"'
end end
...@@ -82,17 +82,17 @@ describe Gitlab::EtagCaching::Middleware do ...@@ -82,17 +82,17 @@ describe Gitlab::EtagCaching::Middleware do
it 'does not call app' do it 'does not call app' do
expect(app).not_to receive(:call) expect(app).not_to receive(:call)
middleware.call(build_env(path, if_none_match)) middleware.call(build_request(path, if_none_match))
end end
it 'returns status code 304' do it 'returns status code 304' do
status, _, _ = middleware.call(build_env(path, if_none_match)) status, _, _ = middleware.call(build_request(path, if_none_match))
expect(status).to eq 304 expect(status).to eq 304
end end
it 'returns empty body' do it 'returns empty body' do
_, _, body = middleware.call(build_env(path, if_none_match)) _, _, body = middleware.call(build_request(path, if_none_match))
expect(body).to be_empty expect(body).to be_empty
end end
...@@ -103,7 +103,7 @@ describe Gitlab::EtagCaching::Middleware do ...@@ -103,7 +103,7 @@ describe Gitlab::EtagCaching::Middleware do
expect(Gitlab::Metrics).to receive(:add_event) expect(Gitlab::Metrics).to receive(:add_event)
.with(:etag_caching_cache_hit, endpoint: 'issue_notes') .with(:etag_caching_cache_hit, endpoint: 'issue_notes')
middleware.call(build_env(path, if_none_match)) middleware.call(build_request(path, if_none_match))
end end
context 'when polling is disabled' do context 'when polling is disabled' do
...@@ -113,7 +113,7 @@ describe Gitlab::EtagCaching::Middleware do ...@@ -113,7 +113,7 @@ describe Gitlab::EtagCaching::Middleware do
end end
it 'returns status code 429' do it 'returns status code 429' do
status, _, _ = middleware.call(build_env(path, if_none_match)) status, _, _ = middleware.call(build_request(path, if_none_match))
expect(status).to eq 429 expect(status).to eq 429
end end
...@@ -131,7 +131,7 @@ describe Gitlab::EtagCaching::Middleware do ...@@ -131,7 +131,7 @@ describe Gitlab::EtagCaching::Middleware do
it 'calls app' do it 'calls app' do
expect(app).to receive(:call).and_return([app_status_code, {}, ['body']]) expect(app).to receive(:call).and_return([app_status_code, {}, ['body']])
middleware.call(build_env(path, if_none_match)) middleware.call(build_request(path, if_none_match))
end end
it 'tracks "etag_caching_resource_changed" event' do it 'tracks "etag_caching_resource_changed" event' do
...@@ -142,7 +142,7 @@ describe Gitlab::EtagCaching::Middleware do ...@@ -142,7 +142,7 @@ describe Gitlab::EtagCaching::Middleware do
expect(Gitlab::Metrics).to receive(:add_event) expect(Gitlab::Metrics).to receive(:add_event)
.with(:etag_caching_resource_changed, endpoint: 'issue_notes') .with(:etag_caching_resource_changed, endpoint: 'issue_notes')
middleware.call(build_env(path, if_none_match)) middleware.call(build_request(path, if_none_match))
end end
end end
...@@ -160,7 +160,7 @@ describe Gitlab::EtagCaching::Middleware do ...@@ -160,7 +160,7 @@ describe Gitlab::EtagCaching::Middleware do
expect(Gitlab::Metrics).to receive(:add_event) expect(Gitlab::Metrics).to receive(:add_event)
.with(:etag_caching_header_missing, endpoint: 'issue_notes') .with(:etag_caching_header_missing, endpoint: 'issue_notes')
middleware.call(build_env(path, if_none_match)) middleware.call(build_request(path, if_none_match))
end end
end end
...@@ -192,10 +192,7 @@ describe Gitlab::EtagCaching::Middleware do ...@@ -192,10 +192,7 @@ describe Gitlab::EtagCaching::Middleware do
.to receive(:get).and_return(value) .to receive(:get).and_return(value)
end end
def build_env(path, if_none_match) def build_request(path, if_none_match)
{ { 'PATH_INFO' => path, 'HTTP_IF_NONE_MATCH' => if_none_match }
'PATH_INFO' => path,
'HTTP_IF_NONE_MATCH' => if_none_match
}
end end
end end
...@@ -2,115 +2,91 @@ require 'spec_helper' ...@@ -2,115 +2,91 @@ require 'spec_helper'
describe Gitlab::EtagCaching::Router do describe Gitlab::EtagCaching::Router do
it 'matches issue notes endpoint' do it 'matches issue notes endpoint' do
request = build_request( result = described_class.match(
'/my-group/and-subgroup/here-comes-the-project/noteable/issue/1/notes' '/my-group/and-subgroup/here-comes-the-project/noteable/issue/1/notes'
) )
result = described_class.match(request)
expect(result).to be_present expect(result).to be_present
expect(result.name).to eq 'issue_notes' expect(result.name).to eq 'issue_notes'
end end
it 'matches issue title endpoint' do it 'matches issue title endpoint' do
request = build_request( result = described_class.match(
'/my-group/my-project/issues/123/realtime_changes' '/my-group/my-project/issues/123/realtime_changes'
) )
result = described_class.match(request)
expect(result).to be_present expect(result).to be_present
expect(result.name).to eq 'issue_title' expect(result.name).to eq 'issue_title'
end end
it 'matches project pipelines endpoint' do it 'matches project pipelines endpoint' do
request = build_request( result = described_class.match(
'/my-group/my-project/pipelines.json' '/my-group/my-project/pipelines.json'
) )
result = described_class.match(request)
expect(result).to be_present expect(result).to be_present
expect(result.name).to eq 'project_pipelines' expect(result.name).to eq 'project_pipelines'
end end
it 'matches commit pipelines endpoint' do it 'matches commit pipelines endpoint' do
request = build_request( result = described_class.match(
'/my-group/my-project/commit/aa8260d253a53f73f6c26c734c72fdd600f6e6d4/pipelines.json' '/my-group/my-project/commit/aa8260d253a53f73f6c26c734c72fdd600f6e6d4/pipelines.json'
) )
result = described_class.match(request)
expect(result).to be_present expect(result).to be_present
expect(result.name).to eq 'commit_pipelines' expect(result.name).to eq 'commit_pipelines'
end end
it 'matches new merge request pipelines endpoint' do it 'matches new merge request pipelines endpoint' do
request = build_request( result = described_class.match(
'/my-group/my-project/merge_requests/new.json' '/my-group/my-project/merge_requests/new.json'
) )
result = described_class.match(request)
expect(result).to be_present expect(result).to be_present
expect(result.name).to eq 'new_merge_request_pipelines' expect(result.name).to eq 'new_merge_request_pipelines'
end end
it 'matches merge request pipelines endpoint' do it 'matches merge request pipelines endpoint' do
request = build_request( result = described_class.match(
'/my-group/my-project/merge_requests/234/pipelines.json' '/my-group/my-project/merge_requests/234/pipelines.json'
) )
result = described_class.match(request)
expect(result).to be_present expect(result).to be_present
expect(result.name).to eq 'merge_request_pipelines' expect(result.name).to eq 'merge_request_pipelines'
end end
it 'matches build endpoint' do it 'matches build endpoint' do
request = build_request( result = described_class.match(
'/my-group/my-project/builds/234.json' '/my-group/my-project/builds/234.json'
) )
result = described_class.match(request)
expect(result).to be_present expect(result).to be_present
expect(result.name).to eq 'project_build' expect(result.name).to eq 'project_build'
end end
it 'does not match blob with confusing name' do it 'does not match blob with confusing name' do
request = build_request( result = described_class.match(
'/my-group/my-project/blob/master/pipelines.json' '/my-group/my-project/blob/master/pipelines.json'
) )
result = described_class.match(request)
expect(result).to be_blank expect(result).to be_blank
end end
it 'matches the environments path' do it 'matches the environments path' do
request = build_request( result = described_class.match(
'/my-group/my-project/environments.json' '/my-group/my-project/environments.json'
) )
result = described_class.match(request)
expect(result).to be_present expect(result).to be_present
expect(result.name).to eq 'environments' expect(result.name).to eq 'environments'
end end
it 'matches pipeline#show endpoint' do it 'matches pipeline#show endpoint' do
request = build_request( result = described_class.match(
'/my-group/my-project/pipelines/2.json' '/my-group/my-project/pipelines/2.json'
) )
result = described_class.match(request)
expect(result).to be_present expect(result).to be_present
expect(result.name).to eq 'project_pipeline' expect(result.name).to eq 'project_pipeline'
end end
def build_request(path)
double(path_info: path)
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment