Skip to content

Commit

Permalink
Rubocop fixes
Browse files Browse the repository at this point in the history
Set RSpec/NestedGroups to 4.
  • Loading branch information
ericproulx committed Mar 23, 2024
1 parent bd169c4 commit 286461b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 33 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,6 @@ RSpec/Capybara/FeatureMethods:

RSpec/ExampleLength:
Max: 60

RSpec/NestedGroups:
Max: 4
8 changes: 4 additions & 4 deletions lib/grape/util/accept_header_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/util/media_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def initialize(type:, subtype:)
end

def ==(other)
self.eql?(other)
eql?(other)
end

def eql?(other)
Expand Down
29 changes: 14 additions & 15 deletions spec/grape/util/accept_header_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,36 @@
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,
versions: versions,
**options
)
end

subject { instance.match_best_quality_media_type! }

let(:accept_header) { '*/*' }
let(:versions) { ['v1'] }
let(:options) { {} }

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

describe '#match_best_quality_media_type!' do
context 'when no vendor set' do
let(:options) do
{
vendor: nil,
vendor: nil
}
end

Expand All @@ -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
Expand All @@ -64,7 +63,7 @@
context 'when media_type found' do
let(:options) do
{
vendor: 'vendor',
vendor: 'vendor'
}
end

Expand All @@ -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

Expand All @@ -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
Expand Down
24 changes: 11 additions & 13 deletions spec/grape/util/media_type_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 286461b

Please sign in to comment.