diff --git a/.rubocop.yml b/.rubocop.yml index 459c29c9ba..120d1d5cf3 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -58,3 +58,6 @@ RSpec/Capybara/FeatureMethods: RSpec/ExampleLength: Max: 60 + +RSpec/NestedGroups: + Max: 4 diff --git a/lib/grape/util/accept_header_handler.rb b/lib/grape/util/accept_header_handler.rb index 451a6567da..25d7036319 100644 --- a/lib/grape/util/accept_header_handler.rb +++ b/lib/grape/util/accept_header_handler.rb @@ -7,12 +7,12 @@ module Util class AcceptHeaderHandler attr_reader :accept_header, :versions, :vendor, :strict, :cascade - def initialize(accept_header:, versions:, vendor: nil, strict: false, cascade: true, **_options) + def initialize(accept_header:, versions:, **options) @accept_header = accept_header @versions = versions - @vendor = vendor - @strict = strict - @cascade = cascade + @vendor = options.fetch(:vendor, nil) + @strict = options.fetch(:strict, false) + @cascade = options.fetch(:cascade, true) end def match_best_quality_media_type!(content_types: Grape::ContentTypes::CONTENT_TYPES, allowed_methods: nil) diff --git a/lib/grape/util/media_type.rb b/lib/grape/util/media_type.rb index 2f3946119a..1812fafa60 100644 --- a/lib/grape/util/media_type.rb +++ b/lib/grape/util/media_type.rb @@ -20,7 +20,7 @@ def initialize(type:, subtype:) end def ==(other) - self.eql?(other) + eql?(other) end def eql?(other) diff --git a/spec/grape/util/accept_header_handler_spec.rb b/spec/grape/util/accept_header_handler_spec.rb index 020ac2cb3f..ac4ff3f461 100644 --- a/spec/grape/util/accept_header_handler_spec.rb +++ b/spec/grape/util/accept_header_handler_spec.rb @@ -3,6 +3,8 @@ require 'grape/util/accept_header_handler' RSpec.describe Grape::Util::AcceptHeaderHandler do + subject(:match_best_quality_media_type!) { instance.match_best_quality_media_type! } + let(:instance) do described_class.new( accept_header: accept_header, @@ -10,9 +12,6 @@ **options ) end - - subject { instance.match_best_quality_media_type! } - let(:accept_header) { '*/*' } let(:versions) { ['v1'] } let(:options) { {} } @@ -20,12 +19,12 @@ shared_examples 'an invalid accept header exception' do |message| before do allow(Grape::Exceptions::InvalidAcceptHeader).to receive(:new) - .with(message, { Grape::Http::Headers::X_CASCADE => 'pass' }) - .and_call_original + .with(message, { Grape::Http::Headers::X_CASCADE => 'pass' }) + .and_call_original end it 'raises a Grape::Exceptions::InvalidAcceptHeader' do - expect { subject }.to raise_error(Grape::Exceptions::InvalidAcceptHeader) + expect { match_best_quality_media_type! }.to raise_error(Grape::Exceptions::InvalidAcceptHeader) end end @@ -33,7 +32,7 @@ context 'when no vendor set' do let(:options) do { - vendor: nil, + vendor: nil } end @@ -55,7 +54,7 @@ end context 'when vendor not found' do - let(:accept_header) { '*/*'} + let(:accept_header) { '*/*' } it_behaves_like 'an invalid accept header exception', 'API vendor or version not found.' end @@ -64,7 +63,7 @@ context 'when media_type found' do let(:options) do { - vendor: 'vendor', + vendor: 'vendor' } end @@ -78,17 +77,17 @@ context 'when media_type is not found' do let(:options) do { - vendor: 'vendor', + vendor: 'vendor' } end let(:accept_header) { 'application/vnd.another_vendor-v1+json' } context 'when allowed_methods present' do - let(:allowed_methods) { ['OPTIONS'] } - subject { instance.match_best_quality_media_type!(allowed_methods: allowed_methods) } + let(:allowed_methods) { ['OPTIONS'] } + it { is_expected.to match_array(allowed_methods) } end @@ -102,12 +101,12 @@ before do allow(Grape::Exceptions::InvalidVersionHeader).to receive(:new) - .with('API version not found.', { Grape::Http::Headers::X_CASCADE => 'pass' }) - .and_call_original + .with('API version not found.', { Grape::Http::Headers::X_CASCADE => 'pass' }) + .and_call_original end it 'raises a Grape::Exceptions::InvalidAcceptHeader' do - expect { subject }.to raise_error(Grape::Exceptions::InvalidVersionHeader) + expect { match_best_quality_media_type! }.to raise_error(Grape::Exceptions::InvalidVersionHeader) end end end diff --git a/spec/grape/util/media_type_spec.rb b/spec/grape/util/media_type_spec.rb index 9576020e86..2905c3f9e5 100644 --- a/spec/grape/util/media_type_spec.rb +++ b/spec/grape/util/media_type_spec.rb @@ -1,9 +1,6 @@ # frozen_string_literal: true - - RSpec.describe Grape::Util::MediaType do - shared_examples 'MediaType' do it { is_expected.to eq(described_class.new(type: type, subtype: subtype)) } end @@ -35,12 +32,13 @@ let(:subtype) { 'vnd.test-v1+json' } it_behaves_like 'MediaType' + end - context 'when header is a vendor mime type without version' do - let(:subtype) { 'vnd.ms-word' } + context 'when header is a vendor mime type without version' do + let(:type) { 'application' } + let(:subtype) { 'vnd.ms-word' } - it_behaves_like 'MediaType' - end + it_behaves_like 'MediaType' end end end @@ -64,12 +62,12 @@ let(:media_type) { 'text/html' } it { is_expected.to be_falsey } + end - context 'when header is a vendor mime type' do - let(:media_type) { 'application/vnd.test-v1+json' } + context 'when header is a vendor mime type' do + let(:media_type) { 'application/vnd.test-v1+json' } - it { is_expected.to be_truthy } - end + it { is_expected.to be_truthy } end end @@ -92,7 +90,7 @@ let(:subtype) { 'html' } it 'calls Rack::Utils.best_q_match' do - expect(Rack::Utils).to receive(:best_q_match).and_call_original + allow(Rack::Utils).to receive(:best_q_match).and_call_original expect(media_type).to eq(described_class.new(type: type, subtype: subtype)) end end @@ -106,7 +104,7 @@ let(:other_media_type_class) { Class.new(Struct.new(:type, :subtype, :vendor, :version, :format)) } let(:other_media_type_instance) { other_media_type_class.new(type, subtype, 'test', 'v1', 'json') } - it { is_expected.not_to eq(other_media_type_class.new(type, subtype, 'test', 'v1', 'json'))} + it { is_expected.not_to eq(other_media_type_class.new(type, subtype, 'test', 'v1', 'json')) } end describe '.hash' do