process.rb 4.0 KB
Newer Older
J
Jeremy Kemper 已提交
1
require 'active_support/core_ext/object/conversions'
2

D
Initial  
David Heinemeier Hansson 已提交
3
module ActionController #:nodoc:
4 5 6 7 8 9 10
  # Essentially generates a modified Tempfile object similar to the object
  # you'd get from the standard library CGI module in a multipart
  # request. This means you can use an ActionController::TestUploadedFile
  # object in the params of a test request in order to simulate
  # a file upload.
  #
  # Usage example, within a functional test:
11
  #   post :change_avatar, :avatar => ActionController::TestUploadedFile.new(ActionController::TestCase.fixture_path + '/files/spongebob.png', 'image/png')
12
  #
13
  # Pass a true third parameter to ensure the uploaded file is opened in binary mode (only required for Windows):
14
  #   post :change_avatar, :avatar => ActionController::TestUploadedFile.new(ActionController::TestCase.fixture_path + '/files/spongebob.png', 'image/png', :binary)
15
  TestUploadedFile = Rack::Test::UploadedFile
16

17
  module TestProcess
18
    def assigns(key = nil)
19 20 21 22
      assigns = {}
      @controller.instance_variable_names.each do |ivar|
        next if ActionController::Base.protected_instance_variables.include?(ivar)
        assigns[ivar[1..-1]] = @controller.instance_variable_get(ivar)
23
      end
24

25
      key.nil? ? assigns : assigns[key.to_s]
26
    end
27

28
    def session
29
      @request.session
30
    end
31

32
    def flash
33
      @request.flash
34
    end
35

36 37 38
    def cookies
      @response.cookies
    end
39

40 41 42
    def redirect_to_url
      @response.redirect_url
    end
43

44
    def html_document
45 46
      xml = @response.content_type =~ /xml$/
      @html_document ||= HTML::Document.new(@response.body, false, xml)
47
    end
48

49 50 51
    def find_tag(conditions)
      html_document.find(conditions)
    end
52

53 54 55
    def find_all_tag(conditions)
      html_document.find_all(conditions)
    end
56

57 58 59
    def method_missing(selector, *args, &block)
      if @controller && ActionController::Routing::Routes.named_routes.helpers.include?(selector)
        @controller.send(selector, *args, &block)
60 61 62
      else
        super
      end
63
    end
64

65
    # Shortcut for <tt>ActionController::TestUploadedFile.new(ActionController::TestCase.fixture_path + path, type)</tt>:
66
    #
67
    #   post :change_avatar, :avatar => fixture_file_upload('/files/spongebob.png', 'image/png')
68
    #
69 70 71
    # To upload binary files on Windows, pass <tt>:binary</tt> as the last parameter.
    # This will not affect other platforms:
    #
72 73
    #   post :change_avatar, :avatar => fixture_file_upload('/files/spongebob.png', 'image/png', :binary)
    def fixture_file_upload(path, mime_type = nil, binary = false)
74 75
      fixture_path = ActionController::TestCase.send(:fixture_path) if ActionController::TestCase.respond_to?(:fixture_path)
      ActionController::TestUploadedFile.new("#{fixture_path}#{path}", mime_type, binary)
76
    end
77

78 79
    # A helper to make it easier to test different route configurations.
    # This method temporarily replaces ActionController::Routing::Routes
80
    # with a new RouteSet instance.
81 82
    #
    # The new instance is yielded to the passed block. Typically the block
83
    # will create some routes using <tt>map.draw { map.connect ... }</tt>:
84
    #
85 86 87 88 89 90 91 92 93
    #   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
94 95 96
    #
    def with_routing
      real_routes = ActionController::Routing::Routes
97
      ActionController::Routing.module_eval { remove_const :Routes }
98 99

      temporary_routes = ActionController::Routing::RouteSet.new
100
      ActionController::Routing.module_eval { const_set :Routes, temporary_routes }
101
      ActionController::Dispatcher.router = temporary_routes
102

103 104 105
      yield temporary_routes
    ensure
      if ActionController::Routing.const_defined? :Routes
106
        ActionController::Routing.module_eval { remove_const :Routes }
107
      end
108
      ActionController::Routing.const_set(:Routes, real_routes) if real_routes
109
      ActionController::Dispatcher.router = ActionController::Routing::Routes
110
    end
111
  end
112
end