diff options
Diffstat (limited to 'spec/requests/api/api_guard/response_coercer_middleware_spec.rb')
-rw-r--r-- | spec/requests/api/api_guard/response_coercer_middleware_spec.rb | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/spec/requests/api/api_guard/response_coercer_middleware_spec.rb b/spec/requests/api/api_guard/response_coercer_middleware_spec.rb new file mode 100644 index 00000000000..6f3f97fe846 --- /dev/null +++ b/spec/requests/api/api_guard/response_coercer_middleware_spec.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe API::APIGuard::ResponseCoercerMiddleware do + using RSpec::Parameterized::TableSyntax + + it 'is loaded' do + expect(API::API.middleware).to include([:use, described_class]) + end + + describe '#call' do + let(:app) do + Class.new(API::API) + end + + [ + nil, 201, 10.5, "test" + ].each do |val| + it 'returns a String body' do + app.get 'bodytest' do + status 200 + env['api.format'] = :binary + body val + end + + unless val.is_a?(String) + expect(Gitlab::ErrorTracking).to receive(:track_and_raise_for_dev_exception).with(instance_of(ArgumentError)) + end + + get api('/bodytest') + + expect(response).to have_gitlab_http_status(:ok) + expect(response.body).to eq(val.to_s) + end + end + + [100, 204, 304].each do |status| + it 'allows nil body' do + app.get 'statustest' do + status status + env['api.format'] = :binary + body nil + end + + expect(Gitlab::ErrorTracking).not_to receive(:track_and_raise_for_dev_exception) + + get api('/statustest') + + expect(response.status).to eq(status) + expect(response.body).to eq('') + end + end + end +end |