Class: ActionDispatch::Integration::Session
- Includes:
- Assertions, RequestHelpers, Routing::UrlFor, TestProcess, Minitest::Assertions
- Defined in:
- actionpack/lib/action_dispatch/testing/integration.rb
Overview
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.
Constant Summary collapse
- DEFAULT_HOST =
"www.example.com"
Constants included from Assertions::ResponseAssertions
Assertions::ResponseAssertions::RESPONSE_PREDICATES
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.
-
#host ⇒ Object
The hostname used in 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.
-
#request_count ⇒ Object
A running counter of the number of requests processed.
-
#response ⇒ Object
readonly
A reference to the response instance used by the last request.
Instance Method Summary collapse
-
#cookies ⇒ Object
A map of the cookies returned by the last response, and which will be sent with the next request.
-
#https!(flag = true) ⇒ Object
Specify whether or not the session should mimic a secure HTTPS request.
-
#https? ⇒ Boolean
Returns
true
if the session is mimicking a secure HTTPS request. -
#initialize(app) ⇒ Session
constructor
Create and initialize a new Session instance.
-
#process(method, path, params: nil, headers: nil, env: nil, xhr: false, as: nil) ⇒ Object
Performs the actual request.
-
#reset! ⇒ Object
Resets the instance.
- #url_options ⇒ Object
Methods included from Routing::UrlFor
#full_url_for, #route_for, #url_for
Methods included from ActiveSupport::Concern
#append_features, #class_methods, extended, #included
Methods included from Routing::PolymorphicRoutes
#polymorphic_path, #polymorphic_url
Methods included from TestProcess
#assigns, #flash, #redirect_to_url, #session
Methods included from TestProcess::FixtureFile
Methods included from RequestHelpers
#delete, #follow_redirect!, #get, #head, #options, #patch, #post, #put
Methods included from Assertions
Methods included from Assertions::RoutingAssertions
#assert_generates, #assert_recognizes, #assert_routing, #method_missing, #setup, #with_routing
Methods included from Assertions::ResponseAssertions
#assert_redirected_to, #assert_response
Constructor Details
#initialize(app) ⇒ Session
Create and initialize a new Session instance.
127 128 129 130 131 132 |
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 127 def initialize(app) super() @app = app reset! end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class ActionDispatch::Assertions::RoutingAssertions
Instance Attribute Details
#accept ⇒ Object
The Accept header to send.
104 105 106 |
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 104 def accept @accept end |
#controller ⇒ Object (readonly)
A reference to the controller instance used by the last request.
113 114 115 |
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 113 def controller @controller end |
#host ⇒ Object
The hostname used in the last request.
95 96 97 |
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 95 def host @host || DEFAULT_HOST end |
#remote_addr ⇒ Object
The remote_addr used in the last request.
101 102 103 |
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 101 def remote_addr @remote_addr end |
#request ⇒ Object (readonly)
A reference to the request instance used by the last request.
116 117 118 |
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 116 def request @request end |
#request_count ⇒ Object
A running counter of the number of requests processed.
122 123 124 |
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 122 def request_count @request_count end |
#response ⇒ Object (readonly)
A reference to the response instance used by the last request.
119 120 121 |
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 119 def response @response end |
Instance Method Details
#cookies ⇒ Object
A map of the cookies returned by the last response, and which will be sent with the next request.
108 109 110 |
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 108 def _mock_session. end |
#https!(flag = true) ⇒ Object
Specify whether or not the session should mimic a secure HTTPS request.
session.https!
session.https!(false)
175 176 177 |
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 175 def https!(flag = true) @https = flag end |
#https? ⇒ Boolean
Returns true
if the session is mimicking a secure HTTPS request.
if session.https?
...
end
184 185 186 |
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 184 def https? @https end |
#process(method, path, params: nil, headers: nil, env: nil, xhr: false, as: nil) ⇒ Object
Performs the actual request.
-
method
: The HTTP method (GET, POST, PATCH, PUT, DELETE, HEAD, OPTIONS) as a symbol. -
path
: The URI (as a String) on which you want to perform the request. -
params
: The HTTP parameters that you want to pass. This may benil
, a Hash, or a String that is appropriately encoded (application/x-www-form-urlencoded
ormultipart/form-data
). -
headers
: Additional headers to pass, as a Hash. The headers will be merged into the Rack env hash. -
env
: Additional env to pass, as a Hash. The headers will be merged into the Rack env hash. -
xhr
: Set to ‘true` if you want to make and Ajax request. Adds request headers characteristic of XMLHttpRequest e.g. HTTP_X_REQUESTED_WITH. The headers will be merged into the Rack env hash. -
as
: Used for encoding the request with different content type. Supports ‘:json` by default and will set the appropriate request headers. The headers will be merged into the Rack env hash.
This method is rarely used directly. Use #get
, #post
, or other standard HTTP methods in integration tests. #process
is only required when using a request method that doesn’t have a method defined in the integration tests.
This method returns the response status, after performing the request. Furthermore, if this method was called from an ActionDispatch::IntegrationTest object, then that object’s @response
instance variable will point to a Response object which one can use to inspect the details of the response.
Example:
process :get, '/author', params: { since: 201501011400 }
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 221 def process(method, path, params: nil, headers: nil, env: nil, xhr: false, as: nil) request_encoder = RequestEncoder.encoder(as) headers ||= {} if method == :get && as == :json && params headers["X-Http-Method-Override"] = "GET" method = :post end if %r{://}.match?(path) path = (path) do |location| https! URI::HTTPS === location if location.scheme if url_host = location.host default = Rack::Request::DEFAULT_PORTS[location.scheme] url_host += ":#{location.port}" if default != location.port host! url_host end end end hostname, port = host.split(":") request_env = { :method => method, :params => request_encoder.encode_params(params), "SERVER_NAME" => hostname, "SERVER_PORT" => port || (https? ? "443" : "80"), "HTTPS" => https? ? "on" : "off", "rack.url_scheme" => https? ? "https" : "http", "REQUEST_URI" => path, "HTTP_HOST" => host, "REMOTE_ADDR" => remote_addr, "CONTENT_TYPE" => request_encoder.content_type, "HTTP_ACCEPT" => request_encoder.accept_header || accept } wrapped_headers = Http::Headers.from_hash({}) wrapped_headers.merge!(headers) if headers if xhr wrapped_headers["HTTP_X_REQUESTED_WITH"] = "XMLHttpRequest" wrapped_headers["HTTP_ACCEPT"] ||= [Mime[:js], Mime[:html], Mime[:xml], "text/xml", "*/*"].join(", ") end # This modifies the passed request_env directly. if wrapped_headers.present? Http::Headers.from_hash(request_env).merge!(wrapped_headers) end if env.present? Http::Headers.from_hash(request_env).merge!(env) end session = Rack::Test::Session.new(_mock_session) # NOTE: rack-test v0.5 doesn't build a default uri correctly # Make sure requested path is always a full URI. session.request(build_full_uri(path, request_env), request_env) @request_count += 1 @request = ActionDispatch::Request.new(session.last_request.env) response = _mock_session.last_response @response = ActionDispatch::TestResponse.from_response(response) @response.request = @request @html_document = nil @url_options = nil @controller = @request.controller_instance response.status 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!
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 151 def reset! @https = false @controller = @request = @response = nil @_mock_session = nil @request_count = 0 @url_options = nil self.host = DEFAULT_HOST 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 defined? @named_routes_configured # the helpers are made protected by default--we make them public for # easier access during testing and troubleshooting. @named_routes_configured = true end end |
#url_options ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 |
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 134 def @url_options ||= .dup.tap do || .reverse_merge!(controller.) if controller if @app.respond_to?(:routes) .reverse_merge!(@app.routes.) end .reverse_merge!(host: host, protocol: https? ? "https" : "http") end end |