此类的一个实例代表了一个测试进程按顺序执行的一系列请求和响应。由于您可以实例化多个会话并并行运行它们,因此您还可以(在一定程度上)模拟多个并发用户与您的系统进行交互。
通常,您会使用 Runner#open_session 来实例化一个新的会话,而不是直接实例化 Session。
- Minitest::Assertions
- ActionDispatch::Routing::UrlFor
常量
| DEFAULT_HOST | = | "www.example.com" |
Attributes
| [RW] | accept | 要发送的 Accept 标头。 |
| [R] | controller | 最后一次请求使用的控制器实例的引用。 |
| [W] | host | |
| [W] | host! | |
| [RW] | remote_addr | 最后一次请求使用的 |
| [R] | request | 最后一次请求使用的请求实例的引用。 |
| [RW] | request_count | 已处理请求数量的运行计数器。 |
| [R] | response | 最后一次请求使用的响应实例的引用。 |
类公共方法
new(app) 链接
创建并初始化一个新的 Session 实例。
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/testing/integration.rb, line 133 def initialize(app) super() @app = app reset! end
实例公共方法
host() 链接
最后一次请求中使用的HOSTNAME。
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/testing/integration.rb, line 101 def host @host || DEFAULT_HOST end
https!(flag = true) 链接
指定会话是否应模拟安全的 HTTPS 请求。
session.https! session.https!(false)
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/testing/integration.rb, line 180 def https!(flag = true) @https = flag end
https?() 链接
如果会话正在模拟安全的 HTTPS 请求,则返回 true。
if session.https? ... end
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/testing/integration.rb, line 189 def https? @https end
process(method, path, params: nil, headers: nil, env: nil, xhr: false, as: nil) 链接
执行实际请求。
-
method: HTTP 方法(GET、POST、PATCH、PUT、DELETE、HEAD、OPTIONS),表示为符号。 -
path: 要在其上执行请求的 URI(作为String)。 -
params: 要传递的 HTTP 参数。这可以是nil、Hash或已正确编码的String(application/x-www-form-urlencoded或multipart/form-data)。 -
headers: 要传递的附加标头,作为Hash。这些标头将合并到 Rack env 哈希中。 -
env: 要传递的附加 env,作为Hash。这些标头将合并到 Rack env 哈希中。 -
xhr: 如果要发出 Ajax 请求,则设置为true。添加 XMLHttpRequest 特有的请求标头,例如 HTTP_X_REQUESTED_WITH。这些标头将合并到 Rack env 哈希中。 -
as: 用于使用不同内容类型编码请求。默认支持:json,并将设置相应的请求标头。这些标头将合并到 Rack env 哈希中。
此方法很少直接使用。在集成测试中使用 RequestHelpers#get、RequestHelpers#post 或其他标准 HTTP 方法。仅当使用集成测试中未定义方法的请求方法时,才需要 process。
此方法在执行请求后返回响应状态。此外,如果此方法是从 ActionDispatch::IntegrationTest 对象调用的,则该对象的 @response 实例变量将指向一个 Response 对象,您可以使用该对象检查响应的详细信息。
示例: process :get, ‘/author’, params: { since: 201501011400 }
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/testing/integration.rb, line 225 def process(method, path, params: nil, headers: nil, env: nil, xhr: false, as: nil) request_encoder = RequestEncoder.encoder(as) headers ||= {} if method == :get && as == :json && params headers["X-Http-Method-Override"] = "GET" method = :post end if path.include?("://") path = build_expanded_path(path) do |location| https! URI::HTTPS === location if location.scheme if url_host = location.host default = Rack::Request::DEFAULT_PORTS[location.scheme] url_host += ":#{location.port}" if default != location.port host! url_host end end end hostname, port = host.split(":") request_env = { :method => method, :params => request_encoder.encode_params(params), "SERVER_NAME" => hostname, "SERVER_PORT" => port || (https? ? "443" : "80"), "HTTPS" => https? ? "on" : "off", "rack.url_scheme" => https? ? "https" : "http", "REQUEST_URI" => path, "HTTP_HOST" => host, "REMOTE_ADDR" => remote_addr, "HTTP_ACCEPT" => request_encoder.accept_header || accept } if request_encoder.content_type request_env["CONTENT_TYPE"] = request_encoder.content_type end wrapped_headers = Http::Headers.from_hash({}) wrapped_headers.merge!(headers) if headers if xhr wrapped_headers["HTTP_X_REQUESTED_WITH"] = "XMLHttpRequest" wrapped_headers["HTTP_ACCEPT"] ||= [Mime[:js], Mime[:html], Mime[:xml], "text/xml", "*/*"].join(", ") end # This modifies the passed request_env directly. if wrapped_headers.present? Http::Headers.from_hash(request_env).merge!(wrapped_headers) end if env.present? Http::Headers.from_hash(request_env).merge!(env) end session = Rack::Test::Session.new(_mock_session) # NOTE: rack-test v0.5 doesn't build a default uri correctly Make sure requested # path is always a full URI. uri = build_full_uri(path, request_env) if method == :get && String === request_env[:params] # rack-test will needlessly parse and rebuild a :params # querystring, using Rack's query parser. At best that's a # waste of time; at worst it can change the value. uri << "?" << request_env.delete(:params) end session.request(uri, request_env) @request_count += 1 @request = ActionDispatch::Request.new(session.last_request.env) response = _mock_session.last_response @response = ActionDispatch::TestResponse.from_response(response) @response.request = @request @html_document = nil @url_options = nil @controller = @request.controller_instance response.status end
reset!() 链接
重置实例。这可以用来重置现有会话实例中的状态信息,以便可以从干净的状态进行使用。
session.reset!
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/testing/integration.rb, line 156 def reset! @https = false @controller = @request = @response = nil @_mock_session = nil @request_count = 0 @url_options = nil self.host = DEFAULT_HOST self.remote_addr = "127.0.0.1" self.accept = "text/xml,application/xml,application/xhtml+xml," \ "text/html;q=0.9,text/plain;q=0.8,image/png," \ "*/*;q=0.5" unless defined? @named_routes_configured # the helpers are made protected by default--we make them public for easier # access during testing and troubleshooting. @named_routes_configured = true end end
url_options() 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/testing/integration.rb, line 140 def url_options @url_options ||= default_url_options.dup.tap do |url_options| url_options.reverse_merge!(controller.url_options) if controller.respond_to?(:url_options) if @app.respond_to?(:routes) url_options.reverse_merge!(@app.routes.default_url_options) end url_options.reverse_merge!(host: host, protocol: https? ? "https" : "http") end end