Commit 5445f839 authored by Mike Greiling's avatar Mike Greiling

allow inspect_request to inject request headers in order to test Sendfile requests in jobs_spec.rb

parent e0ca65c5
...@@ -5,9 +5,11 @@ module Gitlab ...@@ -5,9 +5,11 @@ module Gitlab
class RequestInspectorMiddleware class RequestInspectorMiddleware
@@log_requests = Concurrent::AtomicBoolean.new(false) @@log_requests = Concurrent::AtomicBoolean.new(false)
@@logged_requests = Concurrent::Array.new @@logged_requests = Concurrent::Array.new
@@inject_headers = Concurrent::Hash.new
# Resets the current request log and starts logging requests # 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([]) @@logged_requests.replace([])
@@log_requests.value = true @@log_requests.value = true
end end
...@@ -29,6 +31,7 @@ module Gitlab ...@@ -29,6 +31,7 @@ module Gitlab
return @app.call(env) unless @@log_requests.true? return @app.call(env) unless @@log_requests.true?
url = env['REQUEST_URI'] url = env['REQUEST_URI']
env.merge! http_headers_env(@@inject_headers) if @@inject_headers.any?
request_headers = env_http_headers(env) request_headers = env_http_headers(env)
status, headers, body = @app.call(env) status, headers, body = @app.call(env)
...@@ -53,6 +56,13 @@ module Gitlab ...@@ -53,6 +56,13 @@ module Gitlab
.flatten] .flatten]
end 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) def log_request(response)
@@logged_requests.push(response) @@logged_requests.push(response)
end end
......
...@@ -2,6 +2,8 @@ require 'spec_helper' ...@@ -2,6 +2,8 @@ require 'spec_helper'
require 'tempfile' require 'tempfile'
feature 'Jobs' do feature 'Jobs' do
include InspectRequests
let(:user) { create(:user) } let(:user) { create(:user) }
let(:user_access_level) { :developer } let(:user_access_level) { :developer }
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
...@@ -441,27 +443,30 @@ feature 'Jobs' do ...@@ -441,27 +443,30 @@ feature 'Jobs' do
context 'access source' do context 'access source' do
context 'job from project' do context 'job from project' do
before do before do
Capybara.current_session.driver.headers = { 'X-Sendfile-Type' => 'X-Sendfile' }
job.run! job.run!
visit project_job_path(project, job)
find('.js-raw-link-controller').click()
end end
it 'sends the right headers' do it 'sends the right headers' do
expect(page.response_headers['Content-Type']).to eq('text/plain; charset=utf-8') requests = inspect_requests(inject_headers: { 'X-Sendfile-Type' => 'X-Sendfile' }) do
expect(page.response_headers['X-Sendfile']).to eq(job.trace.send(:current_path)) 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
end end
context 'job from other project' do context 'job from other project' do
before do before do
Capybara.current_session.driver.headers = { 'X-Sendfile-Type' => 'X-Sendfile' }
job2.run! job2.run!
visit raw_project_job_path(project, job2)
end end
it 'sends the right headers' do 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 end
end end
...@@ -470,8 +475,6 @@ feature 'Jobs' do ...@@ -470,8 +475,6 @@ feature 'Jobs' do
let(:existing_file) { Tempfile.new('existing-trace-file').path } let(:existing_file) { Tempfile.new('existing-trace-file').path }
before do before do
Capybara.current_session.driver.headers = { 'X-Sendfile-Type' => 'X-Sendfile' }
job.run! job.run!
end end
...@@ -480,15 +483,14 @@ feature 'Jobs' do ...@@ -480,15 +483,14 @@ feature 'Jobs' do
allow_any_instance_of(Gitlab::Ci::Trace) allow_any_instance_of(Gitlab::Ci::Trace)
.to receive(:paths) .to receive(:paths)
.and_return([existing_file]) .and_return([existing_file])
visit project_job_path(project, job)
find('.js-raw-link-controller').click
end end
it 'sends the right headers' do it 'sends the right headers' do
expect(page.response_headers['Content-Type']).to eq('text/plain; charset=utf-8') requests = inspect_requests(inject_headers: { 'X-Sendfile-Type' => 'X-Sendfile' }) do
expect(page.response_headers['X-Sendfile']).to eq(existing_file) 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
end end
......
...@@ -4,8 +4,8 @@ module InspectRequests ...@@ -4,8 +4,8 @@ module InspectRequests
extend self extend self
include WaitForRequests include WaitForRequests
def inspect_requests def inspect_requests(inject_headers: {})
Gitlab::Testing::RequestInspectorMiddleware.log_requests! Gitlab::Testing::RequestInspectorMiddleware.log_requests!(inject_headers)
yield yield
block_and_wait_for_requests_complete block_and_wait_for_requests_complete
Gitlab::Testing::RequestInspectorMiddleware.requests Gitlab::Testing::RequestInspectorMiddleware.requests
......
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