123456789_123456789_123456789_123456789_123456789_

Class: RSpec::Rails::Matchers::HaveHttpStatus::GenericStatus Private

Do not use. This class is for internal use only.

Overview

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

Not intended to be instantiated directly.

Examples:

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 Also:

Constant Summary

::RSpec::Rails::Matchers::BaseMatcher - Inherited

UNDEFINED

Class Method Summary

::RSpec::Rails::Matchers::BaseMatcher - Inherited

.matcher_name, .new,
.underscore

Borrowed from ActiveSupport.

Instance Attribute Summary

::RSpec::Rails::Matchers::BaseMatcher - Inherited

#actual,
#diffable?

::RSpec::Rails::Matchers are not diffable by default.

#expected, #expects_call_stack_jump?, #matcher_name, #matcher_name=, #rescued_exception,
#supports_block_expectations?

Most matchers are value matchers (i.e. meant to work with ‘expect(value)`) rather than block matchers (i.e. meant to work with `expect { }`), so this defaults to false.

Instance Method Summary

::RSpec::Rails::Matchers::HaveHttpStatus - Included

#invalid_response_type_message,
#as_test_response

Conversion function to coerce the provided object into an ‘ActionDispatch::TestResponse`.

::RSpec::Rails::Matchers::BaseMatcher - Inherited

#actual_formatted,
#description

Generates a description using ::RSpec::Matchers::EnglishPhrasing.

#expected_formatted,
#match_unless_raises

Used to wrap a block of code that will indicate failure by raising one of the named exceptions.

#matches?

Indicates if the match is successful.

#assert_ivars, #present_ivars

::RSpec::Rails::Matchers::BaseMatcher::DefaultFailureMessages - Included

#failure_message

Provides a good generic failure message.

#failure_message_when_negated

Provides a good generic negative failure message.

::RSpec::Rails::Matchers::BaseMatcher::HashFormatting - Included

#improve_hash_formatting

‘{ :a => 5, :b => 2 }.inspect` produces:

::RSpec::Matchers::Composable - Included

#&
#===

Delegates to ‘#matches?`.

#and

Creates a compound ‘and` expectation.

#description_of

Returns the description of the given object in a way that is aware of composed matchers.

#or

Creates a compound ‘or` expectation.

#values_match?

This provides a generic way to fuzzy-match an expected value against an actual value.

#|
#should_enumerate?

We should enumerate arrays as long as they are not recursive.

#surface_descriptions_in

Transforms the given data structure (typically a hash or array) into a new data structure that, when ‘#inspect` is called on it, will provide descriptions of any contained matchers rather than the normal #inspect output.

#unreadable_io?,
#with_matchers_cloned

Historically, a single matcher instance was only checked against a single value.

Class Method Details

.valid_statusesArray<Symbol>

Returns:

  • (Array<Symbol>)

    of status codes which represent a HTTP status code “group”

See Also:

[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/matchers/have_http_status.rb', line 247

def self.valid_statuses
  [
    :error, :success, :missing,
    :server_error, :successful, :not_found,
    :redirect
  ]
end

Instance Method Details

#check_expected_status(test_response, expected) (protected)

[ GitHub ]

  
# File 'rspec-rails/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

#descriptionString

[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/matchers/have_http_status.rb', line 277

def description
  "respond with #{type_message}"
end

#failure_messageString

Returns:

  • (String)

    explaining why the match failed

[ GitHub ]

  
# File 'rspec-rails/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_negatedString

Returns:

  • (String)

    explaining why the match failed

[ GitHub ]

  
# File 'rspec-rails/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) ⇒ Boolean

Returns:

  • (Boolean)

    ‘true` if Rack’s associated numeric HTTP code matched the ‘response` code or the named response status

[ GitHub ]

  
# File 'rspec-rails/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

#type_codesString (private)

Returns:

  • (String)

    formatting the associated code(s) for the various status code “groups”

See Also:

[ GitHub ]

  
# File 'rspec-rails/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_messageString (private)

Returns:

  • (String)

    formatting the expected status and associated code(s)

[ GitHub ]

  
# File 'rspec-rails/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