2008-10-27 07:47:01 +01:00
|
|
|
require 'stringio'
|
|
|
|
require 'uri'
|
2009-02-04 21:26:08 +01:00
|
|
|
require 'active_support/test_case'
|
2009-12-01 02:38:34 +01:00
|
|
|
require 'action_controller/rack_lint_patch'
|
2008-10-27 07:47:01 +01:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
module ActionController
|
|
|
|
module Integration #:nodoc:
|
|
|
|
# An integration Session instance represents a set of requests and responses
|
2009-03-16 15:55:30 +01:00
|
|
|
# performed sequentially by some virtual user. Because you can instantiate
|
2007-01-22 14:43:50 +01:00
|
|
|
# multiple sessions and run them side-by-side, you can also mimic (to some
|
|
|
|
# limited extent) multiple simultaneous users interacting with your system.
|
|
|
|
#
|
2009-02-04 21:26:08 +01:00
|
|
|
# Typically, you will instantiate a new session using
|
|
|
|
# IntegrationTest#open_session, rather than instantiating
|
|
|
|
# Integration::Session directly.
|
2007-01-22 14:43:50 +01:00
|
|
|
class Session
|
|
|
|
include Test::Unit::Assertions
|
2009-02-04 21:26:08 +01:00
|
|
|
include ActionController::TestCase::Assertions
|
2007-01-22 14:43:50 +01:00
|
|
|
include ActionController::TestProcess
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
# Rack application to use
|
|
|
|
attr_accessor :application
|
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
# The integer HTTP status code of the last request.
|
|
|
|
attr_reader :status
|
|
|
|
|
|
|
|
# The status message that accompanied the status code of the last request.
|
|
|
|
attr_reader :status_message
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
# The body of the last request.
|
|
|
|
attr_reader :body
|
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
# The URI of the last request.
|
|
|
|
attr_reader :path
|
|
|
|
|
|
|
|
# The hostname used in the last request.
|
|
|
|
attr_accessor :host
|
|
|
|
|
|
|
|
# The remote_addr used in the last request.
|
|
|
|
attr_accessor :remote_addr
|
|
|
|
|
|
|
|
# The Accept header to send.
|
|
|
|
attr_accessor :accept
|
|
|
|
|
|
|
|
# A map of the cookies returned by the last response, and which will be
|
|
|
|
# sent with the next request.
|
|
|
|
attr_reader :cookies
|
|
|
|
|
|
|
|
# A map of the headers returned by the last response.
|
|
|
|
attr_reader :headers
|
|
|
|
|
|
|
|
# A reference to the controller instance used by the last request.
|
|
|
|
attr_reader :controller
|
|
|
|
|
|
|
|
# A reference to the request instance used by the last request.
|
|
|
|
attr_reader :request
|
|
|
|
|
|
|
|
# A reference to the response instance used by the last request.
|
|
|
|
attr_reader :response
|
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
# A running counter of the number of requests processed.
|
|
|
|
attr_accessor :request_count
|
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
class MultiPartNeededException < Exception
|
|
|
|
end
|
|
|
|
|
2008-06-02 08:35:38 +02:00
|
|
|
# Create and initialize a new Session instance.
|
2009-02-04 21:26:08 +01:00
|
|
|
def initialize(app = nil)
|
|
|
|
@application = app || ActionController::Dispatcher.new
|
2007-01-22 14:43:50 +01:00
|
|
|
reset!
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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!
|
|
|
|
def reset!
|
|
|
|
@status = @path = @headers = nil
|
|
|
|
@result = @status_message = nil
|
|
|
|
@https = false
|
|
|
|
@cookies = {}
|
|
|
|
@controller = @request = @response = nil
|
2007-12-21 08:48:59 +01:00
|
|
|
@request_count = 0
|
2007-10-15 19:16:54 +02:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
self.host = "www.example.com"
|
|
|
|
self.remote_addr = "127.0.0.1"
|
2009-02-04 21:26:08 +01:00
|
|
|
self.accept = "text/xml,application/xml,application/xhtml+xml," +
|
|
|
|
"text/html;q=0.9,text/plain;q=0.8,image/png," +
|
|
|
|
"*/*;q=0.5"
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
unless defined? @named_routes_configured
|
2007-01-22 14:43:50 +01:00
|
|
|
# install the named routes in this session instance.
|
2009-02-04 21:26:08 +01:00
|
|
|
klass = class << self; self; end
|
2007-12-21 08:48:59 +01:00
|
|
|
Routing::Routes.install_helpers(klass)
|
2007-01-22 14:43:50 +01:00
|
|
|
|
|
|
|
# the helpers are made protected by default--we make them public for
|
|
|
|
# easier access during testing and troubleshooting.
|
2007-12-21 08:48:59 +01:00
|
|
|
klass.module_eval { public *Routing::Routes.named_routes.helpers }
|
2007-01-22 14:43:50 +01:00
|
|
|
@named_routes_configured = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Specify whether or not the session should mimic a secure HTTPS request.
|
|
|
|
#
|
|
|
|
# session.https!
|
|
|
|
# session.https!(false)
|
2009-02-04 21:26:08 +01:00
|
|
|
def https!(flag = true)
|
2007-10-15 19:16:54 +02:00
|
|
|
@https = flag
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
# Return +true+ if the session is mimicking a secure HTTPS request.
|
2007-01-22 14:43:50 +01:00
|
|
|
#
|
|
|
|
# if session.https?
|
|
|
|
# ...
|
|
|
|
# end
|
|
|
|
def https?
|
|
|
|
@https
|
|
|
|
end
|
|
|
|
|
|
|
|
# Set the host name to use in the next request.
|
|
|
|
#
|
|
|
|
# session.host! "www.example.com"
|
|
|
|
def host!(name)
|
|
|
|
@host = name
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
def follow_redirect!
|
|
|
|
raise "not a redirect! #{@status} #{@status_message}" unless redirect?
|
2009-02-04 21:26:08 +01:00
|
|
|
get(interpret_uri(headers['location']))
|
2007-01-22 14:43:50 +01:00
|
|
|
status
|
|
|
|
end
|
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
# Performs a request using the specified method, 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.
|
|
|
|
def request_via_redirect(http_method, path, parameters = nil, headers = nil)
|
|
|
|
send(http_method, path, parameters, headers)
|
2007-01-22 14:43:50 +01:00
|
|
|
follow_redirect! while redirect?
|
|
|
|
status
|
|
|
|
end
|
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
# Performs a GET request, following any subsequent redirect.
|
2008-06-02 08:35:38 +02:00
|
|
|
# See +request_via_redirect+ for more information.
|
2007-12-21 08:48:59 +01:00
|
|
|
def get_via_redirect(path, parameters = nil, headers = nil)
|
|
|
|
request_via_redirect(:get, path, parameters, headers)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Performs a POST request, following any subsequent redirect.
|
2008-06-02 08:35:38 +02:00
|
|
|
# See +request_via_redirect+ for more information.
|
2007-12-21 08:48:59 +01:00
|
|
|
def post_via_redirect(path, parameters = nil, headers = nil)
|
|
|
|
request_via_redirect(:post, path, parameters, headers)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Performs a PUT request, following any subsequent redirect.
|
2008-06-02 08:35:38 +02:00
|
|
|
# See +request_via_redirect+ for more information.
|
2007-12-21 08:48:59 +01:00
|
|
|
def put_via_redirect(path, parameters = nil, headers = nil)
|
|
|
|
request_via_redirect(:put, path, parameters, headers)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Performs a DELETE request, following any subsequent redirect.
|
2008-06-02 08:35:38 +02:00
|
|
|
# See +request_via_redirect+ for more information.
|
2007-12-21 08:48:59 +01:00
|
|
|
def delete_via_redirect(path, parameters = nil, headers = nil)
|
|
|
|
request_via_redirect(:delete, path, parameters, headers)
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns +true+ if the last response was a redirect.
|
|
|
|
def redirect?
|
|
|
|
status/100 == 3
|
|
|
|
end
|
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
# Performs a GET request with the given parameters.
|
|
|
|
#
|
2009-02-04 21:26:08 +01:00
|
|
|
# - +path+: The URI (as a String) on which you want to perform a GET
|
|
|
|
# request.
|
|
|
|
# - +parameters+: The HTTP parameters that you want to pass. This may
|
|
|
|
# be +nil+,
|
2008-10-27 07:47:01 +01:00
|
|
|
# a Hash, or a String that is appropriately encoded
|
2009-02-04 21:26:08 +01:00
|
|
|
# (<tt>application/x-www-form-urlencoded</tt> or
|
|
|
|
# <tt>multipart/form-data</tt>).
|
2008-10-27 07:47:01 +01:00
|
|
|
# - +headers+: Additional HTTP headers to pass, as a Hash. The keys will
|
|
|
|
# automatically be upcased, with the prefix 'HTTP_' added if needed.
|
|
|
|
#
|
2009-02-04 21:26:08 +01:00
|
|
|
# This method returns an Response object, which one can use to
|
|
|
|
# inspect the details of the response. Furthermore, if this method was
|
|
|
|
# called from an ActionController::IntegrationTest object, then that
|
|
|
|
# object's <tt>@response</tt> instance variable will point to the same
|
|
|
|
# response object.
|
2007-02-09 09:04:31 +01:00
|
|
|
#
|
2008-06-02 08:35:38 +02:00
|
|
|
# You can also perform POST, PUT, DELETE, and HEAD requests with +post+,
|
|
|
|
# +put+, +delete+, and +head+.
|
2007-12-21 08:48:59 +01:00
|
|
|
def get(path, parameters = nil, headers = nil)
|
2007-01-22 14:43:50 +01:00
|
|
|
process :get, path, parameters, headers
|
|
|
|
end
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
# Performs a POST request with the given parameters. See get() for more
|
|
|
|
# details.
|
2007-12-21 08:48:59 +01:00
|
|
|
def post(path, parameters = nil, headers = nil)
|
2007-01-22 14:43:50 +01:00
|
|
|
process :post, path, parameters, headers
|
|
|
|
end
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
# Performs a PUT request with the given parameters. See get() for more
|
|
|
|
# details.
|
2007-12-21 08:48:59 +01:00
|
|
|
def put(path, parameters = nil, headers = nil)
|
2007-02-09 09:04:31 +01:00
|
|
|
process :put, path, parameters, headers
|
|
|
|
end
|
2007-10-15 19:16:54 +02:00
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
# Performs a DELETE request with the given parameters. See get() for
|
|
|
|
# more details.
|
2007-12-21 08:48:59 +01:00
|
|
|
def delete(path, parameters = nil, headers = nil)
|
2007-02-09 09:04:31 +01:00
|
|
|
process :delete, path, parameters, headers
|
|
|
|
end
|
2007-10-15 19:16:54 +02:00
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
# Performs a HEAD request with the given parameters. See get() for more
|
|
|
|
# details.
|
2007-12-21 08:48:59 +01:00
|
|
|
def head(path, parameters = nil, headers = nil)
|
2007-02-09 09:04:31 +01:00
|
|
|
process :head, path, parameters, headers
|
|
|
|
end
|
|
|
|
|
2007-10-15 19:16:54 +02:00
|
|
|
# Performs an XMLHttpRequest request with the given parameters, mirroring
|
|
|
|
# a request from the Prototype library.
|
|
|
|
#
|
|
|
|
# The request_method is :get, :post, :put, :delete or :head; the
|
|
|
|
# parameters are +nil+, a hash, or a url-encoded or multipart string;
|
|
|
|
# the headers are a hash. Keys are automatically upcased and prefixed
|
|
|
|
# with 'HTTP_' if not already.
|
|
|
|
def xml_http_request(request_method, path, parameters = nil, headers = nil)
|
|
|
|
headers ||= {}
|
|
|
|
headers['X-Requested-With'] = 'XMLHttpRequest'
|
2009-02-04 21:26:08 +01:00
|
|
|
headers['Accept'] ||= [Mime::JS, Mime::HTML, Mime::XML, 'text/xml', Mime::ALL].join(', ')
|
2007-10-15 19:16:54 +02:00
|
|
|
process(request_method, path, parameters, headers)
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
2007-10-15 19:16:54 +02:00
|
|
|
alias xhr :xml_http_request
|
2007-01-22 14:43:50 +01:00
|
|
|
|
|
|
|
# Returns the URL for the given options, according to the rules specified
|
|
|
|
# in the application's routes.
|
|
|
|
def url_for(options)
|
2009-02-04 21:26:08 +01:00
|
|
|
controller ?
|
|
|
|
controller.url_for(options) :
|
|
|
|
generic_url_rewriter.rewrite(options)
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
# Tailors the session based on the given URI, setting the HTTPS value
|
|
|
|
# and the hostname.
|
|
|
|
def interpret_uri(path)
|
|
|
|
location = URI.parse(path)
|
|
|
|
https! URI::HTTPS === location if location.scheme
|
|
|
|
host! location.host if location.host
|
|
|
|
location.query ? "#{location.path}?#{location.query}" : location.path
|
|
|
|
end
|
|
|
|
|
|
|
|
# Performs the actual request.
|
2007-12-21 08:48:59 +01:00
|
|
|
def process(method, path, parameters = nil, headers = nil)
|
2007-01-22 14:43:50 +01:00
|
|
|
data = requestify(parameters)
|
|
|
|
path = interpret_uri(path) if path =~ %r{://}
|
|
|
|
path = "/#{path}" unless path[0] == ?/
|
|
|
|
@path = path
|
|
|
|
env = {}
|
|
|
|
|
|
|
|
if method == :get
|
|
|
|
env["QUERY_STRING"] = data
|
|
|
|
data = nil
|
|
|
|
end
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
env["QUERY_STRING"] ||= ""
|
|
|
|
|
2009-12-01 02:38:34 +01:00
|
|
|
data ||= ''
|
|
|
|
data.force_encoding(Encoding::ASCII_8BIT) if data.respond_to?(:force_encoding)
|
|
|
|
data = data.is_a?(IO) ? data : StringIO.new(data)
|
2009-02-04 21:26:08 +01:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
env.update(
|
2009-02-04 21:26:08 +01:00
|
|
|
"REQUEST_METHOD" => method.to_s.upcase,
|
|
|
|
"SERVER_NAME" => host,
|
|
|
|
"SERVER_PORT" => (https? ? "443" : "80"),
|
|
|
|
"HTTPS" => https? ? "on" : "off",
|
|
|
|
"rack.url_scheme" => https? ? "https" : "http",
|
|
|
|
"SCRIPT_NAME" => "",
|
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
"REQUEST_URI" => path,
|
2009-02-04 21:26:08 +01:00
|
|
|
"PATH_INFO" => path,
|
2007-01-22 14:43:50 +01:00
|
|
|
"HTTP_HOST" => host,
|
|
|
|
"REMOTE_ADDR" => remote_addr,
|
|
|
|
"CONTENT_TYPE" => "application/x-www-form-urlencoded",
|
|
|
|
"CONTENT_LENGTH" => data ? data.length.to_s : nil,
|
|
|
|
"HTTP_COOKIE" => encode_cookies,
|
2009-02-04 21:26:08 +01:00
|
|
|
"HTTP_ACCEPT" => accept,
|
|
|
|
|
|
|
|
"rack.version" => [0,1],
|
|
|
|
"rack.input" => data,
|
|
|
|
"rack.errors" => StringIO.new,
|
|
|
|
"rack.multithread" => true,
|
|
|
|
"rack.multiprocess" => true,
|
2009-08-04 17:16:03 +02:00
|
|
|
"rack.run_once" => false
|
2007-01-22 14:43:50 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
(headers || {}).each do |key, value|
|
|
|
|
key = key.to_s.upcase.gsub(/-/, "_")
|
2007-02-09 09:04:31 +01:00
|
|
|
key = "HTTP_#{key}" unless env.has_key?(key) || key =~ /^HTTP_/
|
2007-01-22 14:43:50 +01:00
|
|
|
env[key] = value
|
|
|
|
end
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
[ControllerCapture, ActionController::ProcessWithTest].each do |mod|
|
|
|
|
unless ActionController::Base < mod
|
|
|
|
ActionController::Base.class_eval { include mod }
|
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
ActionController::Base.clear_last_instantiation!
|
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
app = Rack::Lint.new(@application)
|
2009-02-04 21:26:08 +01:00
|
|
|
status, headers, body = app.call(env)
|
|
|
|
@request_count += 1
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2007-02-09 09:04:31 +01:00
|
|
|
@html_document = nil
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
@status = status.to_i
|
|
|
|
@status_message = StatusCodes::STATUS_CODES[@status]
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
@headers = Rack::Utils::HeaderHash.new(headers)
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2010-05-25 19:45:45 +02:00
|
|
|
cookies = @headers['Set-Cookie']
|
|
|
|
cookies = cookies.to_s.split("\n") unless cookies.is_a?(Array)
|
|
|
|
cookies.each do |cookie|
|
2008-10-27 07:47:01 +01:00
|
|
|
name, value = cookie.match(/^([^=]*)=([^;]*);/)[1,2]
|
2007-01-22 14:43:50 +01:00
|
|
|
@cookies[name] = value
|
2009-02-04 21:26:08 +01:00
|
|
|
end
|
2008-10-27 07:47:01 +01:00
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
@body = ""
|
2009-08-04 17:16:03 +02:00
|
|
|
if body.respond_to?(:to_str)
|
2009-02-04 21:26:08 +01:00
|
|
|
@body << body
|
|
|
|
else
|
|
|
|
body.each { |part| @body << part }
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
if @controller = ActionController::Base.last_instantiation
|
|
|
|
@request = @controller.request
|
|
|
|
@response = @controller.response
|
|
|
|
@controller.send(:set_test_assigns)
|
|
|
|
else
|
|
|
|
# Decorate responses from Rack Middleware and Rails Metal
|
|
|
|
# as an Response for the purposes of integration testing
|
|
|
|
@response = Response.new
|
|
|
|
@response.status = status.to_s
|
|
|
|
@response.headers.replace(@headers)
|
|
|
|
@response.body = @body
|
|
|
|
end
|
|
|
|
|
|
|
|
# Decorate the response with the standard behavior of the
|
|
|
|
# TestResponse so that things like assert_response can be
|
|
|
|
# used in integration tests.
|
|
|
|
@response.extend(TestResponseBehavior)
|
|
|
|
|
2010-05-25 19:45:45 +02:00
|
|
|
body.close if body.respond_to?(:close)
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
return @status
|
2008-10-27 07:47:01 +01:00
|
|
|
rescue MultiPartNeededException
|
|
|
|
boundary = "----------XnJLe9ZIbbGUYtzPQJ16u1"
|
2009-02-04 21:26:08 +01:00
|
|
|
status = process(method, path,
|
|
|
|
multipart_body(parameters, boundary),
|
|
|
|
(headers || {}).merge(
|
|
|
|
{"CONTENT_TYPE" => "multipart/form-data; boundary=#{boundary}"}))
|
2008-10-27 07:47:01 +01:00
|
|
|
return status
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2007-10-15 19:16:54 +02:00
|
|
|
# Encode the cookies hash in a format suitable for passing to a
|
2007-01-22 14:43:50 +01:00
|
|
|
# request.
|
|
|
|
def encode_cookies
|
|
|
|
cookies.inject("") do |string, (name, value)|
|
|
|
|
string << "#{name}=#{value}; "
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
# Get a temporary URL writer object
|
2007-01-22 14:43:50 +01:00
|
|
|
def generic_url_rewriter
|
2008-10-27 07:47:01 +01:00
|
|
|
env = {
|
|
|
|
'REQUEST_METHOD' => "GET",
|
|
|
|
'QUERY_STRING' => "",
|
|
|
|
"REQUEST_URI" => "/",
|
|
|
|
"HTTP_HOST" => host,
|
|
|
|
"SERVER_PORT" => https? ? "443" : "80",
|
|
|
|
"HTTPS" => https? ? "on" : "off"
|
|
|
|
}
|
2009-02-04 21:26:08 +01:00
|
|
|
UrlRewriter.new(Request.new(env), {})
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def name_with_prefix(prefix, name)
|
|
|
|
prefix ? "#{prefix}[#{name}]" : name.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
# Convert the given parameters to a request string. The parameters may
|
|
|
|
# be a string, +nil+, or a Hash.
|
|
|
|
def requestify(parameters, prefix=nil)
|
2008-05-18 06:22:34 +02:00
|
|
|
if TestUploadedFile === parameters
|
|
|
|
raise MultiPartNeededException
|
|
|
|
elsif Hash === parameters
|
2007-01-22 14:43:50 +01:00
|
|
|
return nil if parameters.empty?
|
2009-02-04 21:26:08 +01:00
|
|
|
parameters.map { |k,v|
|
|
|
|
requestify(v, name_with_prefix(prefix, k))
|
|
|
|
}.join("&")
|
2007-01-22 14:43:50 +01:00
|
|
|
elsif Array === parameters
|
2009-02-04 21:26:08 +01:00
|
|
|
parameters.map { |v|
|
|
|
|
requestify(v, name_with_prefix(prefix, ""))
|
|
|
|
}.join("&")
|
2007-01-22 14:43:50 +01:00
|
|
|
elsif prefix.nil?
|
|
|
|
parameters
|
|
|
|
else
|
|
|
|
"#{CGI.escape(prefix)}=#{CGI.escape(parameters.to_s)}"
|
|
|
|
end
|
|
|
|
end
|
2008-05-18 06:22:34 +02:00
|
|
|
|
|
|
|
def multipart_requestify(params, first=true)
|
2010-09-05 22:24:15 +02:00
|
|
|
Array.new.tap do |p|
|
2008-05-18 06:22:34 +02:00
|
|
|
params.each do |key, value|
|
2009-08-04 17:16:03 +02:00
|
|
|
k = first ? key.to_s : "[#{key.to_s}]"
|
2008-05-18 06:22:34 +02:00
|
|
|
if Hash === value
|
|
|
|
multipart_requestify(value, false).each do |subkey, subvalue|
|
2010-09-05 22:24:15 +02:00
|
|
|
p << [k + subkey, subvalue]
|
|
|
|
end
|
|
|
|
elsif Array === value
|
|
|
|
value.each do |element|
|
|
|
|
if Hash === element || Array === element
|
|
|
|
multipart_requestify(element, false).each do |subkey, subvalue|
|
|
|
|
p << ["#{k}[]#{subkey}", subvalue]
|
|
|
|
end
|
|
|
|
else
|
|
|
|
p << ["#{k}[]", element]
|
|
|
|
end
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
|
|
|
else
|
2010-09-05 22:24:15 +02:00
|
|
|
p << [k, value]
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def multipart_body(params, boundary)
|
|
|
|
multipart_requestify(params).map do |key, value|
|
|
|
|
if value.respond_to?(:original_filename)
|
2009-02-04 21:26:08 +01:00
|
|
|
File.open(value.path, "rb") do |f|
|
2008-06-02 08:35:38 +02:00
|
|
|
f.set_encoding(Encoding::BINARY) if f.respond_to?(:set_encoding)
|
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
<<-EOF
|
|
|
|
--#{boundary}\r
|
|
|
|
Content-Disposition: form-data; name="#{key}"; filename="#{CGI.escape(value.original_filename)}"\r
|
|
|
|
Content-Type: #{value.content_type}\r
|
|
|
|
Content-Length: #{File.stat(value.path).size}\r
|
|
|
|
\r
|
|
|
|
#{f.read}\r
|
|
|
|
EOF
|
|
|
|
end
|
|
|
|
else
|
|
|
|
<<-EOF
|
|
|
|
--#{boundary}\r
|
|
|
|
Content-Disposition: form-data; name="#{key}"\r
|
|
|
|
\r
|
|
|
|
#{value}\r
|
|
|
|
EOF
|
|
|
|
end
|
|
|
|
end.join("")+"--#{boundary}--\r"
|
|
|
|
end
|
2010-09-05 22:24:15 +02:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# A module used to extend ActionController::Base, so that integration tests
|
|
|
|
# can capture the controller used to satisfy a request.
|
|
|
|
module ControllerCapture #:nodoc:
|
|
|
|
def self.included(base)
|
|
|
|
base.extend(ClassMethods)
|
|
|
|
base.class_eval do
|
2007-02-09 09:04:31 +01:00
|
|
|
class << self
|
|
|
|
alias_method_chain :new, :capture
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods #:nodoc:
|
|
|
|
mattr_accessor :last_instantiation
|
|
|
|
|
|
|
|
def clear_last_instantiation!
|
|
|
|
self.last_instantiation = nil
|
|
|
|
end
|
2007-02-09 09:04:31 +01:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def new_with_capture(*args)
|
2007-02-09 09:04:31 +01:00
|
|
|
controller = new_without_capture(*args)
|
|
|
|
self.last_instantiation ||= controller
|
|
|
|
controller
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
|
|
|
|
module Runner
|
2009-12-01 02:38:34 +01:00
|
|
|
def initialize(*args)
|
|
|
|
super
|
|
|
|
@integration_session = nil
|
|
|
|
end
|
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
# Reset the current session. This is useful for testing multiple sessions
|
|
|
|
# in a single test case.
|
|
|
|
def reset!
|
|
|
|
@integration_session = open_session
|
|
|
|
end
|
|
|
|
|
|
|
|
%w(get post put head delete cookies assigns
|
2008-10-27 07:47:01 +01:00
|
|
|
xml_http_request xhr get_via_redirect post_via_redirect).each do |method|
|
2007-12-21 08:48:59 +01:00
|
|
|
define_method(method) do |*args|
|
|
|
|
reset! unless @integration_session
|
|
|
|
# reset the html_document variable, but only for new get/post calls
|
|
|
|
@html_document = nil unless %w(cookies assigns).include?(method)
|
2010-09-05 22:24:15 +02:00
|
|
|
@integration_session.__send__(method, *args).tap do
|
2007-12-21 08:48:59 +01:00
|
|
|
copy_session_variables!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Open a new session instance. If a block is given, the new session is
|
|
|
|
# yielded to the block before being returned.
|
|
|
|
#
|
|
|
|
# session = open_session do |sess|
|
|
|
|
# sess.extend(CustomAssertions)
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# By default, a single session is automatically created for you, but you
|
|
|
|
# can use this method to open multiple sessions that ought to be tested
|
|
|
|
# simultaneously.
|
2009-02-04 21:26:08 +01:00
|
|
|
def open_session(application = nil)
|
|
|
|
session = Integration::Session.new(application)
|
2007-12-21 08:48:59 +01:00
|
|
|
|
|
|
|
# delegate the fixture accessors back to the test instance
|
|
|
|
extras = Module.new { attr_accessor :delegate, :test_result }
|
|
|
|
if self.class.respond_to?(:fixture_table_names)
|
|
|
|
self.class.fixture_table_names.each do |table_name|
|
|
|
|
name = table_name.tr(".", "_")
|
|
|
|
next unless respond_to?(name)
|
2009-02-04 21:26:08 +01:00
|
|
|
extras.__send__(:define_method, name) { |*args|
|
|
|
|
delegate.send(name, *args)
|
|
|
|
}
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# delegate add_assertion to the test case
|
2009-02-04 21:26:08 +01:00
|
|
|
extras.__send__(:define_method, :add_assertion) {
|
|
|
|
test_result.add_assertion
|
|
|
|
}
|
2007-12-21 08:48:59 +01:00
|
|
|
session.extend(extras)
|
|
|
|
session.delegate = self
|
|
|
|
session.test_result = @_result
|
|
|
|
|
|
|
|
yield session if block_given?
|
|
|
|
session
|
|
|
|
end
|
|
|
|
|
|
|
|
# Copy the instance variables from the current session instance into the
|
|
|
|
# test instance.
|
|
|
|
def copy_session_variables! #:nodoc:
|
|
|
|
return unless @integration_session
|
|
|
|
%w(controller response request).each do |var|
|
2008-10-27 07:47:01 +01:00
|
|
|
instance_variable_set("@#{var}", @integration_session.__send__(var))
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Delegate unhandled messages to the current session instance.
|
|
|
|
def method_missing(sym, *args, &block)
|
|
|
|
reset! unless @integration_session
|
2009-12-01 02:38:34 +01:00
|
|
|
if @integration_session.respond_to?(sym)
|
2010-09-05 22:24:15 +02:00
|
|
|
@integration_session.__send__(sym, *args, &block).tap do
|
2009-12-01 02:38:34 +01:00
|
|
|
copy_session_variables!
|
|
|
|
end
|
|
|
|
else
|
|
|
|
super
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# An IntegrationTest is one that spans multiple controllers and actions,
|
|
|
|
# tying them all together to ensure they work together as expected. It tests
|
|
|
|
# more completely than either unit or functional tests do, exercising the
|
|
|
|
# entire stack, from the dispatcher to the database.
|
|
|
|
#
|
|
|
|
# At its simplest, you simply extend IntegrationTest and write your tests
|
|
|
|
# using the get/post methods:
|
|
|
|
#
|
|
|
|
# require "#{File.dirname(__FILE__)}/test_helper"
|
|
|
|
#
|
|
|
|
# class ExampleTest < ActionController::IntegrationTest
|
|
|
|
# fixtures :people
|
|
|
|
#
|
|
|
|
# def test_login
|
|
|
|
# # get the login page
|
|
|
|
# get "/login"
|
|
|
|
# assert_equal 200, status
|
|
|
|
#
|
|
|
|
# # post the login and follow through to the home page
|
|
|
|
# post "/login", :username => people(:jamis).username,
|
|
|
|
# :password => people(:jamis).password
|
|
|
|
# follow_redirect!
|
|
|
|
# assert_equal 200, status
|
|
|
|
# assert_equal "/home", path
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# However, you can also have multiple session instances open per test, and
|
|
|
|
# even extend those instances with assertions and methods to create a very
|
|
|
|
# powerful testing DSL that is specific for your application. You can even
|
|
|
|
# reference any named routes you happen to have defined!
|
|
|
|
#
|
|
|
|
# require "#{File.dirname(__FILE__)}/test_helper"
|
|
|
|
#
|
|
|
|
# class AdvancedTest < ActionController::IntegrationTest
|
|
|
|
# fixtures :people, :rooms
|
|
|
|
#
|
|
|
|
# def test_login_and_speak
|
|
|
|
# jamis, david = login(:jamis), login(:david)
|
|
|
|
# room = rooms(:office)
|
|
|
|
#
|
|
|
|
# jamis.enter(room)
|
|
|
|
# jamis.speak(room, "anybody home?")
|
|
|
|
#
|
|
|
|
# david.enter(room)
|
|
|
|
# david.speak(room, "hello!")
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# private
|
|
|
|
#
|
|
|
|
# module CustomAssertions
|
|
|
|
# def enter(room)
|
|
|
|
# # reference a named route, for maximum internal consistency!
|
|
|
|
# get(room_url(:id => room.id))
|
|
|
|
# assert(...)
|
|
|
|
# ...
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def speak(room, message)
|
|
|
|
# xml_http_request "/say/#{room.id}", :message => message
|
|
|
|
# assert(...)
|
|
|
|
# ...
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def login(who)
|
|
|
|
# open_session do |sess|
|
|
|
|
# sess.extend(CustomAssertions)
|
|
|
|
# who = people(who)
|
|
|
|
# sess.post "/login", :username => who.username,
|
|
|
|
# :password => who.password
|
|
|
|
# assert(...)
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
2008-10-27 07:47:01 +01:00
|
|
|
class IntegrationTest < ActiveSupport::TestCase
|
2007-12-21 08:48:59 +01:00
|
|
|
include Integration::Runner
|
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
# Work around a bug in test/unit caused by the default test being named
|
|
|
|
# as a symbol (:default_test), which causes regex test filters
|
|
|
|
# (like "ruby test.rb -n /foo/") to fail because =~ doesn't work on
|
|
|
|
# symbols.
|
|
|
|
def initialize(name) #:nodoc:
|
|
|
|
super(name.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Work around test/unit's requirement that every subclass of TestCase have
|
|
|
|
# at least one test method. Note that this implementation extends to all
|
|
|
|
# subclasses, as well, so subclasses of IntegrationTest may also exist
|
|
|
|
# without any test methods.
|
|
|
|
def run(*args) #:nodoc:
|
|
|
|
return if @method_name == "default_test"
|
2007-10-15 19:16:54 +02:00
|
|
|
super
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Because of how use_instantiated_fixtures and use_transactional_fixtures
|
|
|
|
# are defined, we need to treat them as special cases. Otherwise, users
|
|
|
|
# would potentially have to set their values for both Test::Unit::TestCase
|
|
|
|
# ActionController::IntegrationTest, since by the time the value is set on
|
|
|
|
# TestCase, IntegrationTest has already been defined and cannot inherit
|
2009-02-04 21:26:08 +01:00
|
|
|
# changes to those variables. So, we make those two attributes
|
|
|
|
# copy-on-write.
|
2007-01-22 14:43:50 +01:00
|
|
|
|
|
|
|
class << self
|
|
|
|
def use_transactional_fixtures=(flag) #:nodoc:
|
|
|
|
@_use_transactional_fixtures = true
|
|
|
|
@use_transactional_fixtures = flag
|
|
|
|
end
|
|
|
|
|
|
|
|
def use_instantiated_fixtures=(flag) #:nodoc:
|
|
|
|
@_use_instantiated_fixtures = true
|
|
|
|
@use_instantiated_fixtures = flag
|
|
|
|
end
|
|
|
|
|
|
|
|
def use_transactional_fixtures #:nodoc:
|
|
|
|
@_use_transactional_fixtures ?
|
|
|
|
@use_transactional_fixtures :
|
|
|
|
superclass.use_transactional_fixtures
|
|
|
|
end
|
|
|
|
|
|
|
|
def use_instantiated_fixtures #:nodoc:
|
|
|
|
@_use_instantiated_fixtures ?
|
|
|
|
@use_instantiated_fixtures :
|
|
|
|
superclass.use_instantiated_fixtures
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|