Module: RSpec::Rails::Matchers::HaveHttpStatus Private

Included in:
GenericStatus, NumericCode, SymbolicStatus
Defined in:
lib/rspec/rails/matchers/have_http_status.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Namespace for various implementations of have_http_status.

Defined Under Namespace

Classes: GenericStatus, NumericCode, SymbolicStatus

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (ActionDispatch::TestResponse) as_test_response(obj)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

  • obj (Object)

    object to convert to a response

Returns:

  • (ActionDispatch::TestResponse)
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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

+ (Object) matcher_for_status(target)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

  • target (Object)

    expected http status or code

Returns:

  • response matcher instance

19
20
21
22
23
24
25
26
27
# 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

Instance Method Details

- (String?) invalid_response_type_message

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a formatted failure message if @invalid_response is present, nil otherwise

Returns:

  • (String, nil)

    a formatted failure message if @invalid_response is present, nil otherwise

60
61
62
63
64
# 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