diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2016-01-12 19:47:40 -0200 |
---|---|---|
committer | Rafael França <rafaelmfranca@gmail.com> | 2016-01-12 19:47:40 -0200 |
commit | 12f4976200019ebc6b699e512691d9e49a5c6988 (patch) | |
tree | 56bca46114756a1e0a338263c15c23ccdd9e8236 /actionpack/lib/action_dispatch/testing/assertion_response.rb | |
parent | 89f70938d79cc797a4ddad5aa6cc13bfc5cf08c9 (diff) | |
parent | a4032ca07248c057c1507dda474bcdb1d644c810 (diff) | |
download | rails-12f4976200019ebc6b699e512691d9e49a5c6988.tar.gz rails-12f4976200019ebc6b699e512691d9e49a5c6988.tar.bz2 rails-12f4976200019ebc6b699e512691d9e49a5c6988.zip |
Merge pull request #22935 from cllns/add-status-name-to-output
Add HTTP status name to output of tests
Diffstat (limited to 'actionpack/lib/action_dispatch/testing/assertion_response.rb')
-rw-r--r-- | actionpack/lib/action_dispatch/testing/assertion_response.rb | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/actionpack/lib/action_dispatch/testing/assertion_response.rb b/actionpack/lib/action_dispatch/testing/assertion_response.rb new file mode 100644 index 0000000000..3fb81ff083 --- /dev/null +++ b/actionpack/lib/action_dispatch/testing/assertion_response.rb @@ -0,0 +1,49 @@ +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. + # + # As an input to the initializer, we take a Fixnum, a String, or a Symbol. + # If it's a Fixnum or String, we figure out what its symbolized name. + # If it's a Symbol, we figure out what its corresponding code is. + # The resulting code will be a Fixnum, for real HTTP codes, and it will + # be a String for the pseudo-HTTP codes, such as: + # :success, :missing, :redirect and :error + class AssertionResponse + attr_reader :code, :name + + GENERIC_RESPONSE_CODES = { # :nodoc: + success: "2XX", + missing: "404", + redirect: "3XX", + error: "5XX" + } + + 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 |