跳至内容 跳至搜索

此类的实例表示测试进程按顺序执行的一组请求和响应。因为你可以实例化多个会话并并行运行它们,所以你还可以(在一定程度上)模拟多个同时与你的系统交互的用户。

通常,你将使用 Runner#open_session 实例化一个新会话,而不是直接实例化一个 Session。

方法
C
H
N
P
R
U
包含的模块

常量

DEFAULT_HOST = "www.example.com"
 

属性

[RW] accept

要发送的 Accept 头。

[R] controller

上一个请求使用的控制器实例的引用。

[W] host
[W] host!
[RW] remote_addr

上一个请求中使用的 remote_addr

[R] request

上一个请求使用的请求实例的引用。

[RW] request_count

已处理请求数的运行计数器。

[R] response

上一个请求使用的响应实例的引用。

类公有方法

new(app)

创建并初始化一个新的 Session 实例。

# File actionpack/lib/action_dispatch/testing/integration.rb, line 131
def initialize(app)
  super()
  @app = app

  reset!
end

实例公有方法

cookies()

上一个响应返回的 cookie 的映射,它将与下一个请求一起发送。

# File actionpack/lib/action_dispatch/testing/integration.rb, line 112
def cookies
  _mock_session.cookie_jar
end

host()

上一个请求中使用的主机名。

# File actionpack/lib/action_dispatch/testing/integration.rb, line 99
def host
  @host || DEFAULT_HOST
end

https!(flag = true)

指定会话是否应模拟安全的 HTTPS 请求。

session.https!
session.https!(false)
# File actionpack/lib/action_dispatch/testing/integration.rb, line 179
def https!(flag = true)
  @https = flag
end

https?()

如果会话正在模拟安全的 HTTPS 请求,则返回 true

if session.https?
  ...
end
# File actionpack/lib/action_dispatch/testing/integration.rb, line 188
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 参数。这可能是 nilHash 或经过适当编码的 Stringapplication/x-www-form-urlencodedmultipart/form-data)。

  • headers:要作为 Hash 传递的其他标头。标头将合并到 Rack env 哈希中。

  • env:要作为 Hash 传递的其他 env。标头将合并到 Rack env 哈希中。

  • xhr:如果您想发出 Ajax 请求,请将其设置为 true。添加 XMLHttpRequest 特有的请求标头,例如 HTTP_X_REQUESTED_WITH。标头将合并到 Rack env 哈希中。

  • as:用于使用不同的内容类型对请求进行编码。默认情况下支持 :json,并将设置适当的请求头。这些头将合并到 Rack env 哈希中。

此方法很少直接使用。在集成测试中使用 RequestHelpers#getRequestHelpers#post 或其他标准 HTTP 方法。仅当使用集成测试中未定义方法的请求方法时才需要 #process

此方法在执行请求后返回响应状态。此外,如果此方法是从 ActionDispatch::IntegrationTest 对象调用的,则该对象的 @response 实例变量将指向 Response 对象,该对象可用于检查响应的详细信息。

示例

process :get, '/author', params: { since: 201501011400 }
# File actionpack/lib/action_dispatch/testing/integration.rb, line 226
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.
  session.request(build_full_uri(path, request_env), 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!
# File actionpack/lib/action_dispatch/testing/integration.rb, line 155
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()

# File actionpack/lib/action_dispatch/testing/integration.rb, line 138
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