1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actionpack/lib/action_dispatch/testing/assertion_response.rb
Jeremy Daer 89e2f7e722
Support for unified Integer class in Ruby 2.4+
Ruby 2.4 unifies Fixnum and Bignum into Integer: https://bugs.ruby-lang.org/issues/12005

* Forward compat with new unified Integer class in Ruby 2.4+.
* Backward compat with separate Fixnum/Bignum in Ruby 2.2 & 2.3.
* Drops needless Fixnum distinction in docs, preferring Integer.
2016-05-18 21:58:51 -07:00

45 lines
1.3 KiB
Ruby

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