Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix for custom HTTP status codes #561

Merged
merged 2 commits into from
Feb 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Master (Unreleased)

* The `Rails/HttpStatus` cop is unavailable if the `rack` gem cannot be loaded. ([@bquorning][])
* Fix `Rails/HttpStatus` not working with custom HTTP status codes. ([@bquorning][])

## 1.23.0 (2018-02-23)

* Add `RSpec/Rails/HttpStatus` cop to enforce consistent usage of the status format (numeric or symbolic). ([@anthony-robin][], [@jojos003][])
Expand Down
36 changes: 10 additions & 26 deletions lib/rubocop/cop/rspec/rails/http_status.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'rack/utils'

module RuboCop
module Cop
module RSpec
Expand Down Expand Up @@ -29,13 +31,6 @@ module Rails
# it { is_expected.to have_http_status :error }
#
class HttpStatus < Cop
begin
require 'rack/utils'
RACK_LOADED = true
rescue LoadError
RACK_LOADED = false
end

include ConfigurableEnforcedStyle

def_node_matcher :http_status, <<-PATTERN
Expand All @@ -50,10 +45,6 @@ def on_send(node)
end
end

def support_autocorrect?
RACK_LOADED
end

def autocorrect(node)
lambda do |corrector|
checker = checker_class.new(node)
Expand All @@ -76,24 +67,18 @@ def checker_class
class SymbolicStyleChecker
MSG = 'Prefer `%<prefer>s` over `%<current>s` ' \
'to describe HTTP status code.'.freeze
DEFAULT_MSG = 'Prefer `symbolic` over `numeric` ' \
'to describe HTTP status code.'.freeze

attr_reader :node
def initialize(node)
@node = node
end

def offensive?
!node.sym_type?
!node.sym_type? && !custom_http_status_code?
end

def message
if RACK_LOADED
format(MSG, prefer: preferred_style, current: number.to_s)
else
DEFAULT_MSG
end
format(MSG, prefer: preferred_style, current: number.to_s)
end

def preferred_style
Expand All @@ -109,14 +94,17 @@ def symbol
def number
node.source.to_i
end

def custom_http_status_code?
node.int_type? &&
!::Rack::Utils::SYMBOL_TO_STATUS_CODE.value?(node.source.to_i)
end
end

# :nodoc:
class NumericStyleChecker
MSG = 'Prefer `%<prefer>s` over `%<current>s` ' \
'to describe HTTP status code.'.freeze
DEFAULT_MSG = 'Prefer `numeric` over `symbolic` ' \
'to describe HTTP status code.'.freeze

WHITELIST_STATUS = %i[error success missing redirect].freeze

Expand All @@ -130,11 +118,7 @@ def offensive?
end

def message
if RACK_LOADED
format(MSG, prefer: preferred_style, current: symbol.inspect)
else
DEFAULT_MSG
end
format(MSG, prefer: preferred_style, current: symbol.inspect)
end

def preferred_style
Expand Down
6 changes: 5 additions & 1 deletion lib/rubocop/cop/rspec_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
require_relative 'rspec/factory_bot/dynamic_attribute_defined_statically'
require_relative 'rspec/factory_bot/static_attribute_defined_dynamically'

require_relative 'rspec/rails/http_status'
begin
require_relative 'rspec/rails/http_status'
rescue LoadError # rubocop:disable Lint/HandleExceptions
# Rails/HttpStatus cannot be loaded if rack/utils is unavailable.
end

require_relative 'rspec/align_left_let_brace'
require_relative 'rspec/align_right_let_brace'
Expand Down
28 changes: 6 additions & 22 deletions spec/rubocop/cop/rspec/rails/http_status_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
RUBY
end

it 'does not register an offense when using custom HTTP code' do
expect_no_offenses(<<-RUBY)
it { is_expected.to have_http_status 550 }
RUBY
end

include_examples 'autocorrect',
'it { is_expected.to have_http_status 200 }',
'it { is_expected.to have_http_status :ok }'
Expand All @@ -36,17 +42,6 @@
'it { is_expected.to have_http_status(404) }',
'it { is_expected.to have_http_status(:not_found) }'
end

context 'when rack is not loaded' do
before { stub_const("#{described_class}::RACK_LOADED", false) }

it 'registers an offense when using numeric value' do
expect_offense(<<-RUBY)
it { is_expected.to have_http_status 200 }
^^^ Prefer `symbolic` over `numeric` to describe HTTP status code.
RUBY
end
end
end

context 'when EnforcedStyle is `numeric`' do
Expand Down Expand Up @@ -91,16 +86,5 @@
'it { is_expected.to have_http_status(:not_found) }',
'it { is_expected.to have_http_status(404) }'
end

context 'when rack is not loaded' do
before { stub_const("#{described_class}::RACK_LOADED", false) }

it 'registers an offense when using numeric value' do
expect_offense(<<-RUBY)
it { is_expected.to have_http_status :ok }
^^^ Prefer `numeric` over `symbolic` to describe HTTP status code.
RUBY
end
end
end
end