Make TestProcess methods public for access via Integration::Session. Make return values from some of the Integration::Session methods sane.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3724 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jamis Buck
2006-03-01 16:37:11 +00:00
parent 9ded584ec3
commit abeb77b286
2 changed files with 118 additions and 121 deletions

View File

@@ -72,6 +72,7 @@ module ActionController
def https!(flag=true)
@https = flag
initialize_url_writer
@https
end
# Return +true+ if the session is mimicing a secure HTTPS request.
@@ -89,6 +90,7 @@ module ActionController
def host!(name)
@host = name
initialize_url_writer
@host
end
# Follow a single redirect response. If the last response was not a
@@ -97,6 +99,7 @@ module ActionController
def follow_redirect!
raise "not a redirect! #{@status} #{@status_message}" unless redirect?
get(interpret_uri(headers["location"].first))
status
end
# Performs a GET request, following any subsequent redirect. Note that
@@ -106,6 +109,7 @@ module ActionController
def get_via_redirect(path, args={})
get path, args
follow_redirect! while redirect?
status
end
# Performs a POST request, following any subsequent redirect. This is
@@ -113,6 +117,7 @@ module ActionController
def post_via_redirect(path, args={})
post path, args
follow_redirect! while redirect?
status
end
# Returns +true+ if the last response was a redirect.
@@ -217,6 +222,7 @@ module ActionController
@response = @controller.response
parse_result
return status
end
# Parses the result of the response and extracts the various values,
@@ -254,7 +260,7 @@ module ActionController
'QUERY_STRING' => "",
"REQUEST_URI" => "/",
"HTTP_HOST" => host,
"SERVER_PORT" => https? ? "80" : "443",
"SERVER_PORT" => https? ? "443" : "80",
"HTTPS" => https? ? "on" : "off")
@rewriter = ActionController::UrlRewriter.new(ActionController::CgiRequest.new(cgi), {})
end

View File

@@ -274,145 +274,136 @@ module ActionController #:nodoc:
end
module TestProcess
private
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)
@request.env['REQUEST_METHOD'] = "#{method.upcase}" if @request
process(action, parameters, session, flash)
end
EOV
end
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)
@request.env['REQUEST_METHOD'] = "#{method.upcase}" if @request
process(action, parameters, session, flash)
end
EOV
end
end
# 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.
%w(controller request response).each do |iv_name|
raise "@#{iv_name} is nil: make sure you set it in your test's setup method." if instance_variable_get("@#{iv_name}").nil?
end
# 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.
%w(controller request response).each do |iv_name|
raise "@#{iv_name} is nil: make sure you set it in your test's setup method." if instance_variable_get("@#{iv_name}").nil?
end
@request.recycle!
@request.recycle!
@html_document = nil
@request.env['REQUEST_METHOD'] ||= "GET"
@request.action = action.to_s
@html_document = nil
@request.env['REQUEST_METHOD'] ||= "GET"
@request.action = action.to_s
parameters ||= {}
@request.assign_parameters(@controller.class.controller_path, action.to_s, parameters)
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)
@controller.process(@request, @response)
end
@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)
def xml_http_request(request_method, action, parameters = nil, session = nil, flash = nil)
@request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
returning self.send(request_method, action, parameters, session, flash) do
@request.env.delete 'HTTP_X_REQUESTED_WITH'
end
end
alias xhr :xml_http_request
def follow_redirect
if @response.redirected_to[:controller]
raise "Can't follow redirects outside of current controller (#{@response.redirected_to[:controller]})"
end
def xml_http_request(request_method, action, parameters = nil, session = nil, flash = nil)
@request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
returning self.send(request_method, action, parameters, session, flash) do
@request.env.delete 'HTTP_X_REQUESTED_WITH'
end
end
alias xhr :xml_http_request
get(@response.redirected_to.delete(:action), @response.redirected_to.stringify_keys)
end
def follow_redirect
if @response.redirected_to[:controller]
raise "Can't follow redirects outside of current controller (#{@response.redirected_to[:controller]})"
end
def build_request_uri(action, parameters)
options = @controller.send(:rewrite_options, parameters)
options.update(:only_path => true, :action => action)
url = ActionController::UrlRewriter.new(@request, parameters)
@request.set_REQUEST_URI(url.rewrite(options))
end
get(@response.redirected_to.delete(:action), @response.redirected_to.stringify_keys)
end
def session
@response.session
end
def assigns(key = nil)
if key.nil?
@response.template.assigns
else
@response.template.assigns[key.to_s]
end
end
def flash
@response.flash
end
def build_request_uri(action, parameters)
def cookies
@response.cookies
end
def redirect_to_url
@response.redirect_url
end
def build_request_uri(action, parameters)
unless @request.env['REQUEST_URI']
options = @controller.send(:rewrite_options, parameters)
options.update(:only_path => true, :action => action)
url = ActionController::UrlRewriter.new(@request, parameters)
@request.set_REQUEST_URI(url.rewrite(options))
end
end
def session
@response.session
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)
unless @request.env['REQUEST_URI']
options = @controller.send(:rewrite_options, parameters)
options.update(:only_path => true, :action => action)
url = ActionController::UrlRewriter.new(@request, parameters)
@request.set_REQUEST_URI(url.rewrite(options))
end
end
def html_document
@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 method_missing(selector, *args)
return @controller.send(selector, *args) if ActionController::Routing::NamedRoutes::Helpers.include?(selector)
return super
end
# 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 ... }:
#
# with_routing do |set|
# set.draw { set.connect ':controller/:id/:action' }
# assert_equal(
# ['/content/10/show', {}],
# set.generate(:controller => 'content', :id => 10, :action => 'show')
# )
# end
#
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)
end
ActionController::Routing.const_set(:Routes, real_routes) if real_routes
def html_document
@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 method_missing(selector, *args)
return @controller.send(selector, *args) if ActionController::Routing::NamedRoutes::Helpers.include?(selector)
return super
end
# 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 ... }:
#
# with_routing do |set|
# set.draw { set.connect ':controller/:id/:action' }
# assert_equal(
# ['/content/10/show', {}],
# set.generate(:controller => 'content', :id => 10, :action => 'show')
# )
# end
#
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)
end
ActionController::Routing.const_set(:Routes, real_routes) if real_routes
end
end
end