Class ActionController::Integration::Session
In: vendor/rails/actionpack/lib/action_controller/integration.rb
Parent: Object

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

Test::Unit::Assertions ActionController::Assertions ActionController::TestProcess ControllerCapture

Classes and Modules

Class ActionController::Integration::Session::MultiPartNeededException

Attributes

accept  [RW]  The Accept header to send.
controller  [R]  A reference to the controller instance used by the last request.
cookies  [R]  A map of the cookies returned by the last response, and which will be sent with the next request.
headers  [R]  A map of the headers returned by the last response.
host  [RW]  The hostname used in the last request.
path  [R]  The URI of the last request.
remote_addr  [RW]  The remote_addr used in the last request.
request  [R]  A reference to the request instance used by the last request.
request_count  [RW]  A running counter of the number of requests processed.
response  [R]  A reference to the response instance used by the last request.
status  [R]  The integer HTTP status code of the last request.
status_message  [R]  The status message that accompanied the status code of the last request.

Public Class methods

Create and initialize a new Session instance.

[Source]

    # File vendor/rails/actionpack/lib/action_controller/integration.rb, line 63
63:       def initialize
64:         reset!
65:       end

Public Instance methods

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

[Source]

     # File vendor/rails/actionpack/lib/action_controller/integration.rb, line 199
199:       def delete(path, parameters = nil, headers = nil)
200:         process :delete, path, parameters, headers
201:       end

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

[Source]

     # File vendor/rails/actionpack/lib/action_controller/integration.rb, line 159
159:       def delete_via_redirect(path, parameters = nil, headers = nil)
160:         request_via_redirect(:delete, path, parameters, headers)
161:       end

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.

[Source]

     # File vendor/rails/actionpack/lib/action_controller/integration.rb, line 123
123:       def follow_redirect!
124:         raise "not a redirect! #{@status} #{@status_message}" unless redirect?
125:         get(interpret_uri(headers['location'].first))
126:         status
127:       end

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 AbstractResponse 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.

[Source]

     # File vendor/rails/actionpack/lib/action_controller/integration.rb, line 184
184:       def get(path, parameters = nil, headers = nil)
185:         process :get, path, parameters, headers
186:       end

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

[Source]

     # File vendor/rails/actionpack/lib/action_controller/integration.rb, line 141
141:       def get_via_redirect(path, parameters = nil, headers = nil)
142:         request_via_redirect(:get, path, parameters, headers)
143:       end

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

[Source]

     # File vendor/rails/actionpack/lib/action_controller/integration.rb, line 204
204:       def head(path, parameters = nil, headers = nil)
205:         process :head, path, parameters, headers
206:       end

Set the host name to use in the next request.

  session.host! "www.example.com"

[Source]

     # File vendor/rails/actionpack/lib/action_controller/integration.rb, line 116
116:       def host!(name)
117:         @host = name
118:       end

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

  session.https!
  session.https!(false)

[Source]

     # File vendor/rails/actionpack/lib/action_controller/integration.rb, line 100
100:       def https!(flag=true)
101:         @https = flag
102:       end

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

  if session.https?
    ...
  end

[Source]

     # File vendor/rails/actionpack/lib/action_controller/integration.rb, line 109
109:       def https?
110:         @https
111:       end

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

[Source]

     # File vendor/rails/actionpack/lib/action_controller/integration.rb, line 189
189:       def post(path, parameters = nil, headers = nil)
190:         process :post, path, parameters, headers
191:       end

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

[Source]

     # File vendor/rails/actionpack/lib/action_controller/integration.rb, line 147
147:       def post_via_redirect(path, parameters = nil, headers = nil)
148:         request_via_redirect(:post, path, parameters, headers)
149:       end

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

[Source]

     # File vendor/rails/actionpack/lib/action_controller/integration.rb, line 194
194:       def put(path, parameters = nil, headers = nil)
195:         process :put, path, parameters, headers
196:       end

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

[Source]

     # File vendor/rails/actionpack/lib/action_controller/integration.rb, line 153
153:       def put_via_redirect(path, parameters = nil, headers = nil)
154:         request_via_redirect(:put, path, parameters, headers)
155:       end

Returns true if the last response was a redirect.

[Source]

     # File vendor/rails/actionpack/lib/action_controller/integration.rb, line 164
164:       def redirect?
165:         status/100 == 3
166:       end

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.

[Source]

     # File vendor/rails/actionpack/lib/action_controller/integration.rb, line 133
133:       def request_via_redirect(http_method, path, parameters = nil, headers = nil)
134:         send(http_method, path, parameters, headers)
135:         follow_redirect! while redirect?
136:         status
137:       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!

[Source]

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

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

[Source]

     # File vendor/rails/actionpack/lib/action_controller/integration.rb, line 226
226:       def url_for(options)
227:         controller ? controller.url_for(options) : generic_url_rewriter.rewrite(options)
228:       end
xhr(request_method, path, parameters = nil, headers = nil)

Alias for xml_http_request

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.

[Source]

     # File vendor/rails/actionpack/lib/action_controller/integration.rb, line 215
215:       def xml_http_request(request_method, path, parameters = nil, headers = nil)
216:         headers ||= {}
217:         headers['X-Requested-With'] = 'XMLHttpRequest'
218:         headers['Accept'] ||= 'text/javascript, text/html, application/xml, text/xml, */*'
219: 
220:         process(request_method, path, parameters, headers)
221:       end

[Validate]