Sha256: da82a3145abcf5fa3dc8f4846731939ce5eee8925ff57164fe70373d4f3b976b

Contents?: true

Size: 1.32 KB

Versions: 103

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

module ActionDispatch
  # This is a class that abstracts away an asserted response. It purposely
  # does not inherit from Response because it doesn't need it. That means it
  # does not have headers or a body.
  class AssertionResponse
    attr_reader :code, :name

    GENERIC_RESPONSE_CODES = { # :nodoc:
      success: "2XX",
      missing: "404",
      redirect: "3XX",
      error: "5XX"
    }

    # Accepts a specific response status code as an Integer (404) or String
    # ('404') or a response status range as a Symbol pseudo-code (:success,
    # indicating any 200-299 status code).
    def initialize(code_or_name)
      if code_or_name.is_a?(Symbol)
        @name = code_or_name
        @code = code_from_name(code_or_name)
      else
        @name = name_from_code(code_or_name)
        @code = code_or_name
      end

      raise ArgumentError, "Invalid response name: #{name}" if @code.nil?
      raise ArgumentError, "Invalid response code: #{code}" if @name.nil?
    end

    def code_and_name
      "#{code}: #{name}"
    end

    private
      def code_from_name(name)
        GENERIC_RESPONSE_CODES[name] || Rack::Utils::SYMBOL_TO_STATUS_CODE[name]
      end

      def name_from_code(code)
        GENERIC_RESPONSE_CODES.invert[code] || Rack::Utils::HTTP_STATUS_CODES[code]
      end
  end
end

Version data entries

103 entries across 99 versions & 6 rubygems

Version Path
actionpack-7.0.8.7 lib/action_dispatch/testing/assertion_response.rb
actionpack-7.0.8.6 lib/action_dispatch/testing/assertion_response.rb
actionpack-6.1.7.10 lib/action_dispatch/testing/assertion_response.rb
actionpack-6.1.7.9 lib/action_dispatch/testing/assertion_response.rb
actionpack-7.0.8.5 lib/action_dispatch/testing/assertion_response.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.8.4/lib/action_dispatch/testing/assertion_response.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/actionpack-7.0.5.1/lib/action_dispatch/testing/assertion_response.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/actionpack-7.0.5.1/lib/action_dispatch/testing/assertion_response.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/actionpack-7.0.5.1/lib/action_dispatch/testing/assertion_response.rb
actionpack-7.0.8.4 lib/action_dispatch/testing/assertion_response.rb
actionpack-6.1.7.8 lib/action_dispatch/testing/assertion_response.rb
actionpack-7.0.8.1 lib/action_dispatch/testing/assertion_response.rb
actionpack-6.1.7.7 lib/action_dispatch/testing/assertion_response.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/actionpack-7.0.2.3/lib/action_dispatch/testing/assertion_response.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/actionpack-7.0.3.1/lib/action_dispatch/testing/assertion_response.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/actionpack-6.1.6.1/lib/action_dispatch/testing/assertion_response.rb
actionpack-7.1.2 lib/action_dispatch/testing/assertion_response.rb
actionpack-7.1.1 lib/action_dispatch/testing/assertion_response.rb
actionpack-7.1.0 lib/action_dispatch/testing/assertion_response.rb
actionpack-7.1.0.rc2 lib/action_dispatch/testing/assertion_response.rb