Class: RuboCop::Cop::RSpecRails::HttpStatus

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
ConfigurableEnforcedStyle
Defined in:
lib/rubocop/cop/rspec_rails/http_status.rb

Overview

Enforces use of symbolic or numeric value to describe HTTP status.

This cop inspects only ‘have_http_status` calls. So, this cop does not check if a method starting with `be_*` is used when setting for `EnforcedStyle: symbolic` or `EnforcedStyle: numeric`.

Examples:

‘EnforcedStyle: symbolic` (default)

# bad
it { is_expected.to have_http_status 200 }
it { is_expected.to have_http_status 404 }
it { is_expected.to have_http_status "403" }

# good
it { is_expected.to have_http_status :ok }
it { is_expected.to have_http_status :not_found }
it { is_expected.to have_http_status :forbidden }
it { is_expected.to have_http_status :success }
it { is_expected.to have_http_status :error }

‘EnforcedStyle: numeric`

# bad
it { is_expected.to have_http_status :ok }
it { is_expected.to have_http_status :not_found }
it { is_expected.to have_http_status "forbidden" }

# good
it { is_expected.to have_http_status 200 }
it { is_expected.to have_http_status 404 }
it { is_expected.to have_http_status 403 }
it { is_expected.to have_http_status :success }
it { is_expected.to have_http_status :error }

‘EnforcedStyle: be_status`

# bad
it { is_expected.to have_http_status :ok }
it { is_expected.to have_http_status :not_found }
it { is_expected.to have_http_status "forbidden" }
it { is_expected.to have_http_status 200 }
it { is_expected.to have_http_status 404 }
it { is_expected.to have_http_status "403" }

# good
it { is_expected.to be_ok }
it { is_expected.to be_not_found }
it { is_expected.to have_http_status :success }
it { is_expected.to have_http_status :error }

Defined Under Namespace

Classes: BeStatusStyleChecker, NumericStyleChecker, StyleCheckerBase, SymbolicStyleChecker

Constant Summary collapse

RESTRICT_ON_SEND =
%i[have_http_status].freeze

Instance Method Summary collapse

Instance Method Details

#http_status(node) ⇒ Object



66
67
68
# File 'lib/rubocop/cop/rspec_rails/http_status.rb', line 66

def_node_matcher :http_status, <<~PATTERN
  (send nil? :have_http_status ${int sym str})
PATTERN

#on_send(node) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rubocop/cop/rspec_rails/http_status.rb', line 70

def on_send(node)
  return unless defined?(::Rack::Utils::SYMBOL_TO_STATUS_CODE)

  http_status(node) do |arg|
    return if arg.str_type? && arg.heredoc?

    checker = checker_class.new(arg)
    return unless checker.offensive?

    add_offense(checker.offense_range,
                message: checker.message) do |corrector|
      corrector.replace(checker.offense_range, checker.prefer)
    end
  end
end