| Module | ActionController::TestProcess |
| In: |
actionpack/lib/action_controller/test_process.rb
|
# File actionpack/lib/action_controller/test_process.rb, line 365 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 "def \#{method}(action, parameters = nil, session = nil, flash = nil)\n@request.env['REQUEST_METHOD'] = \"\#{method.upcase}\" if defined?(@request)\nprocess(action, parameters, session, flash)\nend\n", __FILE__, __LINE__ end end
# File actionpack/lib/action_controller/test_process.rb, line 422 def assigns(key = nil) if key.nil? @response.template.assigns else @response.template.assigns[key.to_s] end end
# File actionpack/lib/action_controller/test_process.rb, line 446 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
# File actionpack/lib/action_controller/test_process.rb, line 465 def find_all_tag(conditions) html_document.find_all(conditions) end
# File actionpack/lib/action_controller/test_process.rb, line 461 def find_tag(conditions) html_document.find(conditions) end
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')
To upload binary files on Windows, pass :binary as the last parameter. This will not affect other platforms.
post :change_avatar, :avatar => fixture_file_upload('/files/spongebob.png', 'image/png', :binary)
# File actionpack/lib/action_controller/test_process.rb, line 479 def fixture_file_upload(path, mime_type = nil, binary = false) ActionController::TestUploadedFile.new( Test::Unit::TestCase.respond_to?(:fixture_path) ? Test::Unit::TestCase.fixture_path + path : path, mime_type, binary ) end
# File actionpack/lib/action_controller/test_process.rb, line 413 def follow_redirect 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})" end get(@response.redirected_to.delete(:action), @response.redirected_to.stringify_keys) end
# File actionpack/lib/action_controller/test_process.rb, line 456 def html_document xml = @response.content_type =~ /xml$/ @html_document ||= HTML::Document.new(@response.body, false, xml) end
# File actionpack/lib/action_controller/test_process.rb, line 469 def method_missing(selector, *args) return @controller.send(selector, *args) if ActionController::Routing::Routes.named_routes.helpers.include?(selector) return super end
execute the request and set/volley the response
# File actionpack/lib/action_controller/test_process.rb, line 379 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| 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 end @request.recycle! @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) @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
# File actionpack/lib/action_controller/test_process.rb, line 442 def redirect_to_url @response.redirect_url 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 do |map|
map.connect ':controller/:action/:id'
assert_equal(
['/content/10/show', {}],
map.generate(:controller => 'content', :id => 10, :action => 'show')
end
end
end
# File actionpack/lib/action_controller/test_process.rb, line 504 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
Alias for xml_http_request
# File actionpack/lib/action_controller/test_process.rb, line 403 def xml_http_request(request_method, action, parameters = nil, session = nil, flash = nil) @request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' @request.env['HTTP_ACCEPT'] = 'text/javascript, text/html, application/xml, text/xml, */*' returning self.send(request_method, action, parameters, session, flash) do @request.env.delete 'HTTP_X_REQUESTED_WITH' @request.env.delete 'HTTP_ACCEPT' end end