2007-01-22 14:43:50 +01:00
|
|
|
module ActionController #:nodoc:
|
2009-02-04 21:26:08 +01:00
|
|
|
class TestRequest < Request #:nodoc:
|
2007-01-22 14:43:50 +01:00
|
|
|
attr_accessor :cookies, :session_options
|
2009-02-04 21:26:08 +01:00
|
|
|
attr_accessor :query_parameters, :path, :session
|
|
|
|
attr_accessor :host
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2009-02-28 02:23:00 +01:00
|
|
|
def self.new(env = {})
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(env = {})
|
|
|
|
super(Rack::MockRequest.env_for("/").merge(env))
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
@query_parameters = {}
|
|
|
|
@session = TestSession.new
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
initialize_default_values
|
|
|
|
initialize_containers
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def reset_session
|
2009-02-04 21:26:08 +01:00
|
|
|
@session.reset
|
2007-02-09 09:04:31 +01:00
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
# Wraps raw_post in a StringIO.
|
2008-10-27 07:47:01 +01:00
|
|
|
def body_stream #:nodoc:
|
2007-12-21 08:48:59 +01:00
|
|
|
StringIO.new(raw_post)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Either the RAW_POST_DATA environment variable or the URL-encoded request
|
|
|
|
# parameters.
|
2007-01-22 14:43:50 +01:00
|
|
|
def raw_post
|
2009-02-04 21:26:08 +01:00
|
|
|
@env['RAW_POST_DATA'] ||= begin
|
|
|
|
data = url_encoded_request_parameters
|
|
|
|
data.force_encoding(Encoding::BINARY) if data.respond_to?(:force_encoding)
|
|
|
|
data
|
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def port=(number)
|
|
|
|
@env["SERVER_PORT"] = number.to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
def action=(action_name)
|
|
|
|
@query_parameters.update({ "action" => action_name })
|
|
|
|
@parameters = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
# Used to check AbstractRequest's request_uri functionality.
|
|
|
|
# Disables the use of @path and @request_uri so superclass can handle those.
|
|
|
|
def set_REQUEST_URI(value)
|
|
|
|
@env["REQUEST_URI"] = value
|
|
|
|
@request_uri = nil
|
|
|
|
@path = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def request_uri=(uri)
|
|
|
|
@request_uri = uri
|
|
|
|
@path = uri.split("?").first
|
|
|
|
end
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
def request_method=(method)
|
|
|
|
@request_method = method
|
|
|
|
end
|
|
|
|
|
2007-02-09 09:04:31 +01:00
|
|
|
def accept=(mime_types)
|
|
|
|
@env["HTTP_ACCEPT"] = Array(mime_types).collect { |mime_types| mime_types.to_s }.join(",")
|
2009-02-04 21:26:08 +01:00
|
|
|
@accepts = nil
|
2007-02-09 09:04:31 +01:00
|
|
|
end
|
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
def if_modified_since=(last_modified)
|
|
|
|
@env["HTTP_IF_MODIFIED_SINCE"] = last_modified
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
def if_none_match=(etag)
|
|
|
|
@env["HTTP_IF_NONE_MATCH"] = etag
|
|
|
|
end
|
|
|
|
|
|
|
|
def remote_addr=(addr)
|
|
|
|
@env['REMOTE_ADDR'] = addr
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
def request_uri(*args)
|
2009-02-04 21:26:08 +01:00
|
|
|
@request_uri || super()
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
def path(*args)
|
2009-02-04 21:26:08 +01:00
|
|
|
@path || super()
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def assign_parameters(controller_path, action, parameters)
|
|
|
|
parameters = parameters.symbolize_keys.merge(:controller => controller_path, :action => action)
|
|
|
|
extra_keys = ActionController::Routing::Routes.extra_keys(parameters)
|
|
|
|
non_path_parameters = get? ? query_parameters : request_parameters
|
|
|
|
parameters.each do |key, value|
|
|
|
|
if value.is_a? Fixnum
|
|
|
|
value = value.to_s
|
|
|
|
elsif value.is_a? Array
|
2007-02-09 09:04:31 +01:00
|
|
|
value = ActionController::Routing::PathSegment::Result.new(value)
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
if extra_keys.include?(key.to_sym)
|
|
|
|
non_path_parameters[key] = value
|
|
|
|
else
|
|
|
|
path_parameters[key.to_s] = value
|
|
|
|
end
|
|
|
|
end
|
2009-02-04 21:26:08 +01:00
|
|
|
raw_post # populate env['RAW_POST_DATA']
|
2007-02-09 09:04:31 +01:00
|
|
|
@parameters = nil # reset TestRequest#parameters to use the new path_parameters
|
2008-10-27 07:47:01 +01:00
|
|
|
end
|
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def recycle!
|
|
|
|
self.query_parameters = {}
|
|
|
|
self.path_parameters = {}
|
2009-02-04 21:26:08 +01:00
|
|
|
@headers, @request_method, @accepts, @content_type = nil, nil, nil, nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_agent=(user_agent)
|
|
|
|
@env['HTTP_USER_AGENT'] = user_agent
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
private
|
|
|
|
def initialize_containers
|
2009-02-04 21:26:08 +01:00
|
|
|
@cookies = {}
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize_default_values
|
|
|
|
@host = "test.host"
|
|
|
|
@request_uri = "/"
|
2009-02-04 21:26:08 +01:00
|
|
|
@env['HTTP_USER_AGENT'] = "Rails Testing"
|
|
|
|
@env['REMOTE_ADDR'] = "0.0.0.0"
|
2007-01-22 14:43:50 +01:00
|
|
|
@env["SERVER_PORT"] = 80
|
|
|
|
@env['REQUEST_METHOD'] = "GET"
|
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
|
|
|
|
def url_encoded_request_parameters
|
|
|
|
params = self.request_parameters.dup
|
|
|
|
|
|
|
|
%w(controller action only_path).each do |k|
|
|
|
|
params.delete(k)
|
|
|
|
params.delete(k.to_sym)
|
|
|
|
end
|
|
|
|
|
|
|
|
params.to_query
|
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# A refactoring of TestResponse to allow the same behavior to be applied
|
|
|
|
# to the "real" CgiResponse class in integration tests.
|
|
|
|
module TestResponseBehavior #:nodoc:
|
2008-05-18 06:22:34 +02:00
|
|
|
# The response code of the request
|
2007-01-22 14:43:50 +01:00
|
|
|
def response_code
|
2009-02-04 21:26:08 +01:00
|
|
|
status.to_s[0,3].to_i rescue 0
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
2008-10-27 07:47:01 +01:00
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
# Returns a String to ensure compatibility with Net::HTTPResponse
|
2007-01-22 14:43:50 +01:00
|
|
|
def code
|
2008-10-27 07:47:01 +01:00
|
|
|
status.to_s.split(' ')[0]
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def message
|
2008-10-27 07:47:01 +01:00
|
|
|
status.to_s.split(' ',2)[1]
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
# Was the response successful?
|
2007-01-22 14:43:50 +01:00
|
|
|
def success?
|
2008-09-07 07:54:05 +02:00
|
|
|
(200..299).include?(response_code)
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
# Was the URL not found?
|
2007-01-22 14:43:50 +01:00
|
|
|
def missing?
|
|
|
|
response_code == 404
|
|
|
|
end
|
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
# Were we redirected?
|
2007-01-22 14:43:50 +01:00
|
|
|
def redirect?
|
|
|
|
(300..399).include?(response_code)
|
|
|
|
end
|
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
# Was there a server-side error?
|
2007-01-22 14:43:50 +01:00
|
|
|
def error?
|
|
|
|
(500..599).include?(response_code)
|
|
|
|
end
|
|
|
|
|
|
|
|
alias_method :server_error?, :error?
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
# Was there a client client?
|
|
|
|
def client_error?
|
|
|
|
(400..499).include?(response_code)
|
|
|
|
end
|
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
# Returns the redirection location or nil
|
2007-01-22 14:43:50 +01:00
|
|
|
def redirect_url
|
2007-02-09 09:04:31 +01:00
|
|
|
headers['Location']
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
# Does the redirect location match this regexp pattern?
|
2007-01-22 14:43:50 +01:00
|
|
|
def redirect_url_match?( pattern )
|
|
|
|
return false if redirect_url.nil?
|
|
|
|
p = Regexp.new(pattern) if pattern.class == String
|
|
|
|
p = pattern if pattern.class == Regexp
|
|
|
|
return false if p.nil?
|
|
|
|
p.match(redirect_url) != nil
|
|
|
|
end
|
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
# Returns the template of the file which was used to
|
|
|
|
# render this response (or nil)
|
2009-02-04 21:26:08 +01:00
|
|
|
def rendered
|
|
|
|
template.instance_variable_get(:@_rendered)
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
# A shortcut to the flash. Returns an empty hash if no session flash exists.
|
2007-01-22 14:43:50 +01:00
|
|
|
def flash
|
|
|
|
session['flash'] || {}
|
|
|
|
end
|
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
# Do we have a flash?
|
2007-01-22 14:43:50 +01:00
|
|
|
def has_flash?
|
2009-02-04 21:26:08 +01:00
|
|
|
!flash.empty?
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
# Do we have a flash that has contents?
|
2007-01-22 14:43:50 +01:00
|
|
|
def has_flash_with_contents?
|
|
|
|
!flash.empty?
|
|
|
|
end
|
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
# Does the specified flash object exist?
|
2007-01-22 14:43:50 +01:00
|
|
|
def has_flash_object?(name=nil)
|
|
|
|
!flash[name].nil?
|
|
|
|
end
|
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
# Does the specified object exist in the session?
|
2007-01-22 14:43:50 +01:00
|
|
|
def has_session_object?(name=nil)
|
|
|
|
!session[name].nil?
|
|
|
|
end
|
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
# A shortcut to the template.assigns
|
2007-01-22 14:43:50 +01:00
|
|
|
def template_objects
|
|
|
|
template.assigns || {}
|
|
|
|
end
|
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
# Does the specified template object exist?
|
2007-01-22 14:43:50 +01:00
|
|
|
def has_template_object?(name=nil)
|
2008-10-27 07:47:01 +01:00
|
|
|
!template_objects[name].nil?
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
# Returns the response cookies, converted to a Hash of (name => value) pairs
|
2008-10-27 07:47:01 +01:00
|
|
|
#
|
2009-02-04 21:26:08 +01:00
|
|
|
# assert_equal 'AuthorOfNewPage', r.cookies['author']
|
2007-01-22 14:43:50 +01:00
|
|
|
def cookies
|
2009-02-04 21:26:08 +01:00
|
|
|
cookies = {}
|
|
|
|
Array(headers['Set-Cookie']).each do |cookie|
|
|
|
|
key, value = cookie.split(";").first.split("=")
|
|
|
|
cookies[key] = value
|
|
|
|
end
|
|
|
|
cookies
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns binary content (downloadable file), converted to a String
|
|
|
|
def binary_content
|
|
|
|
raise "Response body is not a Proc: #{body.inspect}" unless body.kind_of?(Proc)
|
|
|
|
require 'stringio'
|
|
|
|
|
|
|
|
sio = StringIO.new
|
2007-12-21 08:48:59 +01:00
|
|
|
body.call(self, sio)
|
2007-01-22 14:43:50 +01:00
|
|
|
|
|
|
|
sio.rewind
|
|
|
|
sio.read
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
# Integration test methods such as ActionController::Integration::Session#get
|
|
|
|
# and ActionController::Integration::Session#post return objects of class
|
|
|
|
# TestResponse, which represent the HTTP response results of the requested
|
|
|
|
# controller actions.
|
|
|
|
#
|
2009-02-04 21:26:08 +01:00
|
|
|
# See Response for more information on controller response objects.
|
|
|
|
class TestResponse < Response
|
2007-01-22 14:43:50 +01:00
|
|
|
include TestResponseBehavior
|
2009-02-04 21:26:08 +01:00
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
def recycle!
|
|
|
|
headers.delete('ETag')
|
|
|
|
headers.delete('Last-Modified')
|
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
class TestSession < Hash #:nodoc:
|
2007-02-09 09:04:31 +01:00
|
|
|
attr_accessor :session_id
|
|
|
|
|
|
|
|
def initialize(attributes = nil)
|
2009-02-04 21:26:08 +01:00
|
|
|
reset_session_id
|
|
|
|
replace_attributes(attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset
|
|
|
|
reset_session_id
|
|
|
|
replace_attributes({ })
|
2007-02-09 09:04:31 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def data
|
2009-02-04 21:26:08 +01:00
|
|
|
to_hash
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def [](key)
|
2009-02-04 21:26:08 +01:00
|
|
|
super(key.to_s)
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def []=(key, value)
|
2009-02-04 21:26:08 +01:00
|
|
|
super(key.to_s, value)
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
def update(hash = nil)
|
|
|
|
if hash.nil?
|
|
|
|
ActiveSupport::Deprecation.warn('use replace instead', caller)
|
|
|
|
replace({})
|
|
|
|
else
|
|
|
|
super(hash)
|
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
def delete(key = nil)
|
|
|
|
if key.nil?
|
|
|
|
ActiveSupport::Deprecation.warn('use clear instead', caller)
|
|
|
|
clear
|
|
|
|
else
|
|
|
|
super(key.to_s)
|
|
|
|
end
|
2007-02-09 09:04:31 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def close
|
2009-02-04 21:26:08 +01:00
|
|
|
ActiveSupport::Deprecation.warn('sessions should no longer be closed', caller)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def reset_session_id
|
|
|
|
@session_id = ''
|
|
|
|
end
|
|
|
|
|
|
|
|
def replace_attributes(attributes = nil)
|
|
|
|
attributes ||= {}
|
|
|
|
replace(attributes.stringify_keys)
|
2007-02-09 09:04:31 +01:00
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
2007-02-09 09:04:31 +01:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
# Essentially generates a modified Tempfile object similar to the object
|
|
|
|
# you'd get from the standard library CGI module in a multipart
|
|
|
|
# request. This means you can use an ActionController::TestUploadedFile
|
|
|
|
# object in the params of a test request in order to simulate
|
|
|
|
# a file upload.
|
|
|
|
#
|
|
|
|
# Usage example, within a functional test:
|
2009-02-04 21:26:08 +01:00
|
|
|
# post :change_avatar, :avatar => ActionController::TestUploadedFile.new(ActionController::TestCase.fixture_path + '/files/spongebob.png', 'image/png')
|
2008-10-27 07:47:01 +01:00
|
|
|
#
|
2007-12-21 08:48:59 +01:00
|
|
|
# Pass a true third parameter to ensure the uploaded file is opened in binary mode (only required for Windows):
|
2009-02-04 21:26:08 +01:00
|
|
|
# post :change_avatar, :avatar => ActionController::TestUploadedFile.new(ActionController::TestCase.fixture_path + '/files/spongebob.png', 'image/png', :binary)
|
2007-02-09 09:04:31 +01:00
|
|
|
require 'tempfile'
|
2007-01-22 14:43:50 +01:00
|
|
|
class TestUploadedFile
|
|
|
|
# The filename, *not* including the path, of the "uploaded" file
|
|
|
|
attr_reader :original_filename
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
# The content type of the "uploaded" file
|
2008-09-07 07:54:05 +02:00
|
|
|
attr_accessor :content_type
|
2007-12-21 08:48:59 +01:00
|
|
|
|
|
|
|
def initialize(path, content_type = Mime::TEXT, binary = false)
|
2007-02-09 09:04:31 +01:00
|
|
|
raise "#{path} file does not exist" unless File.exist?(path)
|
2007-01-22 14:43:50 +01:00
|
|
|
@content_type = content_type
|
|
|
|
@original_filename = path.sub(/^.*#{File::SEPARATOR}([^#{File::SEPARATOR}]+)$/) { $1 }
|
|
|
|
@tempfile = Tempfile.new(@original_filename)
|
2008-06-02 08:35:38 +02:00
|
|
|
@tempfile.set_encoding(Encoding::BINARY) if @tempfile.respond_to?(:set_encoding)
|
2007-12-21 08:48:59 +01:00
|
|
|
@tempfile.binmode if binary
|
2007-01-22 14:43:50 +01:00
|
|
|
FileUtils.copy_file(path, @tempfile.path)
|
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def path #:nodoc:
|
|
|
|
@tempfile.path
|
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
alias local_path path
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def method_missing(method_name, *args, &block) #:nodoc:
|
2008-10-27 07:47:01 +01:00
|
|
|
@tempfile.__send__(method_name, *args, &block)
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
module TestProcess
|
|
|
|
def self.included(base)
|
2009-02-04 21:26:08 +01:00
|
|
|
# Executes a request simulating GET HTTP method and set/volley the response
|
|
|
|
def get(action, parameters = nil, session = nil, flash = nil)
|
|
|
|
process(action, parameters, session, flash, "GET")
|
|
|
|
end
|
|
|
|
|
|
|
|
# Executes a request simulating POST HTTP method and set/volley the response
|
|
|
|
def post(action, parameters = nil, session = nil, flash = nil)
|
|
|
|
process(action, parameters, session, flash, "POST")
|
|
|
|
end
|
|
|
|
|
|
|
|
# Executes a request simulating PUT HTTP method and set/volley the response
|
|
|
|
def put(action, parameters = nil, session = nil, flash = nil)
|
|
|
|
process(action, parameters, session, flash, "PUT")
|
|
|
|
end
|
|
|
|
|
|
|
|
# Executes a request simulating DELETE HTTP method and set/volley the response
|
|
|
|
def delete(action, parameters = nil, session = nil, flash = nil)
|
|
|
|
process(action, parameters, session, flash, "DELETE")
|
|
|
|
end
|
|
|
|
|
|
|
|
# Executes a request simulating HEAD HTTP method and set/volley the response
|
|
|
|
def head(action, parameters = nil, session = nil, flash = nil)
|
|
|
|
process(action, parameters, session, flash, "HEAD")
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
def process(action, parameters = nil, session = nil, flash = nil, http_method = 'GET')
|
2007-01-22 14:43:50 +01:00
|
|
|
# Sanity check for required instance variables so we can give an
|
|
|
|
# understandable error message.
|
2007-02-09 09:04:31 +01:00
|
|
|
%w(@controller @request @response).each do |iv_name|
|
2008-05-18 06:22:34 +02:00
|
|
|
if !(instance_variable_names.include?(iv_name) || instance_variable_names.include?(iv_name.to_sym)) || instance_variable_get(iv_name).nil?
|
2007-02-09 09:04:31 +01:00
|
|
|
raise "#{iv_name} is nil: make sure you set it in your test's setup method."
|
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@request.recycle!
|
2008-10-27 07:47:01 +01:00
|
|
|
@response.recycle!
|
2007-01-22 14:43:50 +01:00
|
|
|
|
|
|
|
@html_document = nil
|
2009-02-04 21:26:08 +01:00
|
|
|
@request.env['REQUEST_METHOD'] = http_method
|
2008-11-24 22:53:39 +01:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
@request.action = action.to_s
|
|
|
|
|
|
|
|
parameters ||= {}
|
|
|
|
@request.assign_parameters(@controller.class.controller_path, action.to_s, parameters)
|
|
|
|
|
|
|
|
@request.session = ActionController::TestSession.new(session) unless session.nil?
|
|
|
|
@request.session["flash"] = ActionController::Flash::FlashHash.new.update(flash) if flash
|
|
|
|
build_request_uri(action, parameters)
|
2009-02-04 21:26:08 +01:00
|
|
|
|
|
|
|
Base.class_eval { include ProcessWithTest } unless Base < ProcessWithTest
|
|
|
|
@controller.process_with_test(@request, @response)
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def xml_http_request(request_method, action, parameters = nil, session = nil, flash = nil)
|
|
|
|
@request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
|
2009-02-04 21:26:08 +01:00
|
|
|
@request.env['HTTP_ACCEPT'] = [Mime::JS, Mime::HTML, Mime::XML, 'text/xml', Mime::ALL].join(', ')
|
2008-10-27 07:47:01 +01:00
|
|
|
returning __send__(request_method, action, parameters, session, flash) do
|
2007-01-22 14:43:50 +01:00
|
|
|
@request.env.delete 'HTTP_X_REQUESTED_WITH'
|
|
|
|
@request.env.delete 'HTTP_ACCEPT'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
alias xhr :xml_http_request
|
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
def assigns(key = nil)
|
|
|
|
if key.nil?
|
|
|
|
@response.template.assigns
|
|
|
|
else
|
|
|
|
@response.template.assigns[key.to_s]
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def session
|
2009-02-04 21:26:08 +01:00
|
|
|
@request.session
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def flash
|
|
|
|
@response.flash
|
|
|
|
end
|
|
|
|
|
|
|
|
def cookies
|
|
|
|
@response.cookies
|
|
|
|
end
|
|
|
|
|
|
|
|
def redirect_to_url
|
|
|
|
@response.redirect_url
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_request_uri(action, parameters)
|
2007-12-21 08:48:59 +01:00
|
|
|
unless @request.env['REQUEST_URI']
|
2008-10-27 07:47:01 +01:00
|
|
|
options = @controller.__send__(:rewrite_options, parameters)
|
2007-01-22 14:43:50 +01:00
|
|
|
options.update(:only_path => true, :action => action)
|
|
|
|
|
|
|
|
url = ActionController::UrlRewriter.new(@request, parameters)
|
2007-12-21 08:48:59 +01:00
|
|
|
@request.set_REQUEST_URI(url.rewrite(options))
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def html_document
|
2007-12-21 08:48:59 +01:00
|
|
|
xml = @response.content_type =~ /xml$/
|
|
|
|
@html_document ||= HTML::Document.new(@response.body, false, xml)
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_tag(conditions)
|
|
|
|
html_document.find(conditions)
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_all_tag(conditions)
|
|
|
|
html_document.find_all(conditions)
|
|
|
|
end
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
def method_missing(selector, *args, &block)
|
|
|
|
if @controller && ActionController::Routing::Routes.named_routes.helpers.include?(selector)
|
|
|
|
@controller.send(selector, *args, &block)
|
2008-10-27 07:47:01 +01:00
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
2008-10-27 07:47:01 +01:00
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
# Shortcut for <tt>ActionController::TestUploadedFile.new(ActionController::TestCase.fixture_path + path, type)</tt>:
|
2008-05-18 06:22:34 +02:00
|
|
|
#
|
2007-01-22 14:43:50 +01:00
|
|
|
# post :change_avatar, :avatar => fixture_file_upload('/files/spongebob.png', 'image/png')
|
2007-12-21 08:48:59 +01:00
|
|
|
#
|
2008-05-18 06:22:34 +02:00
|
|
|
# To upload binary files on Windows, pass <tt>:binary</tt> as the last parameter.
|
|
|
|
# This will not affect other platforms:
|
|
|
|
#
|
2007-12-21 08:48:59 +01:00
|
|
|
# post :change_avatar, :avatar => fixture_file_upload('/files/spongebob.png', 'image/png', :binary)
|
|
|
|
def fixture_file_upload(path, mime_type = nil, binary = false)
|
2009-02-04 21:26:08 +01:00
|
|
|
fixture_path = ActionController::TestCase.send(:fixture_path) if ActionController::TestCase.respond_to?(:fixture_path)
|
|
|
|
ActionController::TestUploadedFile.new("#{fixture_path}#{path}", mime_type, binary)
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# A helper to make it easier to test different route configurations.
|
|
|
|
# This method temporarily replaces ActionController::Routing::Routes
|
2008-10-27 07:47:01 +01:00
|
|
|
# with a new RouteSet instance.
|
2007-01-22 14:43:50 +01:00
|
|
|
#
|
|
|
|
# The new instance is yielded to the passed block. Typically the block
|
2008-05-18 06:22:34 +02:00
|
|
|
# will create some routes using <tt>map.draw { map.connect ... }</tt>:
|
2007-01-22 14:43:50 +01:00
|
|
|
#
|
2008-05-18 06:22:34 +02:00
|
|
|
# with_routing do |set|
|
|
|
|
# set.draw do |map|
|
|
|
|
# map.connect ':controller/:action/:id'
|
|
|
|
# assert_equal(
|
|
|
|
# ['/content/10/show', {}],
|
|
|
|
# map.generate(:controller => 'content', :id => 10, :action => 'show')
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
2007-01-22 14:43:50 +01:00
|
|
|
#
|
|
|
|
def with_routing
|
|
|
|
real_routes = ActionController::Routing::Routes
|
2007-12-21 08:48:59 +01:00
|
|
|
ActionController::Routing.module_eval { remove_const :Routes }
|
2007-01-22 14:43:50 +01:00
|
|
|
|
|
|
|
temporary_routes = ActionController::Routing::RouteSet.new
|
2007-12-21 08:48:59 +01:00
|
|
|
ActionController::Routing.module_eval { const_set :Routes, temporary_routes }
|
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
yield temporary_routes
|
|
|
|
ensure
|
|
|
|
if ActionController::Routing.const_defined? :Routes
|
2007-12-21 08:48:59 +01:00
|
|
|
ActionController::Routing.module_eval { remove_const :Routes }
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
ActionController::Routing.const_set(:Routes, real_routes) if real_routes
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
module ProcessWithTest #:nodoc:
|
|
|
|
def self.included(base)
|
|
|
|
base.class_eval { attr_reader :assigns }
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
2009-02-04 21:26:08 +01:00
|
|
|
|
|
|
|
def process_with_test(*args)
|
|
|
|
process(*args).tap { set_test_assigns }
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def set_test_assigns
|
|
|
|
@assigns = {}
|
|
|
|
(instance_variable_names - self.class.protected_instance_variables).each do |var|
|
|
|
|
name, value = var[1..-1], instance_variable_get(var)
|
|
|
|
@assigns[name] = value
|
|
|
|
response.template.assigns[name] = value if response
|
|
|
|
end
|
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
end
|