Action Dispatch Response
表示由控制器動作產生的 HTTP 回應。使用它來擷取回應的目前狀態,或自訂回應。它可以表示一個真實的 HTTP 回應(即要發送回網路瀏覽器的回應),也可以表示一個 TestResponse
(即從整合測試產生的回應)。
目前請求的 Response
物件在控制器上公開為 ActionController::Metal#response
。ActionController::Metal
也提供了一些額外的方法,委派給 Response
的屬性,例如 ActionController::Metal#headers
。
整合
測試可能也需要更詳細地檢查回應。諸如 Integration::RequestHelpers#get
和 Integration::RequestHelpers#post
之類的方法會为此返回 TestResponse
(繼承自 Response
)的實例。
例如,以下演示整合測試將控制器回應的內容主體輸出到控制台
class DemoControllerTest < ActionDispatch::IntegrationTest
def test_print_root_path_to_console
get('/')
puts response.body
end
end
- A
- B
- C
- D
- E
- G
- H
- L
- M
- N
- P
- R
- S
- T
- W
- Rack::Response::Helpers
- ActionDispatch::Http::FilterRedirect
- ActionDispatch::Http::Cache::Response
- MonitorMixin
常數
CONTENT_TYPE | = | "Content-Type" |
ContentTypeHeader | = | Struct.new :mime_type, :charset |
Header | = | Headers |
將被棄用 |
||
Headers | = | ::Rack::Headers |
NO_CONTENT_CODES | = | [100, 101, 102, 103, 204, 205, 304] |
NullContentTypeHeader | = | ContentTypeHeader.new nil, nil |
SET_COOKIE | = | "Set-Cookie" |
屬性
[R] | 標頭 (header) | 回應的標頭。
也別名為
也別名為 |
[R] | 標頭 (headers) | 回應的標頭。
也別名為
也別名為 |
[RW] | 請求 (request) | 回應所回應的請求。 |
[R] | 狀態 (status) | HTTP 狀態碼。 |
[R] | 串流 (stream) | 底層主體,作為可串流物件。 |
類別公開方法
create(status = 200, headers = {}, body = [], default_headers: self.default_headers) 連結
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 159 def self.create(status = 200, headers = {}, body = [], default_headers: self.default_headers) headers = merge_default_headers(headers, default_headers) new status, headers, body end
merge_default_headers(original, default) 連結
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 164 def self.merge_default_headers(original, default) default.respond_to?(:merge) ? default.merge(original) : original end
new(status = 200, headers = nil, body = []) 連結
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 171 def initialize(status = 200, headers = nil, body = []) super() @headers = Headers.new headers&.each do |key, value| @headers[key] = value end self.body, self.status = body, status @cv = new_cond @committed = false @sending = false @sent = false prepare_cache_control! yield self if block_given? end
實例公開方法
abort() 連結
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 396 def abort if stream.respond_to?(:abort) stream.abort elsif stream.respond_to?(:close) # `stream.close` should really be reserved for a close from the other direction, # but we must fall back to it for compatibility. stream.close end end
await_commit() 連結
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 197 def await_commit synchronize do @cv.wait_until { @committed } end end
await_sent() 連結
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 203 def await_sent synchronize { @cv.wait_until { @sent } } end
body() 連結
以字串形式返回回應的內容。這包含任何對 `render` 呼叫的內容。
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 330 def body @stream.body end
body=(body) 連結
允許您手動設定或覆蓋回應主體。
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 339 def body=(body) if body.respond_to?(:to_path) @stream = body else synchronize do @stream = build_buffer self, munge_body_object(body) end end end
body_parts() 連結
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 383 def body_parts parts = [] @stream.each { |x| parts << x } parts end
charset() 連結
回應的字元集。HTML 需要知道您提供給他們的內容的編碼,因此我們需要將其發送出去。
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 300 def charset header_info = parsed_content_type_header header_info.charset || self.class.default_charset end
charset=(charset) 連結
設定 HTTP 字元集。如果參數為 `nil`,則將字元集設定為 `default_charset`。
response.charset = 'utf-16' # => 'utf-16'
response.charset = nil # => 'utf-8'
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 289 def charset=(charset) content_type = parsed_content_type_header.mime_type if false == charset set_content_type content_type, nil else set_content_type content_type, charset || self.class.default_charset end end
close() 連結
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 392 def close stream.close if stream.respond_to?(:close) end
code() 連結
返回一個字串以確保與 `Net::HTTPResponse` 相容。
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 311 def code @status.to_s end
commit!() 連結
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 207 def commit! synchronize do before_committed @committed = true @cv.broadcast end end
committed?() 連結
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 231 def committed?; synchronize { @committed }; end
content_type() 連結
回應的內容類型。
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 269 def content_type super.presence end
content_type=(content_type) 連結
設定 HTTP 回應的內容 MIME 類型。例如,在控制器中,您可以這樣寫
response.content_type = "text/plain"
如果已为此回應定義了字元集(請參閱 charset=
),則字元集資訊也將包含在內容類型資訊中。
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 259 def content_type=(content_type) return unless content_type new_header_info = parse_content_type(content_type.to_s) prev_header_info = parsed_content_type_header charset = new_header_info.charset || prev_header_info.charset charset ||= self.class.default_charset unless prev_header_info.mime_type set_content_type new_header_info.mime_type, charset end
cookies() 連結
返回響應 Cookie,轉換為 (名稱 => 值) 對的 雜湊
assert_equal 'AuthorOfNewPage', r.cookies['author']
來源:顯示 | 在 GitHub 上
delete_header(key) 連結
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 195 def delete_header(key); @headers.delete key; end
each(&block) 連結
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 77 def each(&block) sending! x = @stream.each(&block) sent! x end
get_header(key) 連結
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 193 def get_header(key); @headers[key]; end
has_header?(key) 連結
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 192 def has_header?(key); @headers.key? key; end
location 連結
回應的位置。
來源:在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 235
location=(location) 連結
設定回應的位置
來源:在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 240
media_type() 連結
回應的媒體類型。
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 274 def media_type parsed_content_type_header.mime_type end
message() 連結
傳回目前 HTTP 狀態碼的對應訊息
response.status = 200
response.message # => "OK"
response.status = 404
response.message # => "Not Found"
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 323 def message Rack::Utils::HTTP_STATUS_CODES[@status] end
reset_body!() 連結
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 379 def reset_body! @stream = build_buffer(self, []) end
response_code() 連結
請求的回應碼。
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 306 def response_code @status end
send_file(path) 連結
將儲存在 `path` 的檔案作為回應主體發送。
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 374 def send_file(path) commit! @stream = FileBody.new(path) end
sending!() 連結
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 215 def sending! synchronize do before_sending @sending = true @cv.broadcast end end
sending?() 連結
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 230 def sending?; synchronize { @sending }; end
sending_file=(v) 連結
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 278 def sending_file=(v) if true == v self.charset = false end end
sent!() 連結
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 223 def sent! synchronize do @sent = true @cv.broadcast end end
sent?() 連結
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 232 def sent?; synchronize { @sent }; end
set_header(key, v) 連結
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 194 def set_header(key, v); @headers[key] = v; end
status=(status) 連結
設定 HTTP 狀態碼。
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 247 def status=(status) @status = Rack::Utils.status_code(status) end
to_a() 連結
將 `Response` 轉換為 Rack 相容的陣列,包含狀態、標頭和主體。允許明確的展開
status, headers, body = *response
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 410 def to_a commit! rack_response @status, @headers.to_hash end
write(string) 連結
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 334 def write(string) @stream.write string end