test_process.rb 15.1 KB
Newer Older
1
require 'action_controller/assertions'
2
require 'action_controller/test_case'
D
Initial  
David Heinemeier Hansson 已提交
3 4 5

module ActionController #:nodoc:
  class Base
6 7
    attr_reader :assigns

P
Pratik Naik 已提交
8
    # Process a test request called with a TestRequest object.
D
Initial  
David Heinemeier Hansson 已提交
9 10 11
    def self.process_test(request)
      new.process_test(request)
    end
12

D
Initial  
David Heinemeier Hansson 已提交
13 14 15
    def process_test(request) #:nodoc:
      process(request, TestResponse.new)
    end
16 17 18

    def process_with_test(*args)
      returning process_without_test(*args) do
19 20 21 22 23 24
        @assigns = {}
        (instance_variable_names - @@protected_instance_variables).each do |var|
          value = instance_variable_get(var)
          @assigns[var[1..-1]] = value
          response.template.assigns[var[1..-1]] = value if response
        end
25 26 27
      end
    end

28
    alias_method_chain :process, :test
D
Initial  
David Heinemeier Hansson 已提交
29 30 31
  end

  class TestRequest < AbstractRequest #:nodoc:
32
    attr_accessor :cookies, :session_options
33
    attr_accessor :query_parameters, :request_parameters, :path, :session
34
    attr_accessor :host, :user_agent
D
Initial  
David Heinemeier Hansson 已提交
35 36 37 38 39

    def initialize(query_parameters = nil, request_parameters = nil, session = nil)
      @query_parameters   = query_parameters || {}
      @request_parameters = request_parameters || {}
      @session            = session || TestSession.new
40

D
Initial  
David Heinemeier Hansson 已提交
41 42 43 44 45 46 47
      initialize_containers
      initialize_default_values

      super()
    end

    def reset_session
48
      @session = TestSession.new
49
    end
50

51
    # Wraps raw_post in a StringIO.
52
    def body_stream #:nodoc:
53 54 55 56 57
      StringIO.new(raw_post)
    end

    # Either the RAW_POST_DATA environment variable or the URL-encoded request
    # parameters.
58
    def raw_post
59
      env['RAW_POST_DATA'] ||= returning(url_encoded_request_parameters) { |b| b.force_encoding(Encoding::BINARY) if b.respond_to?(:force_encoding) }
60 61
    end

62 63
    def port=(number)
      @env["SERVER_PORT"] = number.to_i
64
      port(true)
65 66
    end

D
Initial  
David Heinemeier Hansson 已提交
67 68 69 70
    def action=(action_name)
      @query_parameters.update({ "action" => action_name })
      @parameters = nil
    end
71

72 73 74 75 76 77
    # Used to check AbstractRequest's request_uri functionality.
    # Disables the use of @path and @request_uri so superclass can handle those.
    def set_REQUEST_URI(value)
      @env["REQUEST_URI"] = value
      @request_uri = nil
      @path = nil
78 79
      request_uri(true)
      path(true)
80 81
    end

D
Initial  
David Heinemeier Hansson 已提交
82 83 84 85 86
    def request_uri=(uri)
      @request_uri = uri
      @path = uri.split("?").first
    end

87 88
    def accept=(mime_types)
      @env["HTTP_ACCEPT"] = Array(mime_types).collect { |mime_types| mime_types.to_s }.join(",")
89 90 91 92 93 94 95 96 97
      accepts(true)
    end

    def if_modified_since=(last_modified)
      @env["HTTP_IF_MODIFIED_SINCE"] = last_modified
    end

    def if_none_match=(etag)
      @env["HTTP_IF_NONE_MATCH"] = etag
98 99
    end

100 101 102 103
    def remote_addr=(addr)
      @env['REMOTE_ADDR'] = addr
    end

104
    def request_uri(*args)
105
      @request_uri || super
106 107
    end

108
    def path(*args)
109
      @path || super
110
    end
111

112
    def assign_parameters(controller_path, action, parameters)
113
      parameters = parameters.symbolize_keys.merge(:controller => controller_path, :action => action)
114
      extra_keys = ActionController::Routing::Routes.extra_keys(parameters)
115
      non_path_parameters = get? ? query_parameters : request_parameters
116
      parameters.each do |key, value|
117 118 119
        if value.is_a? Fixnum
          value = value.to_s
        elsif value.is_a? Array
120
          value = ActionController::Routing::PathSegment::Result.new(value)
121 122
        end

123 124 125
        if extra_keys.include?(key.to_sym)
          non_path_parameters[key] = value
        else
126
          path_parameters[key.to_s] = value
127
        end
128
      end
129
      @parameters = nil # reset TestRequest#parameters to use the new path_parameters
130 131
    end

132 133
    def recycle!
      self.request_parameters = {}
134 135
      self.query_parameters   = {}
      self.path_parameters    = {}
136
      unmemoize_all
137 138
    end

D
Initial  
David Heinemeier Hansson 已提交
139 140 141 142
    private
      def initialize_containers
        @env, @cookies = {}, {}
      end
143

D
Initial  
David Heinemeier Hansson 已提交
144
      def initialize_default_values
145 146
        @host                    = "test.host"
        @request_uri             = "/"
147
        @user_agent              = "Rails Testing"
148
        self.remote_addr         = "0.0.0.0"
149
        @env["SERVER_PORT"]      = 80
150
        @env['REQUEST_METHOD']   = "GET"
D
Initial  
David Heinemeier Hansson 已提交
151
      end
152 153 154 155 156 157 158 159 160 161 162

      def url_encoded_request_parameters
        params = self.request_parameters.dup

        %w(controller action only_path).each do |k|
          params.delete(k)
          params.delete(k.to_sym)
        end

        params.to_query
      end
D
Initial  
David Heinemeier Hansson 已提交
163
  end
164

165 166 167
  # A refactoring of TestResponse to allow the same behavior to be applied
  # to the "real" CgiResponse class in integration tests.
  module TestResponseBehavior #:nodoc:
168
    # The response code of the request
D
Initial  
David Heinemeier Hansson 已提交
169
    def response_code
170
      status[0,3].to_i rescue 0
D
Initial  
David Heinemeier Hansson 已提交
171
    end
172

173
    # Returns a String to ensure compatibility with Net::HTTPResponse
174
    def code
175
      status.to_s.split(' ')[0]
176 177 178
    end

    def message
179
      status.to_s.split(' ',2)[1]
180 181
    end

182
    # Was the response successful?
D
Initial  
David Heinemeier Hansson 已提交
183
    def success?
184
      (200..299).include?(response_code)
D
Initial  
David Heinemeier Hansson 已提交
185 186
    end

187
    # Was the URL not found?
D
Initial  
David Heinemeier Hansson 已提交
188 189 190 191
    def missing?
      response_code == 404
    end

192
    # Were we redirected?
D
Initial  
David Heinemeier Hansson 已提交
193 194 195
    def redirect?
      (300..399).include?(response_code)
    end
196

197
    # Was there a server-side error?
198
    def error?
D
Initial  
David Heinemeier Hansson 已提交
199 200 201
      (500..599).include?(response_code)
    end

202 203
    alias_method :server_error?, :error?

204
    # Returns the redirection location or nil
D
Initial  
David Heinemeier Hansson 已提交
205
    def redirect_url
206
      headers['Location']
D
Initial  
David Heinemeier Hansson 已提交
207
    end
208

209
    # Does the redirect location match this regexp pattern?
D
Initial  
David Heinemeier Hansson 已提交
210 211 212 213 214 215 216
    def redirect_url_match?( pattern )
      return false if redirect_url.nil?
      p = Regexp.new(pattern) if pattern.class == String
      p = pattern if pattern.class == Regexp
      return false if p.nil?
      p.match(redirect_url) != nil
    end
217

218 219 220 221
    # Returns the template of the file which was used to
    # render this response (or nil)
    def rendered_template
      template._first_render
D
Initial  
David Heinemeier Hansson 已提交
222 223
    end

P
Pratik Naik 已提交
224
    # A shortcut to the flash. Returns an empty hash if no session flash exists.
D
Initial  
David Heinemeier Hansson 已提交
225 226 227
    def flash
      session['flash'] || {}
    end
228

229
    # Do we have a flash?
D
Initial  
David Heinemeier Hansson 已提交
230
    def has_flash?
231
      !session['flash'].empty?
D
Initial  
David Heinemeier Hansson 已提交
232 233
    end

234
    # Do we have a flash that has contents?
D
Initial  
David Heinemeier Hansson 已提交
235 236 237 238
    def has_flash_with_contents?
      !flash.empty?
    end

239
    # Does the specified flash object exist?
D
Initial  
David Heinemeier Hansson 已提交
240 241 242 243
    def has_flash_object?(name=nil)
      !flash[name].nil?
    end

244
    # Does the specified object exist in the session?
D
Initial  
David Heinemeier Hansson 已提交
245 246 247 248
    def has_session_object?(name=nil)
      !session[name].nil?
    end

249
    # A shortcut to the template.assigns
D
Initial  
David Heinemeier Hansson 已提交
250 251 252
    def template_objects
      template.assigns || {}
    end
253

254
    # Does the specified template object exist?
D
Initial  
David Heinemeier Hansson 已提交
255
    def has_template_object?(name=nil)
256
      !template_objects[name].nil?
D
Initial  
David Heinemeier Hansson 已提交
257
    end
258

259
    # Returns the response cookies, converted to a Hash of (name => CGI::Cookie) pairs
260
    #
261
    #   assert_equal ['AuthorOfNewPage'], r.cookies['author'].value
262 263 264 265
    def cookies
      headers['cookie'].inject({}) { |hash, cookie| hash[cookie.name] = cookie; hash }
    end

266 267 268 269 270 271
    # Returns binary content (downloadable file), converted to a String
    def binary_content
      raise "Response body is not a Proc: #{body.inspect}" unless body.kind_of?(Proc)
      require 'stringio'

      sio = StringIO.new
272
      body.call(self, sio)
273 274 275 276

      sio.rewind
      sio.read
    end
277
  end
D
Initial  
David Heinemeier Hansson 已提交
278

P
Pratik Naik 已提交
279 280 281 282 283 284 285
  # Integration test methods such as ActionController::Integration::Session#get
  # and ActionController::Integration::Session#post return objects of class
  # TestResponse, which represent the HTTP response results of the requested
  # controller actions.
  #
  # See AbstractResponse for more information on controller response objects.
  class TestResponse < AbstractResponse
286 287 288
    include TestResponseBehavior
  end

D
Initial  
David Heinemeier Hansson 已提交
289
  class TestSession #:nodoc:
290 291 292 293
    attr_accessor :session_id

    def initialize(attributes = nil)
      @session_id = ''
294
      @attributes = attributes.nil? ? nil : attributes.stringify_keys
295 296 297 298 299
      @saved_attributes = nil
    end

    def data
      @attributes ||= @saved_attributes || {}
D
Initial  
David Heinemeier Hansson 已提交
300 301 302
    end

    def [](key)
303
      data[key.to_s]
D
Initial  
David Heinemeier Hansson 已提交
304 305 306
    end

    def []=(key, value)
307
      data[key.to_s] = value
D
Initial  
David Heinemeier Hansson 已提交
308
    end
309

310 311
    def update
      @saved_attributes = @attributes
312
    end
313

314 315 316 317 318 319 320 321
    def delete
      @attributes = nil
    end

    def close
      update
      delete
    end
D
Initial  
David Heinemeier Hansson 已提交
322
  end
323

324 325 326 327 328 329 330 331
  # 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:
  #   post :change_avatar, :avatar => ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + '/files/spongebob.png', 'image/png')
332
  #
333 334
  # Pass a true third parameter to ensure the uploaded file is opened in binary mode (only required for Windows):
  #   post :change_avatar, :avatar => ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + '/files/spongebob.png', 'image/png', :binary)
335
  require 'tempfile'
336 337 338
  class TestUploadedFile
    # The filename, *not* including the path, of the "uploaded" file
    attr_reader :original_filename
339

340
    # The content type of the "uploaded" file
341
    attr_accessor :content_type
342

343
    def initialize(path, content_type = Mime::TEXT, binary = false)
344
      raise "#{path} file does not exist" unless File.exist?(path)
345 346 347
      @content_type = content_type
      @original_filename = path.sub(/^.*#{File::SEPARATOR}([^#{File::SEPARATOR}]+)$/) { $1 }
      @tempfile = Tempfile.new(@original_filename)
348
      @tempfile.set_encoding(Encoding::BINARY) if @tempfile.respond_to?(:set_encoding)
349
      @tempfile.binmode if binary
350 351
      FileUtils.copy_file(path, @tempfile.path)
    end
352

353 354 355
    def path #:nodoc:
      @tempfile.path
    end
356

357
    alias local_path path
358

359
    def method_missing(method_name, *args, &block) #:nodoc:
360
      @tempfile.send!(method_name, *args, &block)
361 362
    end
  end
363

364
  module TestProcess
365
    def self.included(base)
P
Pratik Naik 已提交
366
      # execute the request simulating a specific HTTP method and set/volley the response
P
Pratik Naik 已提交
367
      # TODO: this should be un-DRY'ed for the sake of API documentation.
368 369 370
      %w( get post put delete head ).each do |method|
        base.class_eval <<-EOV, __FILE__, __LINE__
          def #{method}(action, parameters = nil, session = nil, flash = nil)
371
            @request.env['REQUEST_METHOD'] = "#{method.upcase}" if defined?(@request)
372 373 374
            process(action, parameters, session, flash)
          end
        EOV
375
      end
376
    end
377

378 379 380 381
    # 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.
382
      %w(@controller @request @response).each do |iv_name|
383
        if !(instance_variable_names.include?(iv_name) || instance_variable_names.include?(iv_name.to_sym)) || instance_variable_get(iv_name).nil?
384 385
          raise "#{iv_name} is nil: make sure you set it in your test's setup method."
        end
386
      end
387

388
      @request.recycle!
389

390 391 392
      @html_document = nil
      @request.env['REQUEST_METHOD'] ||= "GET"
      @request.action = action.to_s
393

394 395
      parameters ||= {}
      @request.assign_parameters(@controller.class.controller_path, action.to_s, parameters)
396

397 398 399 400 401
      @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
402

403 404
    def xml_http_request(request_method, action, parameters = nil, session = nil, flash = nil)
      @request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
405
      @request.env['HTTP_ACCEPT'] = 'text/javascript, text/html, application/xml, text/xml, */*'
406
      returning send!(request_method, action, parameters, session, flash) do
407
        @request.env.delete 'HTTP_X_REQUESTED_WITH'
408
        @request.env.delete 'HTTP_ACCEPT'
409
      end
410 411
    end
    alias xhr :xml_http_request
412

413 414 415 416 417 418 419
    def assigns(key = nil)
      if key.nil?
        @response.template.assigns
      else
        @response.template.assigns[key.to_s]
      end
    end
420

421 422 423
    def session
      @response.session
    end
424

425 426 427
    def flash
      @response.flash
    end
428

429 430 431
    def cookies
      @response.cookies
    end
432

433 434 435
    def redirect_to_url
      @response.redirect_url
    end
436

437 438
    def build_request_uri(action, parameters)
      unless @request.env['REQUEST_URI']
439
        options = @controller.send!(:rewrite_options, parameters)
440
        options.update(:only_path => true, :action => action)
441

442 443
        url = ActionController::UrlRewriter.new(@request, parameters)
        @request.set_REQUEST_URI(url.rewrite(options))
444
      end
445
    end
446

447
    def html_document
448 449
      xml = @response.content_type =~ /xml$/
      @html_document ||= HTML::Document.new(@response.body, false, xml)
450
    end
451

452 453 454
    def find_tag(conditions)
      html_document.find(conditions)
    end
455

456 457 458
    def find_all_tag(conditions)
      html_document.find_all(conditions)
    end
459

460
    def method_missing(selector, *args)
461 462 463 464 465
      if ActionController::Routing::Routes.named_routes.helpers.include?(selector)
        @controller.send(selector, *args)
      else
        super
      end
466
    end
467

468 469
    # Shortcut for <tt>ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + path, type)</tt>:
    #
470
    #   post :change_avatar, :avatar => fixture_file_upload('/files/spongebob.png', 'image/png')
471
    #
472 473 474
    # To upload binary files on Windows, pass <tt>:binary</tt> as the last parameter.
    # This will not affect other platforms:
    #
475 476
    #   post :change_avatar, :avatar => fixture_file_upload('/files/spongebob.png', 'image/png', :binary)
    def fixture_file_upload(path, mime_type = nil, binary = false)
477
      ActionController::TestUploadedFile.new(
478
        Test::Unit::TestCase.respond_to?(:fixture_path) ? Test::Unit::TestCase.fixture_path + path : path,
479 480
        mime_type,
        binary
481 482
      )
    end
483

484 485
    # A helper to make it easier to test different route configurations.
    # This method temporarily replaces ActionController::Routing::Routes
486
    # with a new RouteSet instance.
487 488
    #
    # The new instance is yielded to the passed block. Typically the block
489
    # will create some routes using <tt>map.draw { map.connect ... }</tt>:
490
    #
491 492 493 494 495 496 497 498 499
    #   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
500 501 502
    #
    def with_routing
      real_routes = ActionController::Routing::Routes
503
      ActionController::Routing.module_eval { remove_const :Routes }
504 505

      temporary_routes = ActionController::Routing::RouteSet.new
506 507
      ActionController::Routing.module_eval { const_set :Routes, temporary_routes }

508 509 510
      yield temporary_routes
    ensure
      if ActionController::Routing.const_defined? :Routes
511
        ActionController::Routing.module_eval { remove_const :Routes }
512
      end
513 514
      ActionController::Routing.const_set(:Routes, real_routes) if real_routes
    end
515 516 517 518 519 520 521
  end
end

module Test
  module Unit
    class TestCase #:nodoc:
      include ActionController::TestProcess
522
    end
D
David Heinemeier Hansson 已提交
523
  end
524
end