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