Sha256: ab8a5e41f3f41d4c6684da7b23a868626bf566d6f3e5894633827445205b33b8

Contents?: true

Size: 1.53 KB

Versions: 20

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

require "action_dispatch/testing/request_encoder"

module ActionDispatch
  # Integration test methods such as Integration::RequestHelpers#get
  # and Integration::RequestHelpers#post return objects of class
  # TestResponse, which represent the HTTP response results of the requested
  # controller actions.
  #
  # See Response for more information on controller response objects.
  class TestResponse < Response
    def self.from_response(response)
      new response.status, response.headers, response.body
    end

    # Returns a parsed body depending on the response MIME type. When a parser
    # corresponding to the MIME type is not found, it returns the raw body.
    #
    # ==== Examples
    #   get "/posts"
    #   response.content_type      # => "text/html; charset=utf-8"
    #   response.parsed_body.class # => String
    #   response.parsed_body       # => "<!DOCTYPE html>\n<html>\n..."
    #
    #   get "/posts.json"
    #   response.content_type      # => "application/json; charset=utf-8"
    #   response.parsed_body.class # => Array
    #   response.parsed_body       # => [{"id"=>42, "title"=>"Title"},...
    #
    #   get "/posts/42.json"
    #   response.content_type      # => "application/json; charset=utf-8"
    #   response.parsed_body.class # => Hash
    #   response.parsed_body       # => {"id"=>42, "title"=>"Title"}
    def parsed_body
      @parsed_body ||= response_parser.call(body)
    end

    def response_parser
      @response_parser ||= RequestEncoder.parser(media_type)
    end
  end
end

Version data entries

20 entries across 20 versions & 3 rubygems

Version Path
actionpack-7.0.8.7 lib/action_dispatch/testing/test_response.rb
actionpack-7.0.8.6 lib/action_dispatch/testing/test_response.rb
actionpack-7.0.8.5 lib/action_dispatch/testing/test_response.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.8.4/lib/action_dispatch/testing/test_response.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/actionpack-7.0.5.1/lib/action_dispatch/testing/test_response.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/actionpack-7.0.5.1/lib/action_dispatch/testing/test_response.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/actionpack-7.0.5.1/lib/action_dispatch/testing/test_response.rb
actionpack-7.0.8.4 lib/action_dispatch/testing/test_response.rb
actionpack-7.0.8.1 lib/action_dispatch/testing/test_response.rb
actionpack-7.0.8 lib/action_dispatch/testing/test_response.rb
actionpack-7.0.7.2 lib/action_dispatch/testing/test_response.rb
actionpack-7.0.7.1 lib/action_dispatch/testing/test_response.rb
actionpack-7.0.7 lib/action_dispatch/testing/test_response.rb
actionpack-7.0.6 lib/action_dispatch/testing/test_response.rb
actionpack-7.0.5.1 lib/action_dispatch/testing/test_response.rb
actionpack-7.0.5 lib/action_dispatch/testing/test_response.rb
actionpack-7.0.4.3 lib/action_dispatch/testing/test_response.rb
actionpack-7.0.4.2 lib/action_dispatch/testing/test_response.rb
actionpack-7.0.4.1 lib/action_dispatch/testing/test_response.rb
actionpack-7.0.4 lib/action_dispatch/testing/test_response.rb