mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
333670ceb9
Previously we'd only assign a response parser when a request came through Action Dispatch integration tests. This made calls to `parsed_body` when a TestResponse was manually instantiated — though own doing or perhaps from a framework — unintentionally blow up because no parser was set at that time. The response can lookup a parser entirely through its own ivars. Extract request encoder to its own file and assume that a viable content type is present at TestResponse instantiation. Since the default response parser is a no-op, making `parsed_body` equal to `body`, no exceptions will be thrown.
28 lines
1 KiB
Ruby
28 lines
1 KiB
Ruby
require 'abstract_unit'
|
|
|
|
class TestResponseTest < ActiveSupport::TestCase
|
|
def assert_response_code_range(range, predicate)
|
|
response = ActionDispatch::TestResponse.new
|
|
(0..599).each do |status|
|
|
response.status = status
|
|
assert_equal range.include?(status), response.send(predicate),
|
|
"ActionDispatch::TestResponse.new(#{status}).#{predicate}"
|
|
end
|
|
end
|
|
|
|
test "helpers" do
|
|
assert_response_code_range 200..299, :successful?
|
|
assert_response_code_range [404], :not_found?
|
|
assert_response_code_range 300..399, :redirection?
|
|
assert_response_code_range 500..599, :server_error?
|
|
assert_response_code_range 400..499, :client_error?
|
|
end
|
|
|
|
test "response parsing" do
|
|
response = ActionDispatch::TestResponse.create(200, {}, '')
|
|
assert_equal response.body, response.parsed_body
|
|
|
|
response = ActionDispatch::TestResponse.create(200, { 'Content-Type' => 'application/json' }, '{ "foo": "fighters" }')
|
|
assert_equal({ 'foo' => 'fighters' }, response.parsed_body)
|
|
end
|
|
end
|