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

@api private Provides an implementation for `have_http_status` matching against `ActionDispatch::TestResponse` http status category queries.

Not intended to be instantiated directly.

@example

expect(response).to have_http_status(:success)
expect(response).to have_http_status(:error)
expect(response).to have_http_status(:missing)
expect(response).to have_http_status(:redirect)

@see RSpec::Rails::Matchers#have_http_status @see github.com/rails/rails/blob/6-0-stable/actionpack/lib/action_dispatch/testing/test_response.rb `ActionDispatch::TestResponse`

Constants

RESPONSE_METHODS

Public Class Methods

new(type) click to toggle source
# File lib/rspec/rails/matchers/have_http_status.rb, line 255
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
valid_statuses() click to toggle source

@return [Array<Symbol>] of status codes which represent a HTTP status

code "group"

@see github.com/rails/rails/blob/main/actionpack/lib/action_dispatch/testing/test_response.rb `ActionDispatch::TestResponse`

# File lib/rspec/rails/matchers/have_http_status.rb, line 247
def self.valid_statuses
  [
    :error, :success, :missing,
    :server_error, :successful, :not_found,
    :redirect
  ]
end

Public Instance Methods

description() click to toggle source

@return [String]

# File lib/rspec/rails/matchers/have_http_status.rb, line 277
def description
  "respond with #{type_message}"
end
failure_message() click to toggle source

@return [String] explaining why the match failed

# File lib/rspec/rails/matchers/have_http_status.rb, line 282
def failure_message
  invalid_response_type_message ||
  "expected the response to have #{type_message} but it was #{actual}"
end
failure_message_when_negated() click to toggle source

@return [String] explaining why the match failed

# File lib/rspec/rails/matchers/have_http_status.rb, line 288
def failure_message_when_negated
  invalid_response_type_message ||
  "expected the response not to have #{type_message} but it was #{actual}"
end
matches?(response) click to toggle source

@return [Boolean] `true` if Rack's associated numeric HTTP code matched

the `response` code or the named response status
# File lib/rspec/rails/matchers/have_http_status.rb, line 267
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

Protected Instance Methods

check_expected_status(test_response, expected) click to toggle source
# File lib/rspec/rails/matchers/have_http_status.rb, line 301
def check_expected_status(test_response, expected)
  test_response.send(
    "#{RESPONSE_METHODS.fetch(expected, expected)}?")
end

Private Instance Methods

type_codes() click to toggle source

@return [String] formatting the associated code(s) for the various

status code "groups"

@see github.com/rails/rails/blob/main/actionpack/lib/action_dispatch/testing/test_response.rb `ActionDispatch::TestResponse` @see github.com/rack/rack/blob/master/lib/rack/response.rb `Rack::Response`

# File lib/rspec/rails/matchers/have_http_status.rb, line 318
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
type_message() click to toggle source

@return [String] formatting the expected status and associated code(s)

# File lib/rspec/rails/matchers/have_http_status.rb, line 309
def type_message
  @type_message ||= (expected == :error ? "an error" : "a #{expected}") +
    " status code (#{type_codes})"
end