class RSpec::Rails::Matchers::HaveHttpStatus::SymbolicStatus
@api private Provides an implementation for ‘have_http_status` matching against Rack symbol http status codes.
Not intended to be instantiated directly.
@example
expect(response).to have_http_status(:created)
@see RSpec::Rails::Matchers#have_http_status
@see github.com/rack/rack/blob/master/lib/rack/utils.rb ‘Rack::Utils::SYMBOL_TO_STATUS_CODE`
Attributes
The initialized expected status symbol
Public Class Methods
# File lib/rspec/rails/matchers/have_http_status.rb, line 131 def initialize(status) @expected_status = status @actual = nil @invalid_response = nil set_expected_code! end
Public Instance Methods
@return [String]
# File lib/rspec/rails/matchers/have_http_status.rb, line 151 def description "respond with status code #{pp_expected}" end
@return [String] explaining why the match failed
# File lib/rspec/rails/matchers/have_http_status.rb, line 156 def failure_message invalid_response_type_message || "expected the response to have status code #{pp_expected} but it" \ " was #{pp_actual}" end
@return [String] explaining why the match failed
# File lib/rspec/rails/matchers/have_http_status.rb, line 163 def failure_message_when_negated invalid_response_type_message || "expected the response not to have status code #{pp_expected} " \ "but it did" end
@param [Object] response object providing an http code to match @return [Boolean] ‘true` if Rack’s associated numeric HTTP code matched
the `response` code
# File lib/rspec/rails/matchers/have_http_status.rb, line 141 def matches?(response) test_response = as_test_response(response) @actual = test_response.response_code expected == @actual rescue TypeError => _ignored @invalid_response = response false end
Private Instance Methods
@return [Symbol] representing the actual http numeric code
# File lib/rspec/rails/matchers/have_http_status.rb, line 176 def actual_status return unless actual @actual_status ||= compute_status_from(actual) end
Reverse lookup of the Rack status code symbol based on the numeric http code
@param code [Fixnum] http status code to look up @return [Symbol] representing the http numeric code
# File lib/rspec/rails/matchers/have_http_status.rb, line 187 def compute_status_from(code) status, _ = Rack::Utils::SYMBOL_TO_STATUS_CODE.find do |_, c| c == code end status end
@return [String] pretty format the actual response status
# File lib/rspec/rails/matchers/have_http_status.rb, line 195 def pp_actual pp_status(actual_status, actual) end
@return [String] pretty format the expected status and associated code
# File lib/rspec/rails/matchers/have_http_status.rb, line 200 def pp_expected pp_status(expected_status, expected) end
@return [String] pretty format the actual response status
# File lib/rspec/rails/matchers/have_http_status.rb, line 205 def pp_status(status, code) if status "#{status.inspect} (#{code})" else code.to_s end end
Sets ‘expected` to the numeric http code based on the Rack `expected_status` status
@see Rack::Utils::SYMBOL_TO_STATUS_CODE @raise [ArgumentError] if an associated code could not be found
# File lib/rspec/rails/matchers/have_http_status.rb, line 218 def set_expected_code! @expected ||= Rack::Utils::SYMBOL_TO_STATUS_CODE.fetch(expected_status) do raise ArgumentError, "Invalid HTTP status: #{expected_status.inspect}" end end