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 ActionDispatch::Integration::Runner#open_session, rather than instantiating Integration::Session directly.
Constants
- DEFAULT_HOST
Attributes
The Accept header to send.
A reference to the controller instance used by the last request.
The #remote_addr used in the last request.
A reference to the request instance used by the last request.
A running counter of the number of requests processed.
A reference to the response instance used by the last request.
Public Class Methods
Create and initialize a new Session instance.
# File lib/action_dispatch/testing/integration.rb, line 184 def initialize(app) super() @app = app # If the app is a Rails app, make url_helpers available on the session # This makes app.url_for and app.foo_path available in the console if app.respond_to?(:routes) singleton_class.class_eval do include app.routes.url_helpers include app.routes.mounted_helpers end end reset! end
Public Instance Methods
The hostname used in the last request.
# File lib/action_dispatch/testing/integration.rb, line 152 def host @host || DEFAULT_HOST end
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 241 def https!(flag = true) @https = flag end
Returns true
if the session is mimicking a secure HTTPS
request.
if session.https? ... end
# File lib/action_dispatch/testing/integration.rb, line 250 def https? @https end
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 217 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
# File lib/action_dispatch/testing/integration.rb, line 200 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
# File lib/action_dispatch/testing/integration.rb, line 260 def _mock_session @_mock_session ||= Rack::MockSession.new(@app, host) end
# File lib/action_dispatch/testing/integration.rb, line 311 def build_full_uri(path, env) "#{env['rack.url_scheme']}://#{env['SERVER_NAME']}:#{env['SERVER_PORT']}#{path}" end
Performs the actual request.
# File lib/action_dispatch/testing/integration.rb, line 265 def process(method, path, parameters = nil, headers_or_env = nil) if path =~ %r{://} location = URI.parse(path) https! URI::HTTPS === location if location.scheme host! "#{location.host}:#{location.port}" if location.host path = location.query ? "#{location.path}?#{location.query}" : location.path end hostname, port = host.split(':') env = { :method => method, :params => parameters, "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" => "application/x-www-form-urlencoded", "HTTP_ACCEPT" => accept } # this modifies the passed env directly Http::Headers.new(env).merge!(headers_or_env || {}) 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, env), env) @request_count += 1 @request = ActionDispatch::Request.new(session.last_request.env) response = _mock_session.last_response @response = ActionDispatch::TestResponse.new(response.status, response.headers, response.body) @html_document = nil @url_options = nil @controller = session.last_request.env['action_controller.instance'] return response.status end