class ActionDispatch::Integration::Session

An instance of this class represents a set of requests and responses performed sequentially by a test process. Because you can instantiate multiple sessions and run them side-by-side, you can also mimic (to some limited extent) multiple simultaneous users interacting with your system.

Typically, you will instantiate a new session using IntegrationTest#open_session, rather than instantiating Integration::Session directly.

Constants

DEFAULT_HOST
REQUEST_KWARGS

Attributes

accept[RW]

The Accept header to send.

controller[R]

A reference to the controller instance used by the last request.

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

The #remote_addr used in the last request.

request[R]

A reference to the request instance used by the last request.

request_count[RW]

A running counter of the number of requests processed.

response[R]

A reference to the response instance used by the last request.

Public Class Methods

new(app) click to toggle source

Create and initialize a new Session instance.

Calls superclass method ActionDispatch::Routing::UrlFor.new
# File lib/action_dispatch/testing/integration.rb, line 223
def initialize(app)
  super()
  @app = app

  reset!
end

Public Instance Methods

cookies() click to toggle source

A map of the cookies returned by the last response, and which will be sent with the next request.

# File lib/action_dispatch/testing/integration.rb, line 204
def cookies
  _mock_session.cookie_jar
end
host() click to toggle source

The hostname used in the last request.

# File lib/action_dispatch/testing/integration.rb, line 191
def host
  @host || DEFAULT_HOST
end
https!(flag = true) click to toggle source

Specify whether or not the session should mimic a secure HTTPS request.

session.https!
session.https!(false)
# File lib/action_dispatch/testing/integration.rb, line 271
def https!(flag = true)
  @https = flag
end
https?() click to toggle source

Returns true if the session is mimicking a secure HTTPS request.

if session.https?
  ...
end
# File lib/action_dispatch/testing/integration.rb, line 280
def https?
  @https
end
reset!() click to toggle source

Resets the instance. This can be used to reset the state information in an existing session instance, so it can be used from a clean-slate condition.

session.reset!
# File lib/action_dispatch/testing/integration.rb, line 247
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() click to toggle source
# File lib/action_dispatch/testing/integration.rb, line 230
def url_options
  @url_options ||= default_url_options.dup.tap do |url_options|
    url_options.reverse_merge!(controller.url_options) if controller

    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

Private Instance Methods

_mock_session() click to toggle source
# File lib/action_dispatch/testing/integration.rb, line 290
def _mock_session
  @_mock_session ||= Rack::MockSession.new(@app, host)
end
build_full_uri(path, env) click to toggle source
# File lib/action_dispatch/testing/integration.rb, line 395
def build_full_uri(path, env)
  "#{env['rack.url_scheme']}://#{env['SERVER_NAME']}:#{env['SERVER_PORT']}#{path}"
end
kwarg_request?(args) click to toggle source
# File lib/action_dispatch/testing/integration.rb, line 304
def kwarg_request?(args)
  args[0].respond_to?(:keys) && args[0].keys.any? { |k| REQUEST_KWARGS.include?(k) }
end
non_kwarg_request_warning() click to toggle source
# File lib/action_dispatch/testing/integration.rb, line 308
        def non_kwarg_request_warning
          ActiveSupport::Deprecation.warn("            ActionDispatch::IntegrationTest HTTP request methods will accept only
            the following keyword arguments in future Rails versions:
            #{REQUEST_KWARGS.join(', ')}

            Examples:

            get '/profile',
              params: { id: 1 },
              headers: { 'X-Extra-Header' => '123' },
              env: { 'action_dispatch.custom' => 'custom' },
              xhr: true,
              as: :json
".strip_heredoc)
        end
process(method, path, params: nil, headers: nil, env: nil, xhr: false, as: nil) click to toggle source

Performs the actual request.

# File lib/action_dispatch/testing/integration.rb, line 326
def process(method, path, params: nil, headers: nil, env: nil, xhr: false, as: nil)
  request_encoder = RequestEncoder.encoder(as)

  if path =~ %r{://}
    location = URI.parse(path)
    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
    path = request_encoder.append_format_to location.path
    path = location.query ? "#{path}?#{location.query}" : path
  else
    path = request_encoder.append_format_to path
  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,
    "CONTENT_TYPE"   => request_encoder.content_type,
    "HTTP_ACCEPT"    => accept
  }

  if xhr
    headers ||= {}
    headers['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
    headers['HTTP_ACCEPT'] ||= [Mime[:js], Mime[:html], Mime[:xml], 'text/xml', '*/*'].join(', ')
  end

  # this modifies the passed request_env directly
  if headers.present?
    Http::Headers.from_hash(request_env).merge!(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
  @response.response_parser = RequestEncoder.parser(@response.content_type)
  @html_document = nil
  @url_options = nil

  @controller = @request.controller_instance

  response.status
end
process_with_kwargs(http_method, path, *args) click to toggle source
# File lib/action_dispatch/testing/integration.rb, line 294
def process_with_kwargs(http_method, path, *args)
  if kwarg_request?(args)
    process(http_method, path, *args)
  else
    non_kwarg_request_warning if args.any?
    process(http_method, path, { params: args[0], headers: args[1] })
  end
end