Module: ActionController::TestCase::Behavior

Extended by:
ActiveSupport::Concern
Includes:
ActionDispatch::TestProcess
Included in:
ActionController::TestCase
Defined in:
actionpack/lib/action_controller/test_case.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ActiveSupport::Concern

append_features, extended, included

Methods included from ActionDispatch::TestProcess

#assigns, #cookies, #fixture_file_upload, #flash, #redirect_to_url, #session

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request



339
340
341
# File 'actionpack/lib/action_controller/test_case.rb', line 339

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response



339
340
341
# File 'actionpack/lib/action_controller/test_case.rb', line 339

def response
  @response
end

Instance Method Details

#delete(action, parameters = nil, session = nil, flash = nil) ⇒ Object

Executes a request simulating DELETE HTTP method and set/volley the response



400
401
402
# File 'actionpack/lib/action_controller/test_case.rb', line 400

def delete(action, parameters = nil, session = nil, flash = nil)
  process(action, parameters, session, flash, "DELETE")
end

#get(action, parameters = nil, session = nil, flash = nil) ⇒ Object

Executes a request simulating GET HTTP method and set/volley the response



385
386
387
# File 'actionpack/lib/action_controller/test_case.rb', line 385

def get(action, parameters = nil, session = nil, flash = nil)
  process(action, parameters, session, flash, "GET")
end

#head(action, parameters = nil, session = nil, flash = nil) ⇒ Object

Executes a request simulating HEAD HTTP method and set/volley the response



405
406
407
# File 'actionpack/lib/action_controller/test_case.rb', line 405

def head(action, parameters = nil, session = nil, flash = nil)
  process(action, parameters, session, flash, "HEAD")
end

#paramify_values(hash_or_array_or_value) ⇒ Object



419
420
421
422
423
424
425
426
427
428
429
430
# File 'actionpack/lib/action_controller/test_case.rb', line 419

def paramify_values(hash_or_array_or_value)
  case hash_or_array_or_value
  when Hash
    Hash[hash_or_array_or_value.map{|key, value| [key, paramify_values(value)] }]
  when Array
    hash_or_array_or_value.map {|i| paramify_values(i)}
  when Rack::Test::UploadedFile
    hash_or_array_or_value
  else
    hash_or_array_or_value.to_param
  end
end

#post(action, parameters = nil, session = nil, flash = nil) ⇒ Object

Executes a request simulating POST HTTP method and set/volley the response



390
391
392
# File 'actionpack/lib/action_controller/test_case.rb', line 390

def post(action, parameters = nil, session = nil, flash = nil)
  process(action, parameters, session, flash, "POST")
end

#process(action, parameters = nil, session = nil, flash = nil, http_method = 'GET') ⇒ Object



432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# File 'actionpack/lib/action_controller/test_case.rb', line 432

def process(action, parameters = nil, session = nil, flash = nil, http_method = 'GET')
  # Ensure that numbers and symbols passed as params are converted to
  # proper params, as is the case when engaging rack.
  parameters = paramify_values(parameters) if html_format?(parameters)

  # Sanity check for required instance variables so we can give an
  # understandable error message.
  %w(@routes @controller @request @response).each do |iv_name|
    if !(instance_variable_names.include?(iv_name) || instance_variable_names.include?(iv_name.to_sym)) || 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!
  @response.recycle!
  @controller.response_body = nil
  @controller.formats = nil
  @controller.params = nil

  @html_document = nil
  @request.env['REQUEST_METHOD'] = http_method

  parameters ||= {}
  controller_class_name = @controller.class.anonymous? ?
    "anonymous_controller" :
    @controller.class.name.underscore.sub(/_controller$/, '')

  @request.assign_parameters(@routes, controller_class_name, action.to_s, parameters)

  @request.session = ActionController::TestSession.new(session) if session
  @request.session["flash"] = @request.flash.update(flash || {})
  @request.session["flash"].sweep

  @controller.request = @request
  build_request_uri(action, parameters)
  @controller.class.class_eval { include Testing }
  @controller.recycle!
  @controller.process_with_new_base_test(@request, @response)
  @assigns = @controller.respond_to?(:view_assigns) ? @controller.view_assigns : {}
  @request.session.delete('flash') if @request.session['flash'].blank?
  @response
end

#put(action, parameters = nil, session = nil, flash = nil) ⇒ Object

Executes a request simulating PUT HTTP method and set/volley the response



395
396
397
# File 'actionpack/lib/action_controller/test_case.rb', line 395

def put(action, parameters = nil, session = nil, flash = nil)
  process(action, parameters, session, flash, "PUT")
end

#setup_controller_request_and_responseObject



475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'actionpack/lib/action_controller/test_case.rb', line 475

def setup_controller_request_and_response
  @request = TestRequest.new
  @response = TestResponse.new

  if klass = self.class.controller_class
    @controller ||= klass.new rescue nil
  end

  @request.env.delete('PATH_INFO')

  if defined?(@controller) && @controller
    @controller.request = @request
    @controller.params = {}
  end
end

#xml_http_request(request_method, action, parameters = nil, session = nil, flash = nil) ⇒ Object Also known as: xhr



409
410
411
412
413
414
415
416
# File 'actionpack/lib/action_controller/test_case.rb', line 409

def xml_http_request(request_method, action, parameters = nil, session = nil, flash = nil)
  @request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
  @request.env['HTTP_ACCEPT'] ||=  [Mime::JS, Mime::HTML, Mime::XML, 'text/xml', Mime::ALL].join(', ')
  __send__(request_method, action, parameters, session, flash).tap do
    @request.env.delete 'HTTP_X_REQUESTED_WITH'
    @request.env.delete 'HTTP_ACCEPT'
  end
end