diff options
author | Mike Greiling <mike@pixelcog.com> | 2017-10-30 15:06:08 -0500 |
---|---|---|
committer | Mike Greiling <mike@pixelcog.com> | 2017-10-30 15:14:25 -0500 |
commit | 5445f839e8a1f8bb6ff1a3cf68c3b5d59f3681bc (patch) | |
tree | f1b1218c382c1577b0db813d3a6603f507e5b5e8 | |
parent | e0ca65c5144d4f6de91e3c808437bd85c0323b14 (diff) | |
download | gitlab-ce-5445f839e8a1f8bb6ff1a3cf68c3b5d59f3681bc.tar.gz |
allow inspect_request to inject request headers in order to test Sendfile requests in jobs_spec.rb
-rw-r--r-- | lib/gitlab/testing/request_inspector_middleware.rb | 12 | ||||
-rw-r--r-- | spec/features/projects/jobs_spec.rb | 34 | ||||
-rw-r--r-- | spec/support/inspect_requests.rb | 4 |
3 files changed, 31 insertions, 19 deletions
diff --git a/lib/gitlab/testing/request_inspector_middleware.rb b/lib/gitlab/testing/request_inspector_middleware.rb index a5ac800c735..27fff061c7d 100644 --- a/lib/gitlab/testing/request_inspector_middleware.rb +++ b/lib/gitlab/testing/request_inspector_middleware.rb @@ -5,9 +5,11 @@ module Gitlab class RequestInspectorMiddleware @@log_requests = Concurrent::AtomicBoolean.new(false) @@logged_requests = Concurrent::Array.new + @@inject_headers = Concurrent::Hash.new # Resets the current request log and starts logging requests - def self.log_requests! + def self.log_requests!(headers = {}) + @@inject_headers.replace(headers) @@logged_requests.replace([]) @@log_requests.value = true end @@ -29,6 +31,7 @@ module Gitlab return @app.call(env) unless @@log_requests.true? url = env['REQUEST_URI'] + env.merge! http_headers_env(@@inject_headers) if @@inject_headers.any? request_headers = env_http_headers(env) status, headers, body = @app.call(env) @@ -53,6 +56,13 @@ module Gitlab .flatten] end + def http_headers_env(headers) + Hash[*headers + .collect {|k, v| [k.split('-').collect(&:upcase).join('_'), v]} + .collect {|k, v| [k.prepend('HTTP_'), v]} + .flatten] + end + def log_request(response) @@logged_requests.push(response) end diff --git a/spec/features/projects/jobs_spec.rb b/spec/features/projects/jobs_spec.rb index 171b001aa7a..5e1d81184b5 100644 --- a/spec/features/projects/jobs_spec.rb +++ b/spec/features/projects/jobs_spec.rb @@ -2,6 +2,8 @@ require 'spec_helper' require 'tempfile' feature 'Jobs' do + include InspectRequests + let(:user) { create(:user) } let(:user_access_level) { :developer } let(:project) { create(:project, :repository) } @@ -441,27 +443,30 @@ feature 'Jobs' do context 'access source' do context 'job from project' do before do - Capybara.current_session.driver.headers = { 'X-Sendfile-Type' => 'X-Sendfile' } job.run! - visit project_job_path(project, job) - find('.js-raw-link-controller').click() end it 'sends the right headers' do - expect(page.response_headers['Content-Type']).to eq('text/plain; charset=utf-8') - expect(page.response_headers['X-Sendfile']).to eq(job.trace.send(:current_path)) + requests = inspect_requests(inject_headers: { 'X-Sendfile-Type' => 'X-Sendfile' }) do + visit raw_project_job_path(project, job) + end + + expect(requests.first.status_code).to eq(200) + expect(requests.first.response_headers['Content-Type']).to eq('text/plain; charset=utf-8') + expect(requests.first.response_headers['X-Sendfile']).to eq(job.trace.send(:current_path)) end end context 'job from other project' do before do - Capybara.current_session.driver.headers = { 'X-Sendfile-Type' => 'X-Sendfile' } job2.run! - visit raw_project_job_path(project, job2) end it 'sends the right headers' do - expect(page.status_code).to eq(404) + requests = inspect_requests(inject_headers: { 'X-Sendfile-Type' => 'X-Sendfile' }) do + visit raw_project_job_path(project, job2) + end + expect(requests.first.status_code).to eq(404) end end end @@ -470,8 +475,6 @@ feature 'Jobs' do let(:existing_file) { Tempfile.new('existing-trace-file').path } before do - Capybara.current_session.driver.headers = { 'X-Sendfile-Type' => 'X-Sendfile' } - job.run! end @@ -480,15 +483,14 @@ feature 'Jobs' do allow_any_instance_of(Gitlab::Ci::Trace) .to receive(:paths) .and_return([existing_file]) - - visit project_job_path(project, job) - - find('.js-raw-link-controller').click end it 'sends the right headers' do - expect(page.response_headers['Content-Type']).to eq('text/plain; charset=utf-8') - expect(page.response_headers['X-Sendfile']).to eq(existing_file) + requests = inspect_requests(inject_headers: { 'X-Sendfile-Type' => 'X-Sendfile' }) do + visit raw_project_job_path(project, job) + end + expect(requests.first.response_headers['Content-Type']).to eq('text/plain; charset=utf-8') + expect(requests.first.response_headers['X-Sendfile']).to eq(existing_file) end end diff --git a/spec/support/inspect_requests.rb b/spec/support/inspect_requests.rb index 5947700c1ea..b3ddb245062 100644 --- a/spec/support/inspect_requests.rb +++ b/spec/support/inspect_requests.rb @@ -4,8 +4,8 @@ module InspectRequests extend self include WaitForRequests - def inspect_requests - Gitlab::Testing::RequestInspectorMiddleware.log_requests! + def inspect_requests(inject_headers: {}) + Gitlab::Testing::RequestInspectorMiddleware.log_requests!(inject_headers) yield block_and_wait_for_requests_complete Gitlab::Testing::RequestInspectorMiddleware.requests |