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
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
......
......@@ -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
......
......@@ -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
......
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