Class: ActionDispatch::Integration::Session

Inherits:
Object
  • Object
show all
Includes:
Assertions, RequestHelpers, Routing::UrlFor, TestProcess, Test::Unit::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

DEFAULT_HOST =
"www.example.com"

Constants included from Assertions

Assertions::NO_STRIP

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Routing::UrlFor

#url_for, #url_options

Methods included from ActiveSupport::Concern

#append_features, extended, #included

Methods included from Routing::PolymorphicRoutes

#polymorphic_path, #polymorphic_url

Methods included from RequestHelpers

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

Methods included from TestProcess

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

Constructor Details

- (Session) initialize(app)

Create and initialize a new Session instance.



173
174
175
176
177
178
179
180
181
182
183
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 173

def initialize(app)
  @app = app

  # If the app is a Rails app, make url_helpers available on the session
  # This makes app.url_for and app.foo_path available in the console
  if app.respond_to?(:routes) && app.routes.respond_to?(:url_helpers)
    singleton_class.class_eval { include app.routes.url_helpers }
  end

  reset!
end

Instance Attribute Details

- (Object) accept

The Accept header to send.



150
151
152
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 150

def accept
  @accept
end

- (Object) controller (readonly)

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



159
160
161
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 159

def controller
  @controller
end

- (Object) host

The hostname used in the last request.



141
142
143
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 141

def host
  @host || DEFAULT_HOST
end

- (Object) remote_addr

The remote_addr used in the last request.



147
148
149
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 147

def remote_addr
  @remote_addr
end

- (Object) request (readonly)

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



162
163
164
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 162

def request
  @request
end

- (Object) request_count

A running counter of the number of requests processed.



168
169
170
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 168

def request_count
  @request_count
end

- (Object) response (readonly)

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



165
166
167
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 165

def response
  @response
end

Instance Method Details

- (Object) cookies

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



154
155
156
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 154

def cookies
  _mock_session.cookie_jar
end

- (Object) default_url_options



185
186
187
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 185

def default_url_options
  { :host => host, :protocol => https? ? "https" : "http" }
end

- (Object) host!(name)

Set the host name to use in the next request.

session.host! "www.example.com"


236
237
238
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 236

def host!(name)
  @host = name
end

- (Object) https!(flag = true)

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

session.https!
session.https!(false)


220
221
222
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 220

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

- (Boolean) https?

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

if session.https?
  ...
end

Returns:

  • (Boolean)


229
230
231
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 229

def https?
  @https
end

- (Object) reset!

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!


194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 194

def reset!
  @https = false
  @controller = @request = @response = nil
  @_mock_session = nil
  @request_count = 0

  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
    # install the named routes in this session instance.
    klass = singleton_class

    # the helpers are made protected by default--we make them public for
    # easier access during testing and troubleshooting.
    @named_routes_configured = true
  end
end