class ActionDispatch::Response
end
end
puts @response.body
get(‘/’)
def test_print_root_path_to_console
class DemoControllerTest < ActionDispatch::IntegrationTest
controller response to the console:
For example, the following demo integration “test” prints the body of the
TestResponse (which are of course also of type Response).
ActionDispatch::Integration::Session#post return objects of type
ActionDispatch::Integration::Session#get and
developers. Integration test methods such as
more detail, and that’s when Response can be useful for application
Nevertheless, integration tests may want to inspect controller responses in
ActionControllerBase#headers instead of Response#headers.
to set the HTTP response’s content MIME type, then use
methods defined in ActionController::Base instead. For example, if you want
should never be used directly in controllers. Controllers should use the
Response is mostly a Ruby on Rails framework implement detail, and
from integration tests). See CgiResponse and TestResponse, respectively.
back to the web browser) or a test response (i.e. one that is generated
either represent a “real” HTTP response (i.e. one that is meant to be sent
of the response, or customize the response. An Response object can
an ActionDispatch::Response object to retrieve the current state
Represents an HTTP response generated by a controller action. One can use
:nodoc:
def assign_default_content_type_and_charset!
def assign_default_content_type_and_charset! return if headers[CONTENT_TYPE].present? @content_type ||= Mime::HTML @charset ||= self.class.default_charset type = @content_type.to_s.dup type << "; charset=#{@charset}" unless @sending_file headers[CONTENT_TYPE] = type end
def body
def body str = '' each { |part| str << part.to_s } str end
def body=(body)
def body=(body) @blank = true if body == EMPTY @body = body.respond_to?(:to_str) ? [body] : body end
def body_parts
def body_parts @body end
def code
def code @status.to_s end
def cookies
Returns the response cookies, converted to a Hash of (name => value) pairs
def cookies cookies = {} if header = self["Set-Cookie"] header = header.split("\n") if header.respond_to?(:to_str) header.each do |cookie| if pair = cookie.split(';').first key, value = pair.split("=").map { |v| Rack::Utils.unescape(v) } cookies[key] = value end end end cookies end
def each(&callback)
def each(&callback) if @body.respond_to?(:call) @writer = lambda { |x| callback.call(x) } @body.call(self, self) else @body.each { |part| callback.call(part.to_s) } end @writer = callback @block.call(self) if @block end
def location
def location headers['Location'] end
def location=(url)
def location=(url) headers['Location'] = url end
def message
def message Rack::Utils::HTTP_STATUS_CODES[@status] end
def respond_to?(method)
def respond_to?(method) if method.to_sym == :to_path @body.respond_to?(:to_path) else super end end
def response_code
def response_code @status end
def status=(status)
def status=(status) @status = Rack::Utils.status_code(status) end
def to_a
def to_a assign_default_content_type_and_charset! handle_conditional_get! self["Set-Cookie"] = self["Set-Cookie"].join("\n") if self["Set-Cookie"].respond_to?(:join) self["ETag"] = @_etag if @_etag super end
def to_path
def to_path @body.to_path end
def write(str)
def write(str) str = str.to_s @writer.call str str end