class ActionDispatch::Integration::Session
rather than instantiating a Session directly.
Typically, you will instantiate a new session using Runner#open_session,
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 a test process. Because you can instantiate
An instance of this class represents a set of requests and responses
def _mock_session
def _mock_session @_mock_session ||= Rack::MockSession.new(@app, host) end
def build_expanded_path(path)
def build_expanded_path(path) location = URI.parse(path) yield location if block_given? path = location.path location.query ? "#{path}?#{location.query}" : path end
def build_full_uri(path, env)
def build_full_uri(path, env) "#{env['rack.url_scheme']}://#{env['SERVER_NAME']}:#{env['SERVER_PORT']}#{path}" 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 host
def host @host || DEFAULT_HOST 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?
Returns +true+ if the session is mimicking a secure HTTPS request.
def https? @https end
def initialize(app)
def initialize(app) super() @app = app reset! end
def process(method, path, params: nil, headers: nil, env: nil, xhr: false, as: nil)
Example:
which one can use to inspect the details of the response.
then that object's @response instance variable will point to a Response object
Furthermore, if this method was called from an ActionDispatch::IntegrationTest object,
This method returns the response status, after performing the request.
doesn't have a method defined in the integration tests.
tests. +#process+ is only required when using a request method that
RequestHelpers#post, or other standard HTTP methods in integration
This method is rarely used directly. Use RequestHelpers#get,
The headers will be merged into the Rack env hash.
Supports +:json+ by default and will set the appropriate request headers.
- +as+: Used for encoding the request with different content type.
The headers will be merged into the Rack env hash.
Adds request headers characteristic of XMLHttpRequest e.g. HTTP_X_REQUESTED_WITH.
- +xhr+: Set to +true+ if you want to make an Ajax request.
merged into the Rack env hash.
- +env+: Additional env to pass, as a Hash. The headers will be
merged into the Rack env hash.
- +headers+: Additional headers to pass, as a Hash. The headers will be
multipart/form-data).
(application/x-www-form-urlencoded or
a Hash, or a String that is appropriately encoded
be +nil+,
- +params+: The HTTP parameters that you want to pass. This may
request.
- +path+: The URI (as a String) on which you want to perform the
as a symbol.
- +method+: The HTTP method (GET, POST, PATCH, PUT, DELETE, HEAD, OPTIONS)
Performs the actual request.
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
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 @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
def url_options
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