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/7-2-stable/actionpack/lib/action_dispatch/testing/test_response.rb ‘ActionDispatch::TestResponse`
Constants
- RESPONSE_METHODS
Public Class Methods
Source
# File lib/rspec/rails/matchers/have_http_status.rb, line 251 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
Source
# File lib/rspec/rails/matchers/have_http_status.rb, line 243 def self.valid_statuses [ :error, :success, :missing, :server_error, :successful, :not_found, :redirect ] end
@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`
Public Instance Methods
Source
# File lib/rspec/rails/matchers/have_http_status.rb, line 273 def description "respond with #{type_message}" end
@return [String]
Source
# File lib/rspec/rails/matchers/have_http_status.rb, line 278 def failure_message invalid_response_type_message || "expected the response to have #{type_message} but it was #{actual}" end
@return [String] explaining why the match failed
Source
# File lib/rspec/rails/matchers/have_http_status.rb, line 284 def failure_message_when_negated invalid_response_type_message || "expected the response not to have #{type_message} but it was #{actual}" end
@return [String] explaining why the match failed
Source
# File lib/rspec/rails/matchers/have_http_status.rb, line 263 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
@return [Boolean] ‘true` if Rack’s associated numeric HTTP code matched
the `response` code or the named response status
Protected Instance Methods
Source
# File lib/rspec/rails/matchers/have_http_status.rb, line 297 def check_expected_status(test_response, expected) test_response.send( "#{RESPONSE_METHODS.fetch(expected, expected)}?") end
Private Instance Methods
Source
# File lib/rspec/rails/matchers/have_http_status.rb, line 314 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
@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`
Source
# File lib/rspec/rails/matchers/have_http_status.rb, line 305 def type_message @type_message ||= (expected == :error ? "an error" : "a #{expected}") + " status code (#{type_codes})" end
@return [String] formatting the expected status and associated code(s)