2007-01-28 02:16:55 -05:00
|
|
|
require 'action_controller/assertions'
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
module ActionController #:nodoc:
|
|
|
|
class Base
|
|
|
|
# Process a test request called with a +TestRequest+ object.
|
|
|
|
def self.process_test(request)
|
|
|
|
new.process_test(request)
|
|
|
|
end
|
2005-06-29 05:44:55 -04:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def process_test(request) #:nodoc:
|
|
|
|
process(request, TestResponse.new)
|
|
|
|
end
|
2005-07-31 08:16:21 -04:00
|
|
|
|
|
|
|
def process_with_test(*args)
|
|
|
|
returning process_without_test(*args) do
|
|
|
|
add_variables_to_assigns
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-04-29 16:20:22 -04:00
|
|
|
alias_method_chain :process, :test
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class TestRequest < AbstractRequest #:nodoc:
|
2005-07-22 06:37:09 -04:00
|
|
|
attr_accessor :cookies, :session_options
|
2005-02-14 20:45:35 -05:00
|
|
|
attr_accessor :query_parameters, :request_parameters, :path, :session, :env
|
2007-09-06 23:45:54 -04:00
|
|
|
attr_accessor :host, :user_agent
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
def initialize(query_parameters = nil, request_parameters = nil, session = nil)
|
|
|
|
@query_parameters = query_parameters || {}
|
|
|
|
@request_parameters = request_parameters || {}
|
|
|
|
@session = session || TestSession.new
|
2005-06-29 05:44:55 -04:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
initialize_containers
|
|
|
|
initialize_default_values
|
|
|
|
|
|
|
|
super()
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_session
|
2006-07-28 19:15:12 -04:00
|
|
|
@session = TestSession.new
|
2006-11-13 13:59:01 -05:00
|
|
|
end
|
2006-03-15 22:15:12 -05:00
|
|
|
|
2007-05-15 17:36:21 -04:00
|
|
|
# Wraps raw_post in a StringIO.
|
|
|
|
def body
|
|
|
|
StringIO.new(raw_post)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Either the RAW_POST_DATA environment variable or the URL-encoded request
|
|
|
|
# parameters.
|
2006-03-15 22:15:12 -05:00
|
|
|
def raw_post
|
2007-05-15 17:36:21 -04:00
|
|
|
env['RAW_POST_DATA'] ||= url_encoded_request_parameters
|
2006-03-15 22:15:12 -05:00
|
|
|
end
|
|
|
|
|
2004-12-18 13:01:28 -05:00
|
|
|
def port=(number)
|
|
|
|
@env["SERVER_PORT"] = number.to_i
|
2005-07-23 05:00:05 -04:00
|
|
|
@port_as_int = nil
|
2004-12-18 13:01:28 -05:00
|
|
|
end
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def action=(action_name)
|
|
|
|
@query_parameters.update({ "action" => action_name })
|
|
|
|
@parameters = nil
|
|
|
|
end
|
2005-06-29 05:44:55 -04:00
|
|
|
|
2005-02-14 20:45:35 -05:00
|
|
|
# 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
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def request_uri=(uri)
|
|
|
|
@request_uri = uri
|
|
|
|
@path = uri.split("?").first
|
|
|
|
end
|
|
|
|
|
2006-05-31 00:49:18 -04:00
|
|
|
def accept=(mime_types)
|
|
|
|
@env["HTTP_ACCEPT"] = Array(mime_types).collect { |mime_types| mime_types.to_s }.join(",")
|
|
|
|
end
|
|
|
|
|
2005-05-19 14:24:52 -04:00
|
|
|
def remote_addr=(addr)
|
|
|
|
@env['REMOTE_ADDR'] = addr
|
|
|
|
end
|
|
|
|
|
2005-07-31 06:31:47 -04:00
|
|
|
def remote_addr
|
|
|
|
@env['REMOTE_ADDR']
|
|
|
|
end
|
|
|
|
|
2005-02-14 20:45:35 -05:00
|
|
|
def request_uri
|
2007-01-28 02:16:55 -05:00
|
|
|
@request_uri || super
|
2005-02-14 20:45:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def path
|
2007-01-28 02:16:55 -05:00
|
|
|
@path || super
|
2005-02-14 20:45:35 -05:00
|
|
|
end
|
2005-06-29 05:44:55 -04:00
|
|
|
|
2005-06-29 06:40:26 -04:00
|
|
|
def assign_parameters(controller_path, action, parameters)
|
2005-06-29 05:44:55 -04:00
|
|
|
parameters = parameters.symbolize_keys.merge(:controller => controller_path, :action => action)
|
2005-06-29 06:40:26 -04:00
|
|
|
extra_keys = ActionController::Routing::Routes.extra_keys(parameters)
|
2005-06-29 05:44:55 -04:00
|
|
|
non_path_parameters = get? ? query_parameters : request_parameters
|
2005-06-26 10:36:13 -04:00
|
|
|
parameters.each do |key, value|
|
2005-07-07 15:23:35 -04:00
|
|
|
if value.is_a? Fixnum
|
|
|
|
value = value.to_s
|
|
|
|
elsif value.is_a? Array
|
2006-06-01 11:42:08 -04:00
|
|
|
value = ActionController::Routing::PathSegment::Result.new(value)
|
2005-07-07 15:23:35 -04:00
|
|
|
end
|
|
|
|
|
2005-06-30 10:49:36 -04:00
|
|
|
if extra_keys.include?(key.to_sym)
|
|
|
|
non_path_parameters[key] = value
|
|
|
|
else
|
2005-07-07 15:23:35 -04:00
|
|
|
path_parameters[key.to_s] = value
|
2005-06-29 05:44:55 -04:00
|
|
|
end
|
2005-06-26 10:36:13 -04:00
|
|
|
end
|
2006-04-22 00:59:15 -04:00
|
|
|
@parameters = nil # reset TestRequest#parameters to use the new path_parameters
|
2005-06-30 10:49:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def recycle!
|
|
|
|
self.request_parameters = {}
|
2006-02-20 18:49:43 -05:00
|
|
|
self.query_parameters = {}
|
|
|
|
self.path_parameters = {}
|
2006-03-10 20:23:29 -05:00
|
|
|
@request_method, @accepts, @content_type = nil, nil, nil
|
2005-06-30 10:49:36 -04:00
|
|
|
end
|
2005-02-14 20:45:35 -05:00
|
|
|
|
2006-12-19 12:11:26 -05:00
|
|
|
def referer
|
|
|
|
@env["HTTP_REFERER"]
|
|
|
|
end
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
private
|
|
|
|
def initialize_containers
|
|
|
|
@env, @cookies = {}, {}
|
|
|
|
end
|
2005-06-29 05:44:55 -04:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def initialize_default_values
|
2004-12-31 11:47:08 -05:00
|
|
|
@host = "test.host"
|
|
|
|
@request_uri = "/"
|
2007-09-06 23:45:54 -04:00
|
|
|
@user_agent = "Rails Testing"
|
2005-06-14 04:04:49 -04:00
|
|
|
self.remote_addr = "0.0.0.0"
|
2004-12-31 11:47:08 -05:00
|
|
|
@env["SERVER_PORT"] = 80
|
2005-09-29 04:43:17 -04:00
|
|
|
@env['REQUEST_METHOD'] = "GET"
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2007-05-15 17:36:21 -04: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
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2005-06-29 05:44:55 -04:00
|
|
|
|
2006-03-08 23:57:08 -05:00
|
|
|
# A refactoring of TestResponse to allow the same behavior to be applied
|
|
|
|
# to the "real" CgiResponse class in integration tests.
|
|
|
|
module TestResponseBehavior #:nodoc:
|
2004-11-23 20:04:44 -05:00
|
|
|
# the response code of the request
|
|
|
|
def response_code
|
|
|
|
headers['Status'][0,3].to_i rescue 0
|
|
|
|
end
|
2005-10-15 14:39:19 -04:00
|
|
|
|
|
|
|
# returns a String to ensure compatibility with Net::HTTPResponse
|
2005-10-15 14:36:41 -04:00
|
|
|
def code
|
|
|
|
headers['Status'].to_s.split(' ')[0]
|
|
|
|
end
|
|
|
|
|
|
|
|
def message
|
|
|
|
headers['Status'].to_s.split(' ',2)[1]
|
|
|
|
end
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
# was the response successful?
|
|
|
|
def success?
|
|
|
|
response_code == 200
|
|
|
|
end
|
|
|
|
|
|
|
|
# was the URL not found?
|
|
|
|
def missing?
|
|
|
|
response_code == 404
|
|
|
|
end
|
|
|
|
|
|
|
|
# were we redirected?
|
|
|
|
def redirect?
|
|
|
|
(300..399).include?(response_code)
|
|
|
|
end
|
2005-06-29 05:44:55 -04:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
# was there a server-side error?
|
2005-04-17 07:38:01 -04:00
|
|
|
def error?
|
2004-11-23 20:04:44 -05:00
|
|
|
(500..599).include?(response_code)
|
|
|
|
end
|
|
|
|
|
2005-04-17 07:38:01 -04:00
|
|
|
alias_method :server_error?, :error?
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
# returns the redirection location or nil
|
|
|
|
def redirect_url
|
2006-11-28 18:25:00 -05:00
|
|
|
headers['Location']
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2005-06-29 05:44:55 -04:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
# does the redirect location match this regexp pattern?
|
|
|
|
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
|
2005-06-29 05:44:55 -04:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
# returns the template path of the file which was used to
|
|
|
|
# render this response (or nil)
|
|
|
|
def rendered_file(with_controller=false)
|
|
|
|
unless template.first_render.nil?
|
|
|
|
unless with_controller
|
|
|
|
template.first_render
|
|
|
|
else
|
|
|
|
template.first_render.split('/').last || template.first_render
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# was this template rendered by a file?
|
|
|
|
def rendered_with_file?
|
|
|
|
!rendered_file.nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
# a shortcut to the flash (or an empty hash if no flash.. hey! that rhymes!)
|
|
|
|
def flash
|
|
|
|
session['flash'] || {}
|
|
|
|
end
|
2005-06-29 05:44:55 -04:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
# do we have a flash?
|
|
|
|
def has_flash?
|
2005-03-20 14:12:53 -05:00
|
|
|
!session['flash'].empty?
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# do we have a flash that has contents?
|
|
|
|
def has_flash_with_contents?
|
|
|
|
!flash.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
# does the specified flash object exist?
|
|
|
|
def has_flash_object?(name=nil)
|
|
|
|
!flash[name].nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
# does the specified object exist in the session?
|
|
|
|
def has_session_object?(name=nil)
|
|
|
|
!session[name].nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
# a shortcut to the template.assigns
|
|
|
|
def template_objects
|
|
|
|
template.assigns || {}
|
|
|
|
end
|
2005-06-29 05:44:55 -04:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
# does the specified template object exist?
|
|
|
|
def has_template_object?(name=nil)
|
|
|
|
!template_objects[name].nil?
|
|
|
|
end
|
2005-06-29 05:44:55 -04:00
|
|
|
|
2005-01-13 09:01:17 -05:00
|
|
|
# Returns the response cookies, converted to a Hash of (name => CGI::Cookie) pairs
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# assert_equal ['AuthorOfNewPage'], r.cookies['author'].value
|
|
|
|
def cookies
|
|
|
|
headers['cookie'].inject({}) { |hash, cookie| hash[cookie.name] = cookie; hash }
|
|
|
|
end
|
|
|
|
|
2005-01-20 09:43:59 -05:00
|
|
|
# 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
|
|
|
|
|
|
|
|
begin
|
|
|
|
$stdout = sio
|
|
|
|
body.call
|
|
|
|
ensure
|
|
|
|
$stdout = STDOUT
|
|
|
|
end
|
|
|
|
|
|
|
|
sio.rewind
|
|
|
|
sio.read
|
|
|
|
end
|
2006-01-20 17:18:39 -05:00
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2006-03-08 23:57:08 -05:00
|
|
|
class TestResponse < AbstractResponse #:nodoc:
|
|
|
|
include TestResponseBehavior
|
|
|
|
end
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
class TestSession #:nodoc:
|
2006-11-13 13:59:01 -05:00
|
|
|
attr_accessor :session_id
|
|
|
|
|
|
|
|
def initialize(attributes = nil)
|
|
|
|
@session_id = ''
|
2004-11-23 20:04:44 -05:00
|
|
|
@attributes = attributes
|
2006-11-13 13:59:01 -05:00
|
|
|
@saved_attributes = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def data
|
|
|
|
@attributes ||= @saved_attributes || {}
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def [](key)
|
2007-02-09 22:55:12 -05:00
|
|
|
data[key]
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def []=(key, value)
|
2007-02-09 22:55:12 -05:00
|
|
|
data[key] = value
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2005-06-29 05:44:55 -04:00
|
|
|
|
2006-11-13 13:59:01 -05:00
|
|
|
def update
|
|
|
|
@saved_attributes = @attributes
|
2004-12-06 13:51:04 -05:00
|
|
|
end
|
2005-06-29 05:44:55 -04:00
|
|
|
|
2006-11-13 13:59:01 -05:00
|
|
|
def delete
|
|
|
|
@attributes = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def close
|
|
|
|
update
|
|
|
|
delete
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2006-11-13 13:59:01 -05:00
|
|
|
|
2006-03-18 15:10:52 -05: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:
|
|
|
|
# post :change_avatar, :avatar => ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + '/files/spongebob.png', 'image/png')
|
2006-08-08 20:11:28 -04:00
|
|
|
require 'tempfile'
|
2006-03-18 15:10:52 -05:00
|
|
|
class TestUploadedFile
|
|
|
|
# The filename, *not* including the path, of the "uploaded" file
|
|
|
|
attr_reader :original_filename
|
2007-03-06 04:46:04 -05:00
|
|
|
|
2006-03-18 15:10:52 -05:00
|
|
|
# The content type of the "uploaded" file
|
|
|
|
attr_reader :content_type
|
2007-03-06 04:46:04 -05:00
|
|
|
|
|
|
|
def initialize(path, content_type = Mime::TEXT)
|
2006-07-30 12:16:46 -04:00
|
|
|
raise "#{path} file does not exist" unless File.exist?(path)
|
2006-03-18 15:10:52 -05:00
|
|
|
@content_type = content_type
|
|
|
|
@original_filename = path.sub(/^.*#{File::SEPARATOR}([^#{File::SEPARATOR}]+)$/) { $1 }
|
|
|
|
@tempfile = Tempfile.new(@original_filename)
|
|
|
|
FileUtils.copy_file(path, @tempfile.path)
|
|
|
|
end
|
2007-03-06 04:46:04 -05:00
|
|
|
|
2006-03-18 15:10:52 -05:00
|
|
|
def path #:nodoc:
|
|
|
|
@tempfile.path
|
|
|
|
end
|
2007-03-06 04:46:04 -05:00
|
|
|
|
2006-03-18 15:10:52 -05:00
|
|
|
alias local_path path
|
2007-03-06 04:46:04 -05:00
|
|
|
|
2006-03-18 15:10:52 -05:00
|
|
|
def method_missing(method_name, *args, &block) #:nodoc:
|
|
|
|
@tempfile.send(method_name, *args, &block)
|
|
|
|
end
|
|
|
|
end
|
2007-03-06 04:46:04 -05:00
|
|
|
|
2006-01-20 17:18:39 -05:00
|
|
|
module TestProcess
|
2006-03-01 11:37:11 -05:00
|
|
|
def self.included(base)
|
|
|
|
# execute the request simulating a specific http method and set/volley the response
|
|
|
|
%w( get post put delete head ).each do |method|
|
|
|
|
base.class_eval <<-EOV, __FILE__, __LINE__
|
|
|
|
def #{method}(action, parameters = nil, session = nil, flash = nil)
|
2006-07-08 14:14:49 -04:00
|
|
|
@request.env['REQUEST_METHOD'] = "#{method.upcase}" if defined?(@request)
|
2006-03-01 11:37:11 -05:00
|
|
|
process(action, parameters, session, flash)
|
|
|
|
end
|
|
|
|
EOV
|
2006-01-20 17:18:39 -05:00
|
|
|
end
|
2006-03-01 11:37:11 -05:00
|
|
|
end
|
2005-02-12 14:35:30 -05:00
|
|
|
|
2006-03-01 11:37:11 -05:00
|
|
|
# execute the request and set/volley the response
|
|
|
|
def process(action, parameters = nil, session = nil, flash = nil)
|
|
|
|
# Sanity check for required instance variables so we can give an
|
|
|
|
# understandable error message.
|
2006-07-08 14:14:49 -04:00
|
|
|
%w(@controller @request @response).each do |iv_name|
|
|
|
|
if !instance_variables.include?(iv_name) || instance_variable_get(iv_name).nil?
|
|
|
|
raise "#{iv_name} is nil: make sure you set it in your test's setup method."
|
|
|
|
end
|
2006-03-01 11:37:11 -05:00
|
|
|
end
|
2005-04-19 12:34:44 -04:00
|
|
|
|
2006-03-01 11:37:11 -05:00
|
|
|
@request.recycle!
|
2005-06-29 05:44:55 -04:00
|
|
|
|
2006-03-01 11:37:11 -05:00
|
|
|
@html_document = nil
|
|
|
|
@request.env['REQUEST_METHOD'] ||= "GET"
|
|
|
|
@request.action = action.to_s
|
2005-03-09 19:36:23 -05:00
|
|
|
|
2006-03-01 11:37:11 -05:00
|
|
|
parameters ||= {}
|
|
|
|
@request.assign_parameters(@controller.class.controller_path, action.to_s, parameters)
|
2005-06-29 05:44:55 -04:00
|
|
|
|
2006-03-01 11:37:11 -05:00
|
|
|
@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)
|
|
|
|
@controller.process(@request, @response)
|
|
|
|
end
|
2005-04-17 07:38:01 -04:00
|
|
|
|
2006-03-01 11:37:11 -05:00
|
|
|
def xml_http_request(request_method, action, parameters = nil, session = nil, flash = nil)
|
|
|
|
@request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
|
2006-03-14 14:53:27 -05:00
|
|
|
@request.env['HTTP_ACCEPT'] = 'text/javascript, text/html, application/xml, text/xml, */*'
|
2006-03-01 11:37:11 -05:00
|
|
|
returning self.send(request_method, action, parameters, session, flash) do
|
|
|
|
@request.env.delete 'HTTP_X_REQUESTED_WITH'
|
2006-03-14 14:53:27 -05:00
|
|
|
@request.env.delete 'HTTP_ACCEPT'
|
2006-01-20 17:18:39 -05:00
|
|
|
end
|
2006-03-01 11:37:11 -05:00
|
|
|
end
|
|
|
|
alias xhr :xml_http_request
|
2005-04-17 07:38:01 -04:00
|
|
|
|
2006-03-01 11:37:11 -05:00
|
|
|
def follow_redirect
|
2006-05-22 18:59:56 -04:00
|
|
|
redirected_controller = @response.redirected_to[:controller]
|
|
|
|
if redirected_controller && redirected_controller != @controller.controller_name
|
|
|
|
raise "Can't follow redirects outside of current controller (from #{@controller.controller_name} to #{redirected_controller})"
|
2006-01-20 17:18:39 -05:00
|
|
|
end
|
|
|
|
|
2006-03-01 11:37:11 -05:00
|
|
|
get(@response.redirected_to.delete(:action), @response.redirected_to.stringify_keys)
|
|
|
|
end
|
2005-04-07 02:22:19 -04:00
|
|
|
|
2006-03-01 14:40:37 -05:00
|
|
|
def assigns(key = nil)
|
|
|
|
if key.nil?
|
|
|
|
@response.template.assigns
|
|
|
|
else
|
|
|
|
@response.template.assigns[key.to_s]
|
|
|
|
end
|
|
|
|
end
|
2006-01-28 19:37:39 -05:00
|
|
|
|
2006-03-01 11:37:11 -05:00
|
|
|
def session
|
|
|
|
@response.session
|
|
|
|
end
|
2005-06-29 05:44:55 -04:00
|
|
|
|
2006-03-01 11:37:11 -05:00
|
|
|
def flash
|
|
|
|
@response.flash
|
|
|
|
end
|
2005-04-17 12:43:48 -04:00
|
|
|
|
2006-03-01 11:37:11 -05:00
|
|
|
def cookies
|
|
|
|
@response.cookies
|
|
|
|
end
|
2005-06-29 05:44:55 -04:00
|
|
|
|
2006-03-01 11:37:11 -05:00
|
|
|
def redirect_to_url
|
|
|
|
@response.redirect_url
|
|
|
|
end
|
2005-04-17 12:43:48 -04:00
|
|
|
|
2006-03-01 11:37:11 -05:00
|
|
|
def build_request_uri(action, parameters)
|
|
|
|
unless @request.env['REQUEST_URI']
|
|
|
|
options = @controller.send(:rewrite_options, parameters)
|
|
|
|
options.update(:only_path => true, :action => action)
|
2005-04-17 12:43:48 -04:00
|
|
|
|
2006-03-01 11:37:11 -05:00
|
|
|
url = ActionController::UrlRewriter.new(@request, parameters)
|
|
|
|
@request.set_REQUEST_URI(url.rewrite(options))
|
2006-01-20 17:18:39 -05:00
|
|
|
end
|
2006-03-01 11:37:11 -05:00
|
|
|
end
|
2006-01-20 17:18:39 -05:00
|
|
|
|
2006-03-01 11:37:11 -05:00
|
|
|
def html_document
|
2007-09-07 21:32:16 -04:00
|
|
|
xml = @response.content_type =~ /xml$/
|
|
|
|
@html_document ||= HTML::Document.new(@response.body, false, xml)
|
2006-03-01 11:37:11 -05:00
|
|
|
end
|
2006-01-20 17:18:39 -05:00
|
|
|
|
2006-03-01 11:37:11 -05:00
|
|
|
def find_tag(conditions)
|
|
|
|
html_document.find(conditions)
|
|
|
|
end
|
2006-01-20 17:18:39 -05:00
|
|
|
|
2006-03-01 11:37:11 -05:00
|
|
|
def find_all_tag(conditions)
|
|
|
|
html_document.find_all(conditions)
|
|
|
|
end
|
2005-07-07 15:23:35 -04:00
|
|
|
|
2006-03-01 11:37:11 -05:00
|
|
|
def method_missing(selector, *args)
|
2006-06-01 11:42:08 -04:00
|
|
|
return @controller.send(selector, *args) if ActionController::Routing::Routes.named_routes.helpers.include?(selector)
|
2006-03-01 11:37:11 -05:00
|
|
|
return super
|
|
|
|
end
|
2006-03-18 15:10:52 -05:00
|
|
|
|
|
|
|
# Shortcut for ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + path, type). Example:
|
|
|
|
# post :change_avatar, :avatar => fixture_file_upload('/files/spongebob.png', 'image/png')
|
|
|
|
def fixture_file_upload(path, mime_type = nil)
|
|
|
|
ActionController::TestUploadedFile.new(
|
2006-03-26 21:24:47 -05:00
|
|
|
Test::Unit::TestCase.respond_to?(:fixture_path) ? Test::Unit::TestCase.fixture_path + path : path,
|
2006-03-18 15:10:52 -05:00
|
|
|
mime_type
|
|
|
|
)
|
|
|
|
end
|
2006-01-20 17:18:39 -05:00
|
|
|
|
2006-03-01 11:37:11 -05:00
|
|
|
# A helper to make it easier to test different route configurations.
|
|
|
|
# This method temporarily replaces ActionController::Routing::Routes
|
|
|
|
# with a new RouteSet instance.
|
|
|
|
#
|
|
|
|
# The new instance is yielded to the passed block. Typically the block
|
|
|
|
# will create some routes using map.draw { map.connect ... }:
|
|
|
|
#
|
2006-06-05 10:14:32 -04: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
|
2006-03-01 11:37:11 -05:00
|
|
|
#
|
|
|
|
def with_routing
|
|
|
|
real_routes = ActionController::Routing::Routes
|
|
|
|
ActionController::Routing.send :remove_const, :Routes
|
|
|
|
|
|
|
|
temporary_routes = ActionController::Routing::RouteSet.new
|
|
|
|
ActionController::Routing.send :const_set, :Routes, temporary_routes
|
|
|
|
|
|
|
|
yield temporary_routes
|
|
|
|
ensure
|
|
|
|
if ActionController::Routing.const_defined? :Routes
|
|
|
|
ActionController::Routing.send(:remove_const, :Routes)
|
2006-01-20 17:18:39 -05:00
|
|
|
end
|
2006-03-01 11:37:11 -05:00
|
|
|
ActionController::Routing.const_set(:Routes, real_routes) if real_routes
|
|
|
|
end
|
2006-01-20 17:18:39 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module Test
|
|
|
|
module Unit
|
|
|
|
class TestCase #:nodoc:
|
|
|
|
include ActionController::TestProcess
|
2005-06-26 01:23:41 -04:00
|
|
|
end
|
2004-12-16 12:45:37 -05:00
|
|
|
end
|
2005-06-29 06:40:26 -04:00
|
|
|
end
|