class ActionDispatch::Integration::Session
Integration::Session directly.
IntegrationTest#open_session, rather than instantiating
Typically, you will instantiate a new session using
limited extent) multiple simultaneous users interacting with your system.
multiple sessions and run them side-by-side, you can also mimic (to some
performed sequentially by some virtual user. Because you can instantiate
An integration Session instance represents a set of requests and responses
def _mock_session
def _mock_session @_mock_session ||= Rack::MockSession.new(@app, host) end
def cookies
A map of the cookies returned by the last response, and which will be
def cookies _mock_session.cookie_jar end
def default_url_options
def default_url_options { :host => host, :protocol => https? ? "https" : "http" } end
def host
def host @host || DEFAULT_HOST end
def host!(name)
Set the host name to use in the next request.
def host!(name) @host = name end
def https!(flag = true)
session.https!
Specify whether or not the session should mimic a secure HTTPS request.
def https!(flag = true) @https = flag end
def https?
...
if session.https?
Return +true+ if the session is mimicking a secure HTTPS request.
def https? @https end
def initialize(app)
def initialize(app) @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) && app.routes.respond_to?(:url_helpers) singleton_class.class_eval { include app.routes.url_helpers } end reset! end
def process(method, path, parameters = nil, rack_environment = nil)
def process(method, path, parameters = nil, rack_environment = nil) if path =~ %r{://} location = URI.parse(path) https! URI::HTTPS === location if location.scheme host! location.host if location.host path = location.query ? "#{location.path}?#{location.query}" : location.path end unless ActionController::Base < ActionController::Testing ActionController::Base.class_eval do include ActionController::Testing end end env = { :method => method, :params => parameters, "SERVER_NAME" => host.split(':')[0], "SERVER_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 } session = Rack::Test::Session.new(_mock_session) (rack_environment || {}).each do |key, value| env[key] = value end # NOTE: rack-test v0.5 doesn't build a default uri correctly # Make sure requested path is always a full uri uri = URI.parse('/') uri.scheme ||= env['rack.url_scheme'] uri.host ||= env['SERVER_NAME'] uri.port ||= env['SERVER_PORT'].try(:to_i) uri += path session.request(uri.to_s, 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 @controller = session.last_request.env['action_controller.instance'] return response.status end
def reset!
condition.
in an existing session instance, so it can be used from a clean-slate
Resets the instance. This can be used to reset the state information
def reset! @https = false @controller = @request = @response = nil @_mock_session = nil @request_count = 0 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 # install the named routes in this session instance. klass = singleton_class # the helpers are made protected by default--we make them public for # easier access during testing and troubleshooting. @named_routes_configured = true end end