lib/action_controller/test_process.rb in actionpack-1.7.0 vs lib/action_controller/test_process.rb in actionpack-1.8.0

- old
+ new

@@ -1,7 +1,7 @@ -require File.dirname(__FILE__) + '/assertions/action_pack_assertions' -require File.dirname(__FILE__) + '/assertions/active_record_assertions' +require File.dirname(__FILE__) + '/assertions' +require File.dirname(__FILE__) + '/deprecated_assertions' 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 @@ -92,21 +92,10 @@ @env["SERVER_PORT"] = 80 end end class TestResponse < AbstractResponse #:nodoc: - # the class attribute ties a TestResponse to the assertions - class << self - attr_accessor :assertion_target - end - - # initializer - def initialize - TestResponse.assertion_target=self# if TestResponse.assertion_target.nil? - super() - end - # the response code of the request def response_code headers['Status'][0,3].to_i rescue 0 end @@ -124,14 +113,16 @@ def redirect? (300..399).include?(response_code) end # was there a server-side error? - def server_error? + def error? (500..599).include?(response_code) end + alias_method :server_error?, :error? + # returns the redirection location or nil def redirect_url redirect? ? headers['location'] : nil end @@ -250,16 +241,19 @@ module Unit class TestCase #:nodoc: private # execute the request and set/volley the response def process(action, parameters = nil, session = nil, flash = nil) + @html_document = nil @request.env['REQUEST_METHOD'] ||= "GET" @request.action = action.to_s - @request.path_parameters = { :controller => @controller.class.controller_path } + @request.path_parameters = { :controller => @controller.class.controller_path, + :action => action.to_s } @request.parameters.update(parameters) unless parameters.nil? @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 # execute the request simulating a specific http method and set/volley the response %w( get post put delete head ).each do |method| @@ -277,11 +271,60 @@ end get(@response.redirected_to.delete(:action), @response.redirected_to.stringify_keys) end - def assigns(name) - @response.template.assigns[name.to_s] + 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 + end + + 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 + + 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 end end end