class RSpec::Rails::Matchers::HaveHttpStatus::GenericStatus

@see github.com/rails/rails/blob/7-2-stable/actionpack/lib/action_dispatch/testing/test_response.rb ‘ActionDispatch::TestResponse`
@see RSpec::Rails::Matchers#have_http_status
expect(response).to have_http_status(:redirect)
expect(response).to have_http_status(:missing)
expect(response).to have_http_status(:error)
expect(response).to have_http_status(:success)
@example
Not intended to be instantiated directly.
`ActionDispatch::TestResponse` http status category queries.
Provides an implementation for `have_http_status` matching against
@api private

def self.valid_statuses

Other tags:
    See: https://github.com/rails/rails/blob/main/actionpack/lib/action_dispatch/testing/test_response.rb - `ActionDispatch::TestResponse`

Returns:
  • (Array) - of status codes which represent a HTTP status
def self.valid_statuses
  [
    :error, :success, :missing,
    :server_error, :successful, :not_found,
    :redirect
  ]
end

def check_expected_status(test_response, expected)

def check_expected_status(test_response, expected)
  test_response.send(
    "#{RESPONSE_METHODS.fetch(expected, expected)}?")
end

def description

Returns:
  • (String) -
def description
  "respond with #{type_message}"
end

def failure_message

Returns:
  • (String) - explaining why the match failed
def failure_message
  invalid_response_type_message ||
  "expected the response to have #{type_message} but it was #{actual}"
end

def failure_message_when_negated

Returns:
  • (String) - explaining why the match failed
def failure_message_when_negated
  invalid_response_type_message ||
  "expected the response not to have #{type_message} but it was #{actual}"
end

def initialize(type)

def initialize(type)
  unless self.class.valid_statuses.include?(type)
    raise ArgumentError, "Invalid generic HTTP status: #{type.inspect}"
  end
  @expected = type
  @actual = nil
  @invalid_response = nil
end

def matches?(response)

Returns:
  • (Boolean) - `true` if Rack's associated numeric HTTP code matched
def matches?(response)
  test_response = as_test_response(response)
  @actual = test_response.response_code
  check_expected_status(test_response, expected)
rescue TypeError => _ignored
  @invalid_response = response
  false
end

def type_codes

Other tags:
    See: https://github.com/rack/rack/blob/master/lib/rack/response.rb - `Rack::Response`
    See: https://github.com/rails/rails/blob/main/actionpack/lib/action_dispatch/testing/test_response.rb - `ActionDispatch::TestResponse`

Returns:
  • (String) - formatting the associated code(s) for the various
def type_codes
  # At the time of this commit the most recent version of
  # `ActionDispatch::TestResponse` defines the following aliases:
  #
  #     alias_method :success?,  :successful?
  #     alias_method :missing?,  :not_found?
  #     alias_method :redirect?, :redirection?
  #     alias_method :error?,    :server_error?
  #
  # It's parent `ActionDispatch::Response` includes
  # `Rack::Response::Helpers` which defines the aliased methods as:
  #
  #     def successful?;   status >= 200 && status < 300; end
  #     def redirection?;  status >= 300 && status < 400; end
  #     def server_error?; status >= 500 && status < 600; end
  #     def not_found?;    status == 404;                 end
  #
  # @see https://github.com/rails/rails/blob/ca200378/actionpack/lib/action_dispatch/testing/test_response.rb#L17-L27
  # @see https://github.com/rails/rails/blob/ca200378/actionpack/lib/action_dispatch/http/response.rb#L74
  # @see https://github.com/rack/rack/blob/ce4a3959/lib/rack/response.rb#L119-L122
  @type_codes ||= case expected
                  when :error, :server_error
                    "5xx"
                  when :success, :successful
                    "2xx"
                  when :missing, :not_found
                    "404"
                  when :redirect
                    "3xx"
                  end
end

def type_message

Returns:
  • (String) - formatting the expected status and associated code(s)
def type_message
  @type_message ||= (expected == :error ? "an error" : "a #{expected}") +
    " status code (#{type_codes})"
end