Class: ActionController::AbstractResponse
- Defined in:
- lib/action_controller/response.rb
Overview
Represents an HTTP response generated by a controller action. One can use an ActionController::AbstractResponse object to retrieve the current state of the response, or customize the response. An AbstractResponse object can either represent a “real” HTTP response (i.e. one that is meant to be sent back to the web browser) or a test response (i.e. one that is generated from integration tests). See CgiResponse and TestResponse, respectively.
AbstractResponse is mostly a Ruby on Rails framework implement detail, and should never be used directly in controllers. Controllers should use the methods defined in ActionController::Base instead. For example, if you want to set the HTTP response’s content MIME type, then use ActionControllerBase#headers instead of AbstractResponse#headers.
Nevertheless, integration tests may want to inspect controller responses in more detail, and that’s when AbstractResponse can be useful for application developers. Integration test methods such as ActionController::Integration::Session#get and ActionController::Integration::Session#post return objects of type TestResponse (which are of course also of type AbstractResponse).
For example, the following demo integration “test” prints the body of the controller response to the console:
class DemoControllerTest < ActionController::IntegrationTest
def test_print_root_path_to_console
get('/')
puts @response.body
end
end
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_HEADERS =
{ "Cache-Control" => "no-cache" }
Instance Attribute Summary collapse
-
#assigns ⇒ Object
Returns the value of attribute assigns.
-
#body ⇒ Object
The body content (e.g. HTML) of the response, as a String.
-
#cookies ⇒ Object
Returns the value of attribute cookies.
-
#headers ⇒ Object
The headers of the response, as a Hash.
-
#layout ⇒ Object
Returns the value of attribute layout.
-
#redirected_to ⇒ Object
Returns the value of attribute redirected_to.
-
#redirected_to_method_params ⇒ Object
Returns the value of attribute redirected_to_method_params.
-
#request ⇒ Object
Returns the value of attribute request.
-
#session ⇒ Object
Returns the value of attribute session.
-
#template ⇒ Object
Returns the value of attribute template.
Instance Method Summary collapse
- #assign_default_content_type_and_charset! ⇒ Object
- #charset ⇒ Object
-
#charset=(charset) ⇒ Object
Set the charset of the Content-Type header.
-
#content_type ⇒ Object
Returns the response’s content MIME type, or nil if content type has been set.
-
#content_type=(mime_type) ⇒ Object
Sets the HTTP response’s content MIME type.
- #etag ⇒ Object
- #etag=(etag) ⇒ Object
- #etag? ⇒ Boolean
-
#initialize ⇒ AbstractResponse
constructor
A new instance of AbstractResponse.
- #last_modified ⇒ Object
- #last_modified=(utc_time) ⇒ Object
- #last_modified? ⇒ Boolean
- #location ⇒ Object
- #location=(url) ⇒ Object
- #prepare! ⇒ Object
- #redirect(url, status) ⇒ Object
- #sending_file? ⇒ Boolean
- #status ⇒ Object
- #status=(status) ⇒ Object
Constructor Details
#initialize ⇒ AbstractResponse
Returns a new instance of AbstractResponse.
45 46 47 |
# File 'lib/action_controller/response.rb', line 45 def initialize @body, @headers, @session, @assigns = "", DEFAULT_HEADERS.merge("cookie" => []), [], [] end |
Instance Attribute Details
#assigns ⇒ Object
Returns the value of attribute assigns.
40 41 42 |
# File 'lib/action_controller/response.rb', line 40 def assigns @assigns end |
#body ⇒ Object
The body content (e.g. HTML) of the response, as a String.
37 38 39 |
# File 'lib/action_controller/response.rb', line 37 def body @body end |
#cookies ⇒ Object
Returns the value of attribute cookies.
40 41 42 |
# File 'lib/action_controller/response.rb', line 40 def @cookies end |
#headers ⇒ Object
The headers of the response, as a Hash. It maps header names to header values.
39 40 41 |
# File 'lib/action_controller/response.rb', line 39 def headers @headers end |
#layout ⇒ Object
Returns the value of attribute layout.
40 41 42 |
# File 'lib/action_controller/response.rb', line 40 def layout @layout end |
#redirected_to ⇒ Object
Returns the value of attribute redirected_to.
41 42 43 |
# File 'lib/action_controller/response.rb', line 41 def redirected_to @redirected_to end |
#redirected_to_method_params ⇒ Object
Returns the value of attribute redirected_to_method_params.
41 42 43 |
# File 'lib/action_controller/response.rb', line 41 def redirected_to_method_params @redirected_to_method_params end |
#request ⇒ Object
Returns the value of attribute request.
34 35 36 |
# File 'lib/action_controller/response.rb', line 34 def request @request end |
#session ⇒ Object
Returns the value of attribute session.
40 41 42 |
# File 'lib/action_controller/response.rb', line 40 def session @session end |
#template ⇒ Object
Returns the value of attribute template.
40 41 42 |
# File 'lib/action_controller/response.rb', line 40 def template @template end |
Instance Method Details
#assign_default_content_type_and_charset! ⇒ Object
131 132 133 134 |
# File 'lib/action_controller/response.rb', line 131 def assign_default_content_type_and_charset! self.content_type ||= Mime::HTML self.charset ||= default_charset unless sending_file? end |
#charset ⇒ Object
90 91 92 93 |
# File 'lib/action_controller/response.rb', line 90 def charset charset = String(headers["Content-Type"] || headers["type"]).split(";")[1] charset.blank? ? nil : charset.strip.split("=")[1] end |
#charset=(charset) ⇒ Object
Set the charset of the Content-Type header. Set to nil to remove it. If no content type is set, it defaults to HTML.
81 82 83 84 85 86 87 88 |
# File 'lib/action_controller/response.rb', line 81 def charset=(charset) headers["Content-Type"] = if charset "#{content_type || Mime::HTML}; charset=#{charset}" else content_type || Mime::HTML.to_s end end |
#content_type ⇒ Object
Returns the response’s content MIME type, or nil if content type has been set.
74 75 76 77 |
# File 'lib/action_controller/response.rb', line 74 def content_type content_type = String(headers["Content-Type"] || headers["type"]).split(";")[0] content_type.blank? ? nil : content_type end |
#content_type=(mime_type) ⇒ Object
Sets the HTTP response’s content MIME type. For example, in the controller you could write this:
response.content_type = "text/plain"
If a character set has been defined for this response (see charset=) then the character set information will also be included in the content type information.
64 65 66 67 68 69 70 71 |
# File 'lib/action_controller/response.rb', line 64 def content_type=(mime_type) self.headers["Content-Type"] = if mime_type =~ /charset/ || (c = charset).nil? mime_type.to_s else "#{mime_type}; charset=#{c}" end end |
#etag ⇒ Object
109 110 111 |
# File 'lib/action_controller/response.rb', line 109 def etag headers['ETag'] end |
#etag=(etag) ⇒ Object
117 118 119 |
# File 'lib/action_controller/response.rb', line 117 def etag=(etag) headers['ETag'] = %("#{Digest::MD5.hexdigest(ActiveSupport::Cache.(etag))}") end |
#etag? ⇒ Boolean
113 114 115 |
# File 'lib/action_controller/response.rb', line 113 def etag? headers.include?('ETag') end |
#last_modified ⇒ Object
95 96 97 98 99 |
# File 'lib/action_controller/response.rb', line 95 def last_modified if last = headers['Last-Modified'] Time.httpdate(last) end end |
#last_modified=(utc_time) ⇒ Object
105 106 107 |
# File 'lib/action_controller/response.rb', line 105 def last_modified=(utc_time) headers['Last-Modified'] = utc_time.httpdate end |
#last_modified? ⇒ Boolean
101 102 103 |
# File 'lib/action_controller/response.rb', line 101 def last_modified? headers.include?('Last-Modified') end |
#location ⇒ Object
52 |
# File 'lib/action_controller/response.rb', line 52 def location; headers['Location'] end |
#location=(url) ⇒ Object
53 |
# File 'lib/action_controller/response.rb', line 53 def location=(url) headers['Location'] = url end |
#prepare! ⇒ Object
136 137 138 139 140 141 |
# File 'lib/action_controller/response.rb', line 136 def prepare! assign_default_content_type_and_charset! handle_conditional_get! set_content_length! convert_content_type! end |
#redirect(url, status) ⇒ Object
121 122 123 124 125 |
# File 'lib/action_controller/response.rb', line 121 def redirect(url, status) self.status = status self.location = url.gsub(/[\r\n]/, '') self.body = "<html><body>You are being <a href=\"#{CGI.escapeHTML(url)}\">redirected</a>.</body></html>" end |
#sending_file? ⇒ Boolean
127 128 129 |
# File 'lib/action_controller/response.rb', line 127 def sending_file? headers["Content-Transfer-Encoding"] == "binary" end |
#status ⇒ Object
49 |
# File 'lib/action_controller/response.rb', line 49 def status; headers['Status'] end |
#status=(status) ⇒ Object
50 |
# File 'lib/action_controller/response.rb', line 50 def status=(status) headers['Status'] = status end |