diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-11-18 16:16:36 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-11-18 16:16:36 +0300 |
commit | 311b0269b4eb9839fa63f80c8d7a58f32b8138a0 (patch) | |
tree | 07e7870bca8aed6d61fdcc810731c50d2c40af47 /spec/lib/gitlab/middleware/compressed_json_spec.rb | |
parent | 27909cef6c4170ed9205afa7426b8d3de47cbb0c (diff) |
Add latest changes from gitlab-org/gitlab@14-5-stable-eev14.5.0-rc42
Diffstat (limited to 'spec/lib/gitlab/middleware/compressed_json_spec.rb')
-rw-r--r-- | spec/lib/gitlab/middleware/compressed_json_spec.rb | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/spec/lib/gitlab/middleware/compressed_json_spec.rb b/spec/lib/gitlab/middleware/compressed_json_spec.rb new file mode 100644 index 00000000000..c5efc568971 --- /dev/null +++ b/spec/lib/gitlab/middleware/compressed_json_spec.rb @@ -0,0 +1,75 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Middleware::CompressedJson do + let_it_be(:decompressed_input) { '{"foo": "bar"}' } + let_it_be(:input) { ActiveSupport::Gzip.compress(decompressed_input) } + + let(:app) { double(:app) } + let(:middleware) { described_class.new(app) } + let(:env) do + { + 'HTTP_CONTENT_ENCODING' => 'gzip', + 'REQUEST_METHOD' => 'POST', + 'CONTENT_TYPE' => 'application/json', + 'PATH_INFO' => path, + 'rack.input' => StringIO.new(input) + } + end + + shared_examples 'decompress middleware' do + it 'replaces input with a decompressed content' do + expect(app).to receive(:call) + + middleware.call(env) + + expect(env['rack.input'].read).to eq(decompressed_input) + expect(env['CONTENT_LENGTH']).to eq(decompressed_input.length) + expect(env['HTTP_CONTENT_ENCODING']).to be_nil + end + end + + describe '#call' do + context 'with collector route' do + let(:path) { '/api/v4/error_tracking/collector/1/store'} + + it_behaves_like 'decompress middleware' + end + + context 'with collector route under relative url' do + let(:path) { '/gitlab/api/v4/error_tracking/collector/1/store'} + + before do + stub_config_setting(relative_url_root: '/gitlab') + end + + it_behaves_like 'decompress middleware' + end + + context 'with some other route' do + let(:path) { '/api/projects/123' } + + it 'keeps the original input' do + expect(app).to receive(:call) + + middleware.call(env) + + expect(env['rack.input'].read).to eq(input) + expect(env['HTTP_CONTENT_ENCODING']).to eq('gzip') + end + end + + context 'payload is too large' do + let(:body_limit) { Gitlab::Middleware::CompressedJson::MAXIMUM_BODY_SIZE } + let(:decompressed_input) { 'a' * (body_limit + 100) } + let(:input) { ActiveSupport::Gzip.compress(decompressed_input) } + let(:path) { '/api/v4/error_tracking/collector/1/envelope'} + + it 'reads only limited size' do + expect(middleware.call(env)) + .to eq([413, { 'Content-Type' => 'text/plain' }, ['Payload Too Large']]) + end + end + end +end |