Module | ActionController::TestProcess |
In: |
vendor/rails/actionpack/lib/action_controller/test_process.rb
|
# File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 359 359: def self.included(base) 360: # execute the request simulating a specific http method and set/volley the response 361: %w( get post put delete head ).each do |method| 362: 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__ 363: end 364: end
# File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 416 416: def assigns(key = nil) 417: if key.nil? 418: @response.template.assigns 419: else 420: @response.template.assigns[key.to_s] 421: end 422: end
# File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 440 440: def build_request_uri(action, parameters) 441: unless @request.env['REQUEST_URI'] 442: options = @controller.send!(:rewrite_options, parameters) 443: options.update(:only_path => true, :action => action) 444: 445: url = ActionController::UrlRewriter.new(@request, parameters) 446: @request.set_REQUEST_URI(url.rewrite(options)) 447: end 448: end
# File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 432 432: def cookies 433: @response.cookies 434: end
# File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 459 459: def find_all_tag(conditions) 460: html_document.find_all(conditions) 461: end
# File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 455 455: def find_tag(conditions) 456: html_document.find(conditions) 457: 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 vendor/rails/actionpack/lib/action_controller/test_process.rb, line 473 473: def fixture_file_upload(path, mime_type = nil, binary = false) 474: ActionController::TestUploadedFile.new( 475: Test::Unit::TestCase.respond_to?(:fixture_path) ? Test::Unit::TestCase.fixture_path + path : path, 476: mime_type, 477: binary 478: ) 479: end
# File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 428 428: def flash 429: @response.flash 430: end
# File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 407 407: def follow_redirect 408: redirected_controller = @response.redirected_to[:controller] 409: if redirected_controller && redirected_controller != @controller.controller_name 410: raise "Can't follow redirects outside of current controller (from #{@controller.controller_name} to #{redirected_controller})" 411: end 412: 413: get(@response.redirected_to.delete(:action), @response.redirected_to.stringify_keys) 414: end
# File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 450 450: def html_document 451: xml = @response.content_type =~ /xml$/ 452: @html_document ||= HTML::Document.new(@response.body, false, xml) 453: end
# File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 463 463: def method_missing(selector, *args) 464: return @controller.send!(selector, *args) if ActionController::Routing::Routes.named_routes.helpers.include?(selector) 465: return super 466: end
execute the request and set/volley the response
# File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 373 373: def process(action, parameters = nil, session = nil, flash = nil) 374: # Sanity check for required instance variables so we can give an 375: # understandable error message. 376: %w(@controller @request @response).each do |iv_name| 377: if !(instance_variables.include?(iv_name) || instance_variables.include?(iv_name.to_sym)) || instance_variable_get(iv_name).nil? 378: raise "#{iv_name} is nil: make sure you set it in your test's setup method." 379: end 380: end 381: 382: @request.recycle! 383: 384: @html_document = nil 385: @request.env['REQUEST_METHOD'] ||= "GET" 386: @request.action = action.to_s 387: 388: parameters ||= {} 389: @request.assign_parameters(@controller.class.controller_path, action.to_s, parameters) 390: 391: @request.session = ActionController::TestSession.new(session) unless session.nil? 392: @request.session["flash"] = ActionController::Flash::FlashHash.new.update(flash) if flash 393: build_request_uri(action, parameters) 394: @controller.process(@request, @response) 395: end
# File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 436 436: def redirect_to_url 437: @response.redirect_url 438: end
# File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 424 424: def session 425: @response.session 426: 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 vendor/rails/actionpack/lib/action_controller/test_process.rb, line 498 498: def with_routing 499: real_routes = ActionController::Routing::Routes 500: ActionController::Routing.module_eval { remove_const :Routes } 501: 502: temporary_routes = ActionController::Routing::RouteSet.new 503: ActionController::Routing.module_eval { const_set :Routes, temporary_routes } 504: 505: yield temporary_routes 506: ensure 507: if ActionController::Routing.const_defined? :Routes 508: ActionController::Routing.module_eval { remove_const :Routes } 509: end 510: ActionController::Routing.const_set(:Routes, real_routes) if real_routes 511: end
Alias for xml_http_request
# File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 397 397: def xml_http_request(request_method, action, parameters = nil, session = nil, flash = nil) 398: @request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' 399: @request.env['HTTP_ACCEPT'] = 'text/javascript, text/html, application/xml, text/xml, */*' 400: returning send!(request_method, action, parameters, session, flash) do 401: @request.env.delete 'HTTP_X_REQUESTED_WITH' 402: @request.env.delete 'HTTP_ACCEPT' 403: end 404: end