diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-05-19 18:44:42 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-05-19 18:44:42 +0300 |
commit | 4555e1b21c365ed8303ffb7a3325d773c9b8bf31 (patch) | |
tree | 5423a1c7516cffe36384133ade12572cf709398d /spec/lib/gitlab/middleware | |
parent | e570267f2f6b326480d284e0164a6464ba4081bc (diff) |
Add latest changes from gitlab-org/gitlab@13-12-stable-eev13.12.0-rc42
Diffstat (limited to 'spec/lib/gitlab/middleware')
-rw-r--r-- | spec/lib/gitlab/middleware/rack_multipart_tempfile_factory_spec.rb | 42 | ||||
-rw-r--r-- | spec/lib/gitlab/middleware/speedscope_spec.rb | 61 |
2 files changed, 70 insertions, 33 deletions
diff --git a/spec/lib/gitlab/middleware/rack_multipart_tempfile_factory_spec.rb b/spec/lib/gitlab/middleware/rack_multipart_tempfile_factory_spec.rb index b9d00b556c5..b868207e67c 100644 --- a/spec/lib/gitlab/middleware/rack_multipart_tempfile_factory_spec.rb +++ b/spec/lib/gitlab/middleware/rack_multipart_tempfile_factory_spec.rb @@ -42,44 +42,20 @@ RSpec.describe Gitlab::Middleware::RackMultipartTempfileFactory do context 'for a multipart request' do let(:env) { Rack::MockRequest.env_for('/', multipart_fixture) } - context 'when the environment variable is enabled' do - before do - stub_env('GITLAB_TEMPFILE_IMMEDIATE_UNLINK', '1') - end - - it 'immediately unlinks the temporary file' do - tempfile = Tempfile.new('foo') - - expect(tempfile.path).not_to be(nil) - expect(Rack::Multipart::Parser::TEMPFILE_FACTORY).to receive(:call).and_return(tempfile) - expect(tempfile).to receive(:unlink).and_call_original + it 'immediately unlinks the temporary file' do + tempfile = Tempfile.new('foo') - subject.call(env) + expect(tempfile.path).not_to be(nil) + expect(Rack::Multipart::Parser::TEMPFILE_FACTORY).to receive(:call).and_return(tempfile) + expect(tempfile).to receive(:unlink).and_call_original - expect(tempfile.path).to be(nil) - end + subject.call(env) - it 'processes the request as normal' do - expect(subject.call(env)).to eq([200, { 'Content-Type' => 'image/jpeg' }, [file_contents]]) - end + expect(tempfile.path).to be(nil) end - context 'when the environment variable is disabled' do - it 'does not immediately unlink the temporary file' do - tempfile = Tempfile.new('foo') - - expect(tempfile.path).not_to be(nil) - expect(Rack::Multipart::Parser::TEMPFILE_FACTORY).to receive(:call).and_return(tempfile) - expect(tempfile).not_to receive(:unlink).and_call_original - - subject.call(env) - - expect(tempfile.path).not_to be(nil) - end - - it 'processes the request as normal' do - expect(subject.call(env)).to eq([200, { 'Content-Type' => 'image/jpeg' }, [file_contents]]) - end + it 'processes the request as normal' do + expect(subject.call(env)).to eq([200, { 'Content-Type' => 'image/jpeg' }, [file_contents]]) end end diff --git a/spec/lib/gitlab/middleware/speedscope_spec.rb b/spec/lib/gitlab/middleware/speedscope_spec.rb new file mode 100644 index 00000000000..bb830a2fbda --- /dev/null +++ b/spec/lib/gitlab/middleware/speedscope_spec.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'stackprof' + +RSpec.describe Gitlab::Middleware::Speedscope do + let(:app) { proc { |env| [200, { 'Content-Type' => 'text/plain' }, ['Hello world!']] } } + let(:middleware) { described_class.new(app) } + + describe '#call' do + shared_examples 'returns original response' do + it 'returns original response' do + expect(StackProf).not_to receive(:run) + + status, headers, body = middleware.call(env) + + expect(status).to eq(200) + expect(headers).to eq({ 'Content-Type' => 'text/plain' }) + expect(body.first).to eq('Hello world!') + end + end + + context 'when flamegraph is not requested' do + let(:env) { Rack::MockRequest.env_for('/') } + + it_behaves_like 'returns original response' + end + + context 'when flamegraph requested' do + let(:env) { Rack::MockRequest.env_for('/', params: { 'performance_bar' => 'flamegraph' }) } + + before do + allow(env).to receive(:[]).and_call_original + end + + context 'when user is not allowed' do + before do + allow(env).to receive(:[]).with('warden').and_return(double('Warden', user: create(:user))) + end + + it_behaves_like 'returns original response' + end + + context 'when user is allowed' do + before do + allow(env).to receive(:[]).with('warden').and_return(double('Warden', user: create(:admin))) + end + + it 'runs StackProf and returns a flamegraph' do + expect(StackProf).to receive(:run).and_call_original + + status, headers, body = middleware.call(env) + + expect(status).to eq(200) + expect(headers).to eq({ 'Content-Type' => 'text/html' }) + expect(body.first).to include('speedscope-iframe') + end + end + end + end +end |