Class: ActionController::Integration::Session
- Includes:
- Assertions, TestProcess, Test::Unit::Assertions
- Defined in:
- lib/action_controller/integration.rb
Overview
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.
Defined Under Namespace
Classes: MockCGI
Constant Summary
Constants included from Assertions
Instance Attribute Summary collapse
-
#accept ⇒ Object
The Accept header to send.
-
#controller ⇒ Object
readonly
A reference to the controller instance used by the last request.
-
#cookies ⇒ Object
readonly
A map of the cookies returned by the last response, and which will be sent with the next request.
-
#headers ⇒ Object
readonly
A map of the headers returned by the last response.
-
#host ⇒ Object
The hostname used in the last request.
-
#path ⇒ Object
readonly
The URI of the last request.
-
#remote_addr ⇒ Object
The remote_addr used in the last request.
-
#request ⇒ Object
readonly
A reference to the request instance used by the last request.
-
#response ⇒ Object
readonly
A reference to the response instance used by the last request.
-
#status ⇒ Object
readonly
The integer HTTP status code of the last request.
-
#status_message ⇒ Object
readonly
The status message that accompanied the status code of the last request.
Instance Method Summary collapse
-
#delete(path, parameters = nil, headers = nil) ⇒ Object
Performs a DELETE request with the given parameters.
-
#follow_redirect! ⇒ Object
Follow a single redirect response.
-
#get(path, parameters = nil, headers = nil) ⇒ Object
Performs a GET request with the given parameters.
-
#get_via_redirect(path, args = {}) ⇒ Object
Performs a GET request, following any subsequent redirect.
-
#head(path, parameters = nil, headers = nil) ⇒ Object
Performs a HEAD request with the given parameters.
-
#host!(name) ⇒ Object
Set the host name to use in the next request.
-
#https!(flag = true) ⇒ Object
Specify whether or not the session should mimic a secure HTTPS request.
-
#https? ⇒ Boolean
Return
true
if the session is mimicing a secure HTTPS request. -
#initialize ⇒ Session
constructor
Create an initialize a new Session instance.
-
#post(path, parameters = nil, headers = nil) ⇒ Object
Performs a POST request with the given parameters.
-
#post_via_redirect(path, args = {}) ⇒ Object
Performs a POST request, following any subsequent redirect.
-
#put(path, parameters = nil, headers = nil) ⇒ Object
Performs a PUT request with the given parameters.
-
#redirect? ⇒ Boolean
Returns
true
if the last response was a redirect. -
#reset! ⇒ Object
Resets the instance.
-
#url_for(options) ⇒ Object
Returns the URL for the given options, according to the rules specified in the application’s routes.
-
#xml_http_request(path, parameters = nil, headers = nil) ⇒ Object
Performs an XMLHttpRequest request with the given parameters, mimicing the request environment created by the Prototype library.
Methods included from TestProcess
#assigns, #build_request_uri, #find_all_tag, #find_tag, #fixture_file_upload, #flash, #follow_redirect, #html_document, included, #method_missing, #redirect_to_url, #session, #with_routing
Methods included from Assertions
Constructor Details
#initialize ⇒ Session
Create an initialize a new Session instance.
55 56 57 |
# File 'lib/action_controller/integration.rb', line 55 def initialize reset! end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class ActionController::TestProcess
Instance Attribute Details
#accept ⇒ Object
The Accept header to send.
36 37 38 |
# File 'lib/action_controller/integration.rb', line 36 def accept @accept end |
#controller ⇒ Object (readonly)
A reference to the controller instance used by the last request.
46 47 48 |
# File 'lib/action_controller/integration.rb', line 46 def controller @controller end |
#cookies ⇒ Object (readonly)
A map of the cookies returned by the last response, and which will be sent with the next request.
40 41 42 |
# File 'lib/action_controller/integration.rb', line 40 def @cookies end |
#headers ⇒ Object (readonly)
A map of the headers returned by the last response.
43 44 45 |
# File 'lib/action_controller/integration.rb', line 43 def headers @headers end |
#host ⇒ Object
The hostname used in the last request.
30 31 32 |
# File 'lib/action_controller/integration.rb', line 30 def host @host end |
#path ⇒ Object (readonly)
The URI of the last request.
27 28 29 |
# File 'lib/action_controller/integration.rb', line 27 def path @path end |
#remote_addr ⇒ Object
The remote_addr used in the last request.
33 34 35 |
# File 'lib/action_controller/integration.rb', line 33 def remote_addr @remote_addr end |
#request ⇒ Object (readonly)
A reference to the request instance used by the last request.
49 50 51 |
# File 'lib/action_controller/integration.rb', line 49 def request @request end |
#response ⇒ Object (readonly)
A reference to the response instance used by the last request.
52 53 54 |
# File 'lib/action_controller/integration.rb', line 52 def response @response end |
#status ⇒ Object (readonly)
The integer HTTP status code of the last request.
21 22 23 |
# File 'lib/action_controller/integration.rb', line 21 def status @status end |
#status_message ⇒ Object (readonly)
The status message that accompanied the status code of the last request.
24 25 26 |
# File 'lib/action_controller/integration.rb', line 24 def @status_message end |
Instance Method Details
#delete(path, parameters = nil, headers = nil) ⇒ Object
Performs a DELETE request with the given parameters. See get() for more details.
166 167 168 |
# File 'lib/action_controller/integration.rb', line 166 def delete(path, parameters=nil, headers=nil) process :delete, path, parameters, headers end |
#follow_redirect! ⇒ Object
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.
114 115 116 117 118 |
# File 'lib/action_controller/integration.rb', line 114 def follow_redirect! raise "not a redirect! #{@status} #{@status_message}" unless redirect? get(interpret_uri(headers['location'].first)) status end |
#get(path, parameters = nil, headers = nil) ⇒ Object
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.
151 152 153 |
# File 'lib/action_controller/integration.rb', line 151 def get(path, parameters=nil, headers=nil) process :get, path, parameters, headers end |
#get_via_redirect(path, args = {}) ⇒ Object
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.
124 125 126 127 128 |
# File 'lib/action_controller/integration.rb', line 124 def get_via_redirect(path, args={}) get path, args follow_redirect! while redirect? status end |
#head(path, parameters = nil, headers = nil) ⇒ Object
Performs a HEAD request with the given parameters. See get() for more details.
171 172 173 |
# File 'lib/action_controller/integration.rb', line 171 def head(path, parameters=nil, headers=nil) process :head, path, parameters, headers end |
#host!(name) ⇒ Object
Set the host name to use in the next request.
session.host! "www.example.com"
107 108 109 |
# File 'lib/action_controller/integration.rb', line 107 def host!(name) @host = name end |
#https!(flag = true) ⇒ Object
Specify whether or not the session should mimic a secure HTTPS request.
session.https!
session.https!(false)
91 92 93 |
# File 'lib/action_controller/integration.rb', line 91 def https!(flag=true) @https = flag end |
#https? ⇒ Boolean
Return true
if the session is mimicing a secure HTTPS request.
if session.https?
...
end
100 101 102 |
# File 'lib/action_controller/integration.rb', line 100 def https? @https end |
#post(path, parameters = nil, headers = nil) ⇒ Object
Performs a POST request with the given parameters. See get() for more details.
156 157 158 |
# File 'lib/action_controller/integration.rb', line 156 def post(path, parameters=nil, headers=nil) process :post, path, parameters, headers end |
#post_via_redirect(path, args = {}) ⇒ Object
Performs a POST request, following any subsequent redirect. This is vulnerable to infinite loops, the same as #get_via_redirect.
132 133 134 135 136 |
# File 'lib/action_controller/integration.rb', line 132 def post_via_redirect(path, args={}) post path, args follow_redirect! while redirect? status end |
#put(path, parameters = nil, headers = nil) ⇒ Object
Performs a PUT request with the given parameters. See get() for more details.
161 162 163 |
# File 'lib/action_controller/integration.rb', line 161 def put(path, parameters=nil, headers=nil) process :put, path, parameters, headers end |
#redirect? ⇒ Boolean
Returns true
if the last response was a redirect.
139 140 141 |
# File 'lib/action_controller/integration.rb', line 139 def redirect? status/100 == 3 end |
#reset! ⇒ Object
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!
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/action_controller/integration.rb', line 64 def reset! @status = @path = @headers = nil @result = @status_message = nil @https = false @cookies = {} @controller = @request = @response = nil self.host = "www.example.com" self.remote_addr = "127.0.0.1" self.accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" unless @named_routes_configured # install the named routes in this session instance. klass = class<<self; self; end Routing::Routes.named_routes.install(klass) # the helpers are made protected by default--we make them public for # easier access during testing and troubleshooting. klass.send(:public, *Routing::Routes.named_routes.helpers) @named_routes_configured = true end end |
#url_for(options) ⇒ Object
Returns the URL for the given options, according to the rules specified in the application’s routes.
192 193 194 |
# File 'lib/action_controller/integration.rb', line 192 def url_for() controller ? controller.url_for() : generic_url_rewriter.rewrite() end |
#xml_http_request(path, parameters = nil, headers = nil) ⇒ Object
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.
181 182 183 184 185 186 187 188 |
# File 'lib/action_controller/integration.rb', line 181 def xml_http_request(path, parameters=nil, headers=nil) headers = (headers || {}).merge( "X-Requested-With" => "XMLHttpRequest", "Accept" => "text/javascript, text/html, application/xml, text/xml, */*" ) post(path, parameters, headers) end |