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.
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. |
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. |
Performs a DELETE request with the given parameters. See get() for more details.
# File vendor/rails/actionpack/lib/action_controller/integration.rb, line 165 165: def delete(path, parameters=nil, headers=nil) 166: process :delete, path, parameters, headers 167: 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.
# File vendor/rails/actionpack/lib/action_controller/integration.rb, line 113 113: def follow_redirect! 114: raise "not a redirect! #{@status} #{@status_message}" unless redirect? 115: get(interpret_uri(headers["location"].first)) 116: status 117: end
Performs a GET request with the given parameters. The parameters may be nil, a Hash, or a string that is appropriately encoded (application/x-www-form-urlencoded or multipart/form-data). The headers should be a hash. The keys will automatically be upcased, with the prefix ‘HTTP_’ added if needed.
You can also perform POST, PUT, DELETE, and HEAD requests with post, put, delete, and head.
# File vendor/rails/actionpack/lib/action_controller/integration.rb, line 150 150: def get(path, parameters=nil, headers=nil) 151: process :get, path, parameters, headers 152: end
Performs a GET request, 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 vendor/rails/actionpack/lib/action_controller/integration.rb, line 123 123: def get_via_redirect(path, args={}) 124: get path, args 125: follow_redirect! while redirect? 126: status 127: end
Performs a HEAD request with the given parameters. See get() for more details.
# File vendor/rails/actionpack/lib/action_controller/integration.rb, line 170 170: def head(path, parameters=nil, headers=nil) 171: process :head, path, parameters, headers 172: end
Set the host name to use in the next request.
session.host! "www.example.com"
# File vendor/rails/actionpack/lib/action_controller/integration.rb, line 106 106: def host!(name) 107: @host = name 108: end
Specify whether or not the session should mimic a secure HTTPS request.
session.https! session.https!(false)
# File vendor/rails/actionpack/lib/action_controller/integration.rb, line 90 90: def https!(flag=true) 91: @https = flag 92: end
Return true if the session is mimicing a secure HTTPS request.
if session.https? ... end
# File vendor/rails/actionpack/lib/action_controller/integration.rb, line 99 99: def https? 100: @https 101: end
Performs a POST request with the given parameters. See get() for more details.
# File vendor/rails/actionpack/lib/action_controller/integration.rb, line 155 155: def post(path, parameters=nil, headers=nil) 156: process :post, path, parameters, headers 157: end
Performs a POST request, following any subsequent redirect. This is vulnerable to infinite loops, the same as get_via_redirect.
# File vendor/rails/actionpack/lib/action_controller/integration.rb, line 131 131: def post_via_redirect(path, args={}) 132: post path, args 133: follow_redirect! while redirect? 134: status 135: end
Performs a PUT request with the given parameters. See get() for more details.
# File vendor/rails/actionpack/lib/action_controller/integration.rb, line 160 160: def put(path, parameters=nil, headers=nil) 161: process :put, path, parameters, headers 162: end
Returns true if the last response was a redirect.
# File vendor/rails/actionpack/lib/action_controller/integration.rb, line 138 138: def redirect? 139: status/100 == 3 140: 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 vendor/rails/actionpack/lib/action_controller/integration.rb, line 63 63: def reset! 64: @status = @path = @headers = nil 65: @result = @status_message = nil 66: @https = false 67: @cookies = {} 68: @controller = @request = @response = nil 69: 70: self.host = "www.example.com" 71: self.remote_addr = "127.0.0.1" 72: self.accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" 73: 74: unless @named_routes_configured 75: # install the named routes in this session instance. 76: klass = class<<self; self; end 77: Routing::Routes.named_routes.install(klass) 78: 79: # the helpers are made protected by default--we make them public for 80: # easier access during testing and troubleshooting. 81: klass.send(:public, *Routing::Routes.named_routes.helpers) 82: @named_routes_configured = true 83: end 84: end
Returns the URL for the given options, according to the rules specified in the application’s routes.
# File vendor/rails/actionpack/lib/action_controller/integration.rb, line 191 191: def url_for(options) 192: controller ? controller.url_for(options) : generic_url_rewriter.rewrite(options) 193: end
Performs an XMLHttpRequest request with the given parameters, mimicing the request environment created by the Prototype library. The parameters may be nil, a Hash, or a string that is appropriately encoded (application/x-www-form-urlencoded or multipart/form-data). The headers should be a hash. The keys will automatically be upcased, with the prefix ‘HTTP_’ added if needed.
# File vendor/rails/actionpack/lib/action_controller/integration.rb, line 180 180: def xml_http_request(path, parameters=nil, headers=nil) 181: headers = (headers || {}).merge( 182: "X-Requested-With" => "XMLHttpRequest", 183: "Accept" => "text/javascript, text/html, application/xml, text/xml, */*" 184: ) 185: 186: post(path, parameters, headers) 187: end