2005-04-17 11:38:01 +00:00
|
|
|
require File.dirname(__FILE__) + '/assertions'
|
|
|
|
require File.dirname(__FILE__) + '/deprecated_assertions'
|
2004-11-24 01:04:44 +00:00
|
|
|
|
2005-01-10 01:17:53 +00:00
|
|
|
if defined?(RAILS_ROOT)
|
|
|
|
# Temporary hack for getting functional tests in Rails running under 1.8.2
|
|
|
|
class Object #:nodoc:
|
|
|
|
alias_method :require_without_load_path_reloading, :require
|
|
|
|
def require(file_name)
|
|
|
|
begin
|
|
|
|
require_without_load_path_reloading(file_name)
|
|
|
|
rescue Object => e
|
|
|
|
ADDITIONAL_LOAD_PATHS.reverse.each { |dir| $:.unshift(dir) if File.directory?(dir) }
|
|
|
|
require_without_load_path_reloading(file_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2004-11-24 01:04:44 +00: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
|
|
|
|
|
|
|
|
def process_test(request) #:nodoc:
|
|
|
|
process(request, TestResponse.new)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestRequest < AbstractRequest #:nodoc:
|
2005-02-07 13:44:48 +00:00
|
|
|
attr_accessor :cookies
|
2005-02-15 01:45:35 +00:00
|
|
|
attr_accessor :query_parameters, :request_parameters, :path, :session, :env
|
|
|
|
attr_accessor :host, :remote_addr
|
2004-11-24 01:04:44 +00:00
|
|
|
|
|
|
|
def initialize(query_parameters = nil, request_parameters = nil, session = nil)
|
|
|
|
@query_parameters = query_parameters || {}
|
|
|
|
@request_parameters = request_parameters || {}
|
|
|
|
@session = session || TestSession.new
|
|
|
|
|
|
|
|
initialize_containers
|
|
|
|
initialize_default_values
|
|
|
|
|
|
|
|
super()
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_session
|
|
|
|
@session = {}
|
|
|
|
end
|
|
|
|
|
2004-12-18 18:01:28 +00:00
|
|
|
def port=(number)
|
|
|
|
@env["SERVER_PORT"] = number.to_i
|
|
|
|
end
|
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
def action=(action_name)
|
|
|
|
@query_parameters.update({ "action" => action_name })
|
|
|
|
@parameters = nil
|
|
|
|
end
|
|
|
|
|
2005-02-15 01:45:35 +00: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-24 01:04:44 +00:00
|
|
|
def request_uri=(uri)
|
|
|
|
@request_uri = uri
|
|
|
|
@path = uri.split("?").first
|
|
|
|
end
|
|
|
|
|
2005-02-15 01:45:35 +00:00
|
|
|
def request_uri
|
|
|
|
@request_uri || super()
|
|
|
|
end
|
|
|
|
|
|
|
|
def path
|
|
|
|
@path || super()
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
private
|
|
|
|
def initialize_containers
|
|
|
|
@env, @cookies = {}, {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize_default_values
|
2004-12-31 16:47:08 +00:00
|
|
|
@host = "test.host"
|
|
|
|
@request_uri = "/"
|
|
|
|
@remote_addr, @remote_ip = "127.0.0.1"
|
|
|
|
@env["SERVER_PORT"] = 80
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestResponse < AbstractResponse #:nodoc:
|
|
|
|
# the response code of the request
|
|
|
|
def response_code
|
|
|
|
headers['Status'][0,3].to_i rescue 0
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
# was there a server-side error?
|
2005-04-17 11:38:01 +00:00
|
|
|
def error?
|
2004-11-24 01:04:44 +00:00
|
|
|
(500..599).include?(response_code)
|
|
|
|
end
|
|
|
|
|
2005-04-17 11:38:01 +00:00
|
|
|
alias_method :server_error?, :error?
|
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
# returns the redirection location or nil
|
|
|
|
def redirect_url
|
|
|
|
redirect? ? headers['location'] : nil
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
# do we have a flash?
|
|
|
|
def has_flash?
|
2005-03-20 19:12:53 +00:00
|
|
|
!session['flash'].empty?
|
2004-11-24 01:04:44 +00: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
|
|
|
|
|
|
|
|
# does the specified template object exist?
|
|
|
|
def has_template_object?(name=nil)
|
|
|
|
!template_objects[name].nil?
|
|
|
|
end
|
2005-01-13 14:01:17 +00: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 14:43:59 +00: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
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
class TestSession #:nodoc:
|
|
|
|
def initialize(attributes = {})
|
|
|
|
@attributes = attributes
|
|
|
|
end
|
|
|
|
|
|
|
|
def [](key)
|
|
|
|
@attributes[key]
|
|
|
|
end
|
|
|
|
|
|
|
|
def []=(key, value)
|
|
|
|
@attributes[key] = value
|
|
|
|
end
|
|
|
|
|
2004-12-06 18:51:04 +00:00
|
|
|
def session_id
|
|
|
|
""
|
|
|
|
end
|
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
def update() end
|
|
|
|
def close() end
|
|
|
|
def delete() @attributes = {} end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-12-16 17:45:37 +00:00
|
|
|
module Test
|
|
|
|
module Unit
|
|
|
|
class TestCase #:nodoc:
|
|
|
|
private
|
|
|
|
# execute the request and set/volley the response
|
2005-03-26 21:43:25 +00:00
|
|
|
def process(action, parameters = nil, session = nil, flash = nil)
|
2005-04-17 16:43:48 +00:00
|
|
|
@html_document = nil
|
2004-12-16 17:45:37 +00:00
|
|
|
@request.env['REQUEST_METHOD'] ||= "GET"
|
|
|
|
@request.action = action.to_s
|
2005-04-17 06:18:33 +00:00
|
|
|
@request.path_parameters = { :controller => @controller.class.controller_path,
|
|
|
|
:action => action.to_s }
|
2004-12-16 17:45:37 +00:00
|
|
|
@request.parameters.update(parameters) unless parameters.nil?
|
|
|
|
@request.session = ActionController::TestSession.new(session) unless session.nil?
|
2005-03-26 21:43:25 +00:00
|
|
|
@request.session["flash"] = ActionController::Flash::FlashHash.new.update(flash) if flash
|
2005-04-07 06:22:19 +00:00
|
|
|
build_request_uri(action, parameters)
|
2004-12-16 17:45:37 +00:00
|
|
|
@controller.process(@request, @response)
|
|
|
|
end
|
2004-12-07 11:10:46 +00:00
|
|
|
|
2004-12-16 17:45:37 +00:00
|
|
|
# execute the request simulating a specific http method and set/volley the response
|
|
|
|
%w( get post put delete head ).each do |method|
|
|
|
|
class_eval <<-EOV
|
2005-04-19 11:04:38 +00:00
|
|
|
def #{method}(action, parameters = nil, session = nil, flash = nil)
|
2004-12-16 17:45:37 +00:00
|
|
|
@request.env['REQUEST_METHOD'] = "#{method.upcase}"
|
2005-04-19 11:04:38 +00:00
|
|
|
process(action, parameters, session, flash)
|
2004-12-16 17:45:37 +00:00
|
|
|
end
|
|
|
|
EOV
|
2004-12-07 11:10:46 +00:00
|
|
|
end
|
2005-02-12 19:35:30 +00:00
|
|
|
|
2005-04-19 16:34:44 +00:00
|
|
|
def xml_http_request(request_method, action, parameters = nil, session = nil, flash = nil)
|
|
|
|
@request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
|
|
|
|
self.send(request_method, action, parameters, session, flash)
|
|
|
|
end
|
|
|
|
alias xhr :xml_http_request
|
|
|
|
|
2005-02-12 19:35:30 +00:00
|
|
|
def follow_redirect
|
|
|
|
if @response.redirected_to[:controller]
|
|
|
|
raise "Can't follow redirects outside of current controller (#{@response.redirected_to[:controller]})"
|
|
|
|
end
|
|
|
|
|
2005-02-12 21:54:34 +00:00
|
|
|
get(@response.redirected_to.delete(:action), @response.redirected_to.stringify_keys)
|
2005-02-12 19:35:30 +00:00
|
|
|
end
|
2005-03-10 00:36:23 +00:00
|
|
|
|
2005-04-17 11:38:01 +00:00
|
|
|
def assigns(key = nil)
|
|
|
|
if key.nil?
|
|
|
|
@response.template.assigns
|
|
|
|
else
|
|
|
|
@response.template.assigns[key.to_s]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def session
|
|
|
|
@response.session
|
|
|
|
end
|
|
|
|
|
|
|
|
def flash
|
|
|
|
@response.flash
|
|
|
|
end
|
|
|
|
|
|
|
|
def cookies
|
|
|
|
@response.cookies
|
|
|
|
end
|
|
|
|
|
|
|
|
def redirect_to_url
|
|
|
|
@response.redirect_url
|
2005-03-10 00:36:23 +00:00
|
|
|
end
|
2005-04-07 06:22:19 +00:00
|
|
|
|
|
|
|
def build_request_uri(action, parameters)
|
|
|
|
return if @request.env['REQUEST_URI']
|
|
|
|
url = ActionController::UrlRewriter.new(@request, parameters)
|
|
|
|
@request.set_REQUEST_URI(
|
|
|
|
url.rewrite(@controller.send(:rewrite_options,
|
|
|
|
(parameters||{}).update(:only_path => true, :action=>action))))
|
|
|
|
end
|
2005-04-17 16:43:48 +00:00
|
|
|
|
|
|
|
def html_document
|
|
|
|
require_html_scanner
|
|
|
|
@html_document ||= HTML::Document.new(@response.body)
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_tag(conditions)
|
|
|
|
html_document.find(conditions)
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_all_tag(conditions)
|
|
|
|
html_document.find_all(conditions)
|
|
|
|
end
|
|
|
|
|
|
|
|
def require_html_scanner
|
|
|
|
return true if defined?(HTML::Document)
|
|
|
|
require 'html/document'
|
|
|
|
rescue LoadError
|
|
|
|
$:.unshift File.dirname(__FILE__) + "/vendor/html-scanner"
|
|
|
|
require 'html/document'
|
|
|
|
end
|
2005-03-10 00:36:23 +00:00
|
|
|
end
|
2004-12-16 17:45:37 +00:00
|
|
|
end
|
2005-03-20 19:12:53 +00:00
|
|
|
end
|