Class: ActionDispatch::TestResponse

Inherits:
Response
  • Object
show all
Defined in:
lib/action_dispatch/testing/test_response.rb

Overview

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.

Constant Summary

Constants inherited from Response

Response::CONTENT_TYPE, Response::Header, Response::NO_CONTENT_CODES, Response::SET_COOKIE

Constants included from Http::FilterRedirect

Http::FilterRedirect::FILTERED

Instance Attribute Summary

Attributes inherited from Response

#headers, #request, #status, #stream

Attributes included from Http::Cache::Response

#cache_control

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Response

#abort, #await_commit, #await_sent, #body, #body=, #body_parts, #charset, #charset=, #close, #code, #commit!, #committed?, #content_type, #content_type=, #cookies, create, #delete_header, #each, #get_header, #has_header?, #initialize, #media_type, merge_default_headers, #message, #reset_body!, #response_code, #send_file, #sending!, #sending?, #sending_file=, #sent!, #sent?, #set_header, #to_a, #write

Methods included from Http::Cache::Response

#date, #date=, #date?, #etag=, #etag?, #last_modified, #last_modified=, #last_modified?, #strong_etag=, #strong_etag?, #weak_etag=, #weak_etag?

Methods included from Http::FilterRedirect

#filtered_location

Constructor Details

This class inherits a constructor from ActionDispatch::Response

Class Method Details

.from_response(response) ⇒ Object



13
14
15
# File 'lib/action_dispatch/testing/test_response.rb', line 13

def self.from_response(response)
  new response.status, response.headers, response.body
end

Instance Method Details

#parsed_bodyObject

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    # => Nokogiri::HTML5::Document
response.parsed_body.to_html  # => "<!DOCTYPE html>\n<html>\n..."

assert_pattern { response.parsed_body.at("main") => { content: "Hello, world" } }

response.parsed_body.at("main") => {name:, content:}
assert_equal "main", name
assert_equal "Some main content", content

get "/posts.json"
response.content_type         # => "application/json; charset=utf-8"
response.parsed_body.class    # => Array
response.parsed_body          # => [{"id"=>42, "title"=>"Title"},...

assert_pattern { response.parsed_body => [{ id: 42 }] }

get "/posts/42.json"
response.content_type         # => "application/json; charset=utf-8"
response.parsed_body.class    # => ActiveSupport::HashWithIndifferentAccess
response.parsed_body          # => {"id"=>42, "title"=>"Title"}

assert_pattern { response.parsed_body => [{ title: /title/i }] }

response.parsed_body => {id:, title:}
assert_equal 42, id
assert_equal "Title", title


49
50
51
# File 'lib/action_dispatch/testing/test_response.rb', line 49

def parsed_body
  @parsed_body ||= response_parser.call(body)
end

#response_parserObject



53
54
55
# File 'lib/action_dispatch/testing/test_response.rb', line 53

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