Class: ActionDispatch::Integration::Session

Inherits:
Object
  • Object
show all
Includes:
Assertions, RequestHelpers, Routing::UrlFor, TestProcess, Minitest::Assertions
Defined in:
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.

Defined Under Namespace

Classes: RequestEncoder

Constant Summary collapse

DEFAULT_HOST =
"www.example.com"

Constants included from Assertions::ResponseAssertions

Assertions::ResponseAssertions::RESPONSE_PREDICATES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Routing::UrlFor

#url_for

Methods included from Routing::PolymorphicRoutes

#polymorphic_path, #polymorphic_url

Methods included from TestProcess

#assigns, #fixture_file_upload, #flash, #redirect_to_url, #session

Methods included from RequestHelpers

#delete, #delete_via_redirect, #follow_redirect!, #get, #get_via_redirect, #head, #patch, #patch_via_redirect, #post, #post_via_redirect, #put, #put_via_redirect, #request_via_redirect, #xml_http_request

Methods included from Assertions

#html_document

Methods included from Assertions::RoutingAssertions

#assert_generates, #assert_recognizes, #assert_routing, #method_missing, #with_routing

Methods included from Assertions::ResponseAssertions

#assert_redirected_to, #assert_response

Constructor Details

#initialize(app) ⇒ Session

Create and initialize a new Session instance.



223
224
225
226
227
228
# File 'lib/action_dispatch/testing/integration.rb', line 223

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

#acceptObject

The Accept header to send.



200
201
202
# File 'lib/action_dispatch/testing/integration.rb', line 200

def accept
  @accept
end

#controllerObject (readonly)

A reference to the controller instance used by the last request.



209
210
211
# File 'lib/action_dispatch/testing/integration.rb', line 209

def controller
  @controller
end

#hostObject

The hostname used in the last request.



191
192
193
# File 'lib/action_dispatch/testing/integration.rb', line 191

def host
  @host || DEFAULT_HOST
end

#remote_addrObject

The remote_addr used in the last request.



197
198
199
# File 'lib/action_dispatch/testing/integration.rb', line 197

def remote_addr
  @remote_addr
end

#requestObject (readonly)

A reference to the request instance used by the last request.



212
213
214
# File 'lib/action_dispatch/testing/integration.rb', line 212

def request
  @request
end

#request_countObject

A running counter of the number of requests processed.



218
219
220
# File 'lib/action_dispatch/testing/integration.rb', line 218

def request_count
  @request_count
end

#responseObject (readonly)

A reference to the response instance used by the last request.



215
216
217
# File 'lib/action_dispatch/testing/integration.rb', line 215

def response
  @response
end

Instance Method Details

#cookiesObject

A map of the cookies returned by the last response, and which will be sent with the next request.



204
205
206
# File 'lib/action_dispatch/testing/integration.rb', line 204

def cookies
  _mock_session.cookie_jar
end

#https!(flag = true) ⇒ Object

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

session.https!
session.https!(false)


271
272
273
# File 'lib/action_dispatch/testing/integration.rb', line 271

def https!(flag = true)
  @https = flag
end

#https?Boolean

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

if session.https?
  ...
end

Returns:

  • (Boolean)


280
281
282
# File 'lib/action_dispatch/testing/integration.rb', line 280

def https?
  @https
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!


247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/action_dispatch/testing/integration.rb', line 247

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_optionsObject



230
231
232
233
234
235
236
237
238
239
240
# File 'lib/action_dispatch/testing/integration.rb', line 230

def url_options
  @url_options ||= default_url_options.dup.tap do |url_options|
    url_options.reverse_merge!(controller.url_options) if controller

    if @app.respond_to?(:routes)
      url_options.reverse_merge!(@app.routes.default_url_options)
    end

    url_options.reverse_merge!(:host => host, :protocol => https? ? "https" : "http")
  end
end