Class: ActionDispatch::Integration::Session
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
self,
::ActionDispatch::Routing::UrlFor ,
::ActionDispatch::Routing::PolymorphicRoutes ,
::ActionDispatch::TestProcess ,
::ActionDispatch::TestProcess::FixtureFile ,
RequestHelpers ,
::ActionDispatch::Assertions ,
Rails::Dom::Testing::Assertions,
::ActionDispatch::Assertions::RoutingAssertions ,
::ActionDispatch::Assertions::ResponseAssertions ,
Minitest::Assertions
|
|
Inherits: | Object |
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 Runner#open_session, rather than instantiating a Session
directly.
Constant Summary
-
DEFAULT_HOST =
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 92"www.example.com"
::ActionDispatch::Assertions::ResponseAssertions
- Included
::ActionDispatch::Routing::UrlFor
- Attributes & Methods
Class Method Summary
-
.new(app) ⇒ Session
constructor
Create and initialize a new
Session
instance.
Instance Attribute Summary
-
#accept
rw
The Accept header to send.
- #body readonly
-
#controller
readonly
A reference to the controller instance used by the last request.
- #headers readonly
-
#host
rw
The hostname used in the last request.
- #host=(value) (also: #host!) rw
-
#https? ⇒ Boolean
readonly
Returns
true
if the session is mimicking a secure HTTPS request. - #path readonly
-
#remote_addr
rw
The remote_addr used in the last request.
-
#request
readonly
A reference to the request instance used by the last request.
-
#request_count
rw
A running counter of the number of requests processed.
-
#response
readonly
A reference to the response instance used by the last request.
- #status readonly
- #status_message readonly
::ActionDispatch::Routing::UrlFor
- Included
Instance Method Summary
-
#cookies
A map of the cookies returned by the last response, and which will be sent with the next request.
-
#https!(flag = true)
Specify whether or not the session should mimic a secure HTTPS request.
-
#process(method, path, params: nil, headers: nil, env: nil, xhr: false, as: nil)
Performs the actual request.
- #redirect? ⇒ Boolean
-
#reset!
Resets the instance.
- #url_options
- #_mock_session private
- #build_expanded_path(path) {|location| ... } private
- #build_full_uri(path, env) private
::ActionDispatch::Routing::UrlFor
- Included
#initialize, | |
#route_for | Allows calling direct or regular named route. |
#url_for | Generate a URL based on the options provided, .default_url_options, and the routes defined in |
#url_options | Hook overridden in controller to add request information with .default_url_options. |
#_routes_context, #_with_routes, #full_url_for |
::ActionDispatch::Routing::PolymorphicRoutes
- Included
#polymorphic_path | Returns the path component of a URL for the given record. |
#polymorphic_url | Constructs a call to a named RESTful route for the given record and returns the resulting URL string. |
#polymorphic_mapping, #polymorphic_path_for_action, #polymorphic_url_for_action |
::ActionDispatch::TestProcess
- Included
::ActionDispatch::TestProcess::FixtureFile
- Included
#file_fixture_upload | Shortcut for ‘Rack::Test::UploadedFile.new(File.join(ActionDispatch::IntegrationTest.file_f ixture_path, path), type)`: |
#fixture_file_upload | Alias for TestProcess::FixtureFile#file_fixture_upload. |
RequestHelpers
- Included
#delete | Performs a DELETE request with the given parameters. |
#follow_redirect! | Follow a single redirect response. |
#get | Performs a GET request with the given parameters. |
#head | Performs a HEAD request with the given parameters. |
#options | Performs an OPTIONS request with the given parameters. |
#patch | Performs a PATCH request with the given parameters. |
#post | Performs a POST request with the given parameters. |
#put | Performs a PUT request with the given parameters. |
::ActionDispatch::Assertions
- Included
::ActionDispatch::Assertions::RoutingAssertions
- Included
#assert_generates | Asserts that the provided options can be used to generate the provided path. |
#assert_recognizes | |
#assert_routing | |
#method_missing | ROUTES TODO: These assertions should really work in an integration context. |
#with_routing | A helper to make it easier to test different route configurations. |
#create_routes, #fail_on, | |
#recognized_request_for | Recognizes the route for a given path. |
#reset_routes, #setup |
::ActionDispatch::Assertions::ResponseAssertions
- Included
#assert_redirected_to | Asserts that the response is a redirect to a URL matching the given options. |
#assert_response | Asserts that the response is one of the following types: |
#code_with_name, #exception_if_present, #generate_response_message, #location_if_redirected, #normalize_argument_to_redirection, | |
#parameterize | Proxy to to_param if the object will respond to it. |
#response_body_if_short |
Constructor Details
.new(app) ⇒ Session
Create and initialize a new Session
instance.
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 133
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
Class Attribute Details
.default_url_options (rw)
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/url_for.rb', line 100
class_attribute :
.default_url_options? ⇒ Boolean
(rw)
[ GitHub ]
# File 'actionpack/lib/action_dispatch/routing/url_for.rb', line 100
class_attribute :
Instance Attribute Details
#accept (rw)
The Accept header to send.
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 110
attr_accessor :accept
#body (readonly)
[ GitHub ]#controller (readonly)
A reference to the controller instance used by the last request.
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 119
attr_reader :controller
#default_url_options (rw)
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/url_for.rb', line 100
class_attribute :
#default_url_options? ⇒ Boolean
(rw)
[ GitHub ]
# File 'actionpack/lib/action_dispatch/routing/url_for.rb', line 100
class_attribute :
#headers (readonly)
[ GitHub ]#host (rw)
The hostname used in the last request.
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 101
def host @host || DEFAULT_HOST end
#host=(value) (rw) Also known as: #host!
[ GitHub ]# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 104
attr_writer :host
#https? ⇒ Boolean
(readonly)
Returns true
if the session is mimicking a secure HTTPS request.
if session.https?
#...
end
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 189
def https? @https end
#path (readonly)
[ GitHub ]# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 98
delegate :path, to: :request, allow_nil: true
#remote_addr (rw)
The remote_addr used in the last request.
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 107
attr_accessor :remote_addr
#request (readonly)
A reference to the request instance used by the last request.
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 122
attr_reader :request
#request_count (rw)
A running counter of the number of requests processed.
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 128
attr_accessor :request_count
#response (readonly)
A reference to the response instance used by the last request.
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 125
attr_reader :response
#status (readonly)
[ GitHub ]#status_message (readonly)
[ GitHub ]Instance Method Details
#_mock_session (private)
[ GitHub ]#build_expanded_path(path) {|location| ... } (private)
#build_full_uri(path, env) (private)
[ GitHub ]#cookies
A map of the cookies returned by the last response, and which will be sent with the next request.
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 114
def _mock_session. end
#https!(flag = true)
Specify whether or not the session should mimic a secure HTTPS request.
session.https!
session.https!(false)
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 180
def https!(flag = true) @https = flag end
#process(method, path, params: nil, headers: nil, env: nil, xhr: false, as: nil)
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 totrue
if you want to make an 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 RequestHelpers#get, RequestHelpers#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 ::ActionDispatch::Response
object which one can use to inspect the details of the response.
Example:
process :get, '/author', params: { since: 201501011400 }
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 225
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 path.include?("://") 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, "HTTP_ACCEPT" => request_encoder.accept_header || accept } if request_encoder.content_type request_env["CONTENT_TYPE"] = request_encoder.content_type end 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. uri = build_full_uri(path, request_env) if method == :get && String === request_env[:params] # rack-test will needlessly parse and rebuild a :params # querystring, using Rack's query parser. At best that's a # waste of time; at worst it can change the value. uri << "?" << request_env.delete(:params) end session.request(uri, 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
#redirect? ⇒ Boolean
#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!
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 156
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
[ GitHub ]# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 140
def @url_options ||= .dup.tap do || .reverse_merge!(controller. ) if controller.respond_to?(: ) if @app.respond_to?(:routes) .reverse_merge!(@app.routes. ) end .reverse_merge!(host: host, protocol: https? ? "https" : "http") end end