Browse files

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
  • Loading branch information...
1 parent 9ded584 commit abeb77b2868d185ff7604855f2d5cf198bd9a460 @jamis jamis committed Mar 1, 2006
Showing with 112 additions and 115 deletions.
  1. +7 −1 actionpack/lib/action_controller/integration_test.rb
  2. +105 −114 actionpack/lib/action_controller/test_process.rb
View
8 actionpack/lib/action_controller/integration_test.rb
@@ -72,6 +72,7 @@ def reset!
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 @@ def https?
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 @@ def host!(name)
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,13 +109,15 @@ def follow_redirect!
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
# vulnerable to infinite loops, the same as #get_via_redirect.
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 @@ def process(method, path, parameters=nil, headers=nil)
@response = @controller.response
parse_result
+ return status
end
# Parses the result of the response and extracts the various values,
@@ -254,7 +260,7 @@ def initialize_url_writer
'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
219 actionpack/lib/action_controller/test_process.rb
@@ -274,145 +274,136 @@ def delete() @attributes = {} end
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)
+ 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
+ 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
- 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
+ end
+ alias xhr :xml_http_request
- get(@response.redirected_to.delete(:action), @response.redirected_to.stringify_keys)
+ def follow_redirect
+ if @response.redirected_to[:controller]
+ raise "Can't follow redirects outside of current controller (#{@response.redirected_to[:controller]})"
end
- def assigns(key = nil)
- if key.nil?
- @response.template.assigns
- else
- @response.template.assigns[key.to_s]
- end
- end
+ get(@response.redirected_to.delete(:action), @response.redirected_to.stringify_keys)
+ 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
+ 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
- def session
- @response.session
- end
+ def session
+ @response.session
+ end
- def flash
- @response.flash
- end
+ def flash
+ @response.flash
+ end
- def cookies
- @response.cookies
- end
+ def cookies
+ @response.cookies
+ end
- def redirect_to_url
- @response.redirect_url
- 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)
+ 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
+ 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 html_document
+ @html_document ||= HTML::Document.new(@response.body)
+ end
- def find_tag(conditions)
- html_document.find(conditions)
- end
+ def find_tag(conditions)
+ html_document.find(conditions)
+ end
- def find_all_tag(conditions)
- html_document.find_all(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
+ 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
+ # 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

0 comments on commit abeb77b

Please sign in to comment.