module RSpec::Rails::Matchers::HaveHttpStatus

Namespace for various implementations of `have_http_status`.

@api private

Public Class Methods

as_test_response(obj) click to toggle source

@api private Conversion function to coerce the provided object into an `ActionDispatch::TestResponse`.

@param obj [Object] object to convert to a response @return [ActionDispatch::TestResponse]

# File lib/rspec/rails/matchers/have_http_status.rb, line 35
def as_test_response(obj)
  if ::ActionDispatch::Response === obj
    ::ActionDispatch::TestResponse.from_response(obj)
  elsif ::ActionDispatch::TestResponse === obj
    obj
  elsif obj.respond_to?(:status_code) && obj.respond_to?(:response_headers)
    # Acts As Capybara Session
    # Hack to support `Capybara::Session` without having to load
    # Capybara or catch `NameError`s for the undefined constants
    obj = ActionDispatch::Response.new.tap do |resp|
      resp.status  = obj.status_code
      resp.headers.clear
      resp.headers.merge!(obj.response_headers)
      resp.body    = obj.body
      resp.request = ActionDispatch::Request.new({})
    end
    ::ActionDispatch::TestResponse.from_response(obj)
  else
    raise TypeError, "Invalid response type: #{obj}"
  end
end
matcher_for_status(target) click to toggle source

Instantiates an instance of the proper matcher based on the provided `target`.

@param target [Object] expected http status or code @return response matcher instance

# File lib/rspec/rails/matchers/have_http_status.rb, line 19
def self.matcher_for_status(target)
  if GenericStatus.valid_statuses.include?(target)
    GenericStatus.new(target)
  elsif Symbol === target
    SymbolicStatus.new(target)
  else
    NumericCode.new(target)
  end
end

Public Instance Methods

invalid_response_type_message() click to toggle source

@return [String, nil] a formatted failure message if

`@invalid_response` is present, `nil` otherwise
# File lib/rspec/rails/matchers/have_http_status.rb, line 60
def invalid_response_type_message
  return unless @invalid_response

  "expected a response object, but an instance of " \
  "#{@invalid_response.class} was received"
end

Private Instance Methods

as_test_response(obj) click to toggle source

@api private Conversion function to coerce the provided object into an `ActionDispatch::TestResponse`.

@param obj [Object] object to convert to a response @return [ActionDispatch::TestResponse]

# File lib/rspec/rails/matchers/have_http_status.rb, line 35
def as_test_response(obj)
  if ::ActionDispatch::Response === obj
    ::ActionDispatch::TestResponse.from_response(obj)
  elsif ::ActionDispatch::TestResponse === obj
    obj
  elsif obj.respond_to?(:status_code) && obj.respond_to?(:response_headers)
    # Acts As Capybara Session
    # Hack to support `Capybara::Session` without having to load
    # Capybara or catch `NameError`s for the undefined constants
    obj = ActionDispatch::Response.new.tap do |resp|
      resp.status  = obj.status_code
      resp.headers.clear
      resp.headers.merge!(obj.response_headers)
      resp.body    = obj.body
      resp.request = ActionDispatch::Request.new({})
    end
    ::ActionDispatch::TestResponse.from_response(obj)
  else
    raise TypeError, "Invalid response type: #{obj}"
  end
end