Parent

Included Modules

Class Index [+]

Quicksearch

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 IntegrationTest#open_session, rather than instantiating Integration::Session directly.

Constants

DEFAULT_HOST

Attributes

host[W]
remote_addr[RW]

The remote_addr used in the last request.

accept[RW]

The Accept header to send.

controller[R]

A reference to the controller instance used by the last request.

request[R]

A reference to the request instance used by the last request.

response[R]

A reference to the response instance used by the last request.

request_count[RW]

A running counter of the number of requests processed.

Public Class Methods

new(app) click to toggle source

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

Public Instance Methods

cookies() click to toggle source

A map of the cookies returned by the last response, and which will be sent with the next request.

     # File lib/action_dispatch/testing/integration.rb, line 154
154:       def cookies
155:         _mock_session.cookie_jar
156:       end
default_url_options() click to toggle source
     # File lib/action_dispatch/testing/integration.rb, line 185
185:       def default_url_options
186:         { :host => host, :protocol => https? ? "https" : "http" }
187:       end
host() click to toggle source

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
host!(name) click to toggle source

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
https!(flag = true) click to toggle source

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
https?() click to toggle source

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
reset!() click to toggle source

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

Private Instance Methods

_mock_session() click to toggle source
     # File lib/action_dispatch/testing/integration.rb, line 241
241:         def _mock_session
242:           @_mock_session ||= Rack::MockSession.new(@app, host)
243:         end
process(method, path, parameters = nil, rack_environment = nil) click to toggle source

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.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.