Object
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.
Create and initialize a new Session instance.
# File lib/action_dispatch/testing/integration.rb, line 174 174: def initialize(app) 175: super() 176: @app = app 177: 178: # If the app is a Rails app, make url_helpers available on the session 179: # This makes app.url_for and app.foo_path available in the console 180: if app.respond_to?(:routes) 181: singleton_class.class_eval do 182: include app.routes.url_helpers if app.routes.respond_to?(:url_helpers) 183: include app.routes.mounted_helpers if app.routes.respond_to?(:mounted_helpers) 184: end 185: end 186: 187: reset! 188: end
The hostname used in the last request.
# File lib/action_dispatch/testing/integration.rb, line 142 142: def host 143: @host || DEFAULT_HOST 144: 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 231 231: def https!(flag = true) 232: @https = flag 233: end
Return true if the session is mimicking a secure HTTPS request.
if session.https? ... end
# File lib/action_dispatch/testing/integration.rb, line 240 240: def https? 241: @https 242: 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 207 207: def reset! 208: @https = false 209: @controller = @request = @response = nil 210: @_mock_session = nil 211: @request_count = 0 212: @url_options = nil 213: 214: self.host = DEFAULT_HOST 215: self.remote_addr = "127.0.0.1" 216: self.accept = "text/xml,application/xml,application/xhtml+xml," + 217: "text/html;q=0.9,text/plain;q=0.8,image/png," + 218: "*/*;q=0.5" 219: 220: unless defined? @named_routes_configured 221: # the helpers are made protected by default--we make them public for 222: # easier access during testing and troubleshooting. 223: @named_routes_configured = true 224: end 225: end
# File lib/action_dispatch/testing/integration.rb, line 190 190: def url_options 191: @url_options ||= default_url_options.dup.tap do |url_options| 192: url_options.reverse_merge!(controller.url_options) if controller 193: 194: if @app.respond_to?(:routes) && @app.routes.respond_to?(:default_url_options) 195: url_options.reverse_merge!(@app.routes.default_url_options) 196: end 197: 198: url_options.reverse_merge!(:host => host, :protocol => https? ? "https" : "http") 199: end 200: end
# File lib/action_dispatch/testing/integration.rb, line 250 250: def _mock_session 251: @_mock_session ||= Rack::MockSession.new(@app, host) 252: end
Performs the actual request.
# File lib/action_dispatch/testing/integration.rb, line 255 255: def process(method, path, parameters = nil, rack_env = nil) 256: rack_env ||= {} 257: if path =~ %{://} 258: location = URI.parse(path) 259: https! URI::HTTPS === location if location.scheme 260: host! location.host if location.host 261: path = location.query ? "#{location.path}?#{location.query}" : location.path 262: end 263: 264: unless ActionController::Base < ActionController::Testing 265: ActionController::Base.class_eval do 266: include ActionController::Testing 267: end 268: end 269: 270: hostname, port = host.split(':') 271: 272: env = { 273: :method => method, 274: :params => parameters, 275: 276: "SERVER_NAME" => hostname, 277: "SERVER_PORT" => port || (https? ? "443" : "80"), 278: "HTTPS" => https? ? "on" : "off", 279: "rack.url_scheme" => https? ? "https" : "http", 280: 281: "REQUEST_URI" => path, 282: "HTTP_HOST" => host, 283: "REMOTE_ADDR" => remote_addr, 284: "CONTENT_TYPE" => "application/x-www-form-urlencoded", 285: "HTTP_ACCEPT" => accept 286: } 287: 288: session = Rack::Test::Session.new(_mock_session) 289: 290: env.merge!(rack_env) 291: 292: # NOTE: rack-test v0.5 doesn't build a default uri correctly 293: # Make sure requested path is always a full uri 294: uri = URI.parse('/') 295: uri.scheme ||= env['rack.url_scheme'] 296: uri.host ||= env['SERVER_NAME'] 297: uri.port ||= env['SERVER_PORT'].try(:to_i) 298: uri += path 299: 300: session.request(uri.to_s, env) 301: 302: @request_count += 1 303: @request = ActionDispatch::Request.new(session.last_request.env) 304: response = _mock_session.last_response 305: @response = ActionDispatch::TestResponse.new(response.status, response.headers, response.body) 306: @html_document = nil 307: @url_options = nil 308: 309: @controller = session.last_request.env['action_controller.instance'] 310: 311: return response.status 312: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.