An integration Session instance represents a set of requests and responses performed sequentially by some virtual user. 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.

Methods
Included Modules
Classes and Modules
Class ActionController::Integration::Session::MultiPartNeededException
Attributes
[RW] accept The Accept header to send.
[RW] application Rack application to use
[R] body The body of the last request.
[R] controller A reference to the controller instance used by the last request.
[R] cookies A map of the cookies returned by the last response, and which will be sent with the next request.
[R] headers A map of the headers returned by the last response.
[RW] host The hostname used in the last request.
[R] path The URI of the last request.
[RW] remote_addr The remote_addr used in the last request.
[R] request A reference to the request instance used by the last request.
[RW] request_count A running counter of the number of requests processed.
[R] response A reference to the response instance used by the last request.
[R] status The integer HTTP status code of the last request.
[R] status_message The status message that accompanied the status code of the last request.
Public Class methods
new(app = nil)

Create and initialize a new Session instance.

    # File actionpack/lib/action_controller/integration.rb, line 68
68:       def initialize(app = nil)
69:         @application = app || ActionController::Dispatcher.new
70:         reset!
71:       end
Public Instance methods
delete(path, parameters = nil, headers = nil)

Performs a DELETE request with the given parameters. See get() for more details.

     # File actionpack/lib/action_controller/integration.rb, line 214
214:       def delete(path, parameters = nil, headers = nil)
215:         process :delete, path, parameters, headers
216:       end
delete_via_redirect(path, parameters = nil, headers = nil)

Performs a DELETE request, following any subsequent redirect. See request_via_redirect for more information.

     # File actionpack/lib/action_controller/integration.rb, line 167
167:       def delete_via_redirect(path, parameters = nil, headers = nil)
168:         request_via_redirect(:delete, path, parameters, headers)
169:       end
follow_redirect!()

Follow a single redirect response. If the last response was not a redirect, an exception will be raised. Otherwise, the redirect is performed on the location header.

     # File actionpack/lib/action_controller/integration.rb, line 131
131:       def follow_redirect!
132:         raise "not a redirect! #{@status} #{@status_message}" unless redirect?
133:         get(interpret_uri(headers['location']))
134:         status
135:       end
get(path, parameters = nil, headers = nil)

Performs a GET request with the given parameters.

  • path: The URI (as a String) on which you want to perform a GET request.
  • parameters: The HTTP parameters that you want to pass. This may be nil, a Hash, or a String that is appropriately encoded (application/x-www-form-urlencoded or multipart/form-data).
  • headers: Additional HTTP headers to pass, as a Hash. The keys will automatically be upcased, with the prefix ‘HTTP_’ added if needed.

This method returns an Response object, which one can use to inspect the details of the response. Furthermore, if this method was called from an ActionController::IntegrationTest object, then that object‘s @response instance variable will point to the same response object.

You can also perform POST, PUT, DELETE, and HEAD requests with post, put, delete, and head.

     # File actionpack/lib/action_controller/integration.rb, line 196
196:       def get(path, parameters = nil, headers = nil)
197:         process :get, path, parameters, headers
198:       end
get_via_redirect(path, parameters = nil, headers = nil)

Performs a GET request, following any subsequent redirect. See request_via_redirect for more information.

     # File actionpack/lib/action_controller/integration.rb, line 149
149:       def get_via_redirect(path, parameters = nil, headers = nil)
150:         request_via_redirect(:get, path, parameters, headers)
151:       end
head(path, parameters = nil, headers = nil)

Performs a HEAD request with the given parameters. See get() for more details.

     # File actionpack/lib/action_controller/integration.rb, line 220
220:       def head(path, parameters = nil, headers = nil)
221:         process :head, path, parameters, headers
222:       end
host!(name)

Set the host name to use in the next request.

  session.host! "www.example.com"
     # File actionpack/lib/action_controller/integration.rb, line 124
124:       def host!(name)
125:         @host = name
126:       end
https!(flag = true)

Specify whether or not the session should mimic a secure HTTPS request.

  session.https!
  session.https!(false)
     # File actionpack/lib/action_controller/integration.rb, line 108
108:       def https!(flag = true)
109:         @https = flag
110:       end
https?()

Return true if the session is mimicking a secure HTTPS request.

  if session.https?
    ...
  end
     # File actionpack/lib/action_controller/integration.rb, line 117
117:       def https?
118:         @https
119:       end
post(path, parameters = nil, headers = nil)

Performs a POST request with the given parameters. See get() for more details.

     # File actionpack/lib/action_controller/integration.rb, line 202
202:       def post(path, parameters = nil, headers = nil)
203:         process :post, path, parameters, headers
204:       end
post_via_redirect(path, parameters = nil, headers = nil)

Performs a POST request, following any subsequent redirect. See request_via_redirect for more information.

     # File actionpack/lib/action_controller/integration.rb, line 155
155:       def post_via_redirect(path, parameters = nil, headers = nil)
156:         request_via_redirect(:post, path, parameters, headers)
157:       end
put(path, parameters = nil, headers = nil)

Performs a PUT request with the given parameters. See get() for more details.

     # File actionpack/lib/action_controller/integration.rb, line 208
208:       def put(path, parameters = nil, headers = nil)
209:         process :put, path, parameters, headers
210:       end
put_via_redirect(path, parameters = nil, headers = nil)

Performs a PUT request, following any subsequent redirect. See request_via_redirect for more information.

     # File actionpack/lib/action_controller/integration.rb, line 161
161:       def put_via_redirect(path, parameters = nil, headers = nil)
162:         request_via_redirect(:put, path, parameters, headers)
163:       end
redirect?()

Returns true if the last response was a redirect.

     # File actionpack/lib/action_controller/integration.rb, line 172
172:       def redirect?
173:         status/100 == 3
174:       end
request_via_redirect(http_method, path, parameters = nil, headers = nil)

Performs a request using the specified method, following any subsequent redirect. Note that the redirects are followed until the response is not a redirect—this means you may run into an infinite loop if your redirect loops back to itself.

     # File actionpack/lib/action_controller/integration.rb, line 141
141:       def request_via_redirect(http_method, path, parameters = nil, headers = nil)
142:         send(http_method, path, parameters, headers)
143:         follow_redirect! while redirect?
144:         status
145:       end
reset!()

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 actionpack/lib/action_controller/integration.rb, line 78
 78:       def reset!
 79:         @status = @path = @headers = nil
 80:         @result = @status_message = nil
 81:         @https = false
 82:         @cookies = {}
 83:         @controller = @request = @response = nil
 84:         @request_count = 0
 85: 
 86:         self.host        = "www.example.com"
 87:         self.remote_addr = "127.0.0.1"
 88:         self.accept      = "text/xml,application/xml,application/xhtml+xml," +
 89:                            "text/html;q=0.9,text/plain;q=0.8,image/png," +
 90:                            "*/*;q=0.5"
 91: 
 92:         unless defined? @named_routes_configured
 93:           # install the named routes in this session instance.
 94:           klass = class << self; self; end
 95:           Routing::Routes.install_helpers(klass)
 96: 
 97:           # the helpers are made protected by default--we make them public for
 98:           # easier access during testing and troubleshooting.
 99:           klass.module_eval { public *Routing::Routes.named_routes.helpers }
100:           @named_routes_configured = true
101:         end
102:       end
url_for(options)

Returns the URL for the given options, according to the rules specified in the application‘s routes.

     # File actionpack/lib/action_controller/integration.rb, line 241
241:       def url_for(options)
242:         controller ?
243:           controller.url_for(options) :
244:           generic_url_rewriter.rewrite(options)
245:       end
xhr(request_method, path, parameters = nil, headers = nil)

Alias for xml_http_request

xml_http_request(request_method, path, parameters = nil, headers = nil)

Performs an XMLHttpRequest request with the given parameters, mirroring a request from the Prototype library.

The request_method is :get, :post, :put, :delete or :head; the parameters are nil, a hash, or a url-encoded or multipart string; the headers are a hash. Keys are automatically upcased and prefixed with ‘HTTP_’ if not already.

This method is also aliased as xhr
     # File actionpack/lib/action_controller/integration.rb, line 231
231:       def xml_http_request(request_method, path, parameters = nil, headers = nil)
232:         headers ||= {}
233:         headers['X-Requested-With'] = 'XMLHttpRequest'
234:         headers['Accept'] ||= [Mime::JS, Mime::HTML, Mime::XML, 'text/xml', Mime::ALL].join(', ')
235:         process(request_method, path, parameters, headers)
236:       end