module ActionController::TestCase::Behavior

Attributes

request[R]
response[R]

Public Instance Methods

build_response(klass) click to toggle source
# File lib/action_controller/test_case.rb, line 542
def build_response(klass)
  klass.create
end
controller_class_name() click to toggle source
# File lib/action_controller/test_case.rb, line 502
def controller_class_name
  @controller.class.anonymous? ? "anonymous" : @controller.class.controller_path
end
delete(action, **args) click to toggle source

Simulate a DELETE request with the given parameters and set/volley the response. See get for more details.

# File lib/action_controller/test_case.rb, line 423
def delete(action, **args)
  process(action, method: "DELETE", **args)
end
generated_path(generated_extras) click to toggle source
# File lib/action_controller/test_case.rb, line 506
def generated_path(generated_extras)
  generated_extras[0]
end
get(action, **args) click to toggle source

Simulate a GET request with the given parameters.

  • action: The controller action to call.

  • params: The hash with HTTP parameters that you want to pass. This may be nil.

  • body: The request body with a string that is appropriately encoded (application/x-www-form-urlencoded or multipart/form-data).

  • session: A hash of parameters to store in the session. This may be nil.

  • flash: A hash of parameters to store in the flash. This may be nil.

You can also simulate POST, PATCH, PUT, DELETE, and HEAD requests with post, patch, put, delete, and head. Example sending parameters, session and setting a flash message:

get :show,
  params: { id: 7 },
  session: { user_id: 1 },
  flash: { notice: 'This is flash message' }

Note that the request method is not verified. The different methods are available to make the tests more expressive.

# File lib/action_controller/test_case.rb, line 397
def get(action, **args)
  res = process(action, method: "GET", **args)
  cookies.update res.cookies
  res
end
head(action, **args) click to toggle source

Simulate a HEAD request with the given parameters and set/volley the response. See get for more details.

# File lib/action_controller/test_case.rb, line 429
def head(action, **args)
  process(action, method: "HEAD", **args)
end
patch(action, **args) click to toggle source

Simulate a PATCH request with the given parameters and set/volley the response. See get for more details.

# File lib/action_controller/test_case.rb, line 411
def patch(action, **args)
  process(action, method: "PATCH", **args)
end
post(action, **args) click to toggle source

Simulate a POST request with the given parameters and set/volley the response. See get for more details.

# File lib/action_controller/test_case.rb, line 405
def post(action, **args)
  process(action, method: "POST", **args)
end
process(action, method: "GET", params: nil, session: nil, body: nil, flash: {}, format: nil, xhr: false, as: nil) click to toggle source

Simulate an HTTP request to action by specifying request method, parameters and set/volley the response.

  • action: The controller action to call.

  • method: Request method used to send the HTTP request. Possible values are GET, POST, PATCH, PUT, DELETE, HEAD. Defaults to GET. Can be a symbol.

  • params: The hash with HTTP parameters that you want to pass. This may be nil.

  • body: The request body with a string that is appropriately encoded (application/x-www-form-urlencoded or multipart/form-data).

  • session: A hash of parameters to store in the session. This may be nil.

  • flash: A hash of parameters to store in the flash. This may be nil.

  • format: Request format. Defaults to nil. Can be string or symbol.

  • as: Content type. Defaults to nil. Must be a symbol that corresponds to a mime type.

Example calling create action and sending two params:

process :create,
  method: 'POST',
  params: {
    user: { name: 'Gaurish Sharma', email: 'user@example.com' }
  },
  session: { user_id: 1 },
  flash: { notice: 'This is flash message' }

To simulate GET, POST, PATCH, PUT, DELETE and HEAD requests prefer using get, post, patch, put, delete and head methods respectively which will make tests more expressive.

Note that the request method is not verified.

# File lib/action_controller/test_case.rb, line 463
def process(action, method: "GET", params: nil, session: nil, body: nil, flash: {}, format: nil, xhr: false, as: nil)
  check_required_ivars

  action = +action.to_s
  http_method = method.to_s.upcase

  @html_document = nil

  cookies.update(@request.cookies)
  cookies.update_cookies_from_jar
  @request.set_header "HTTP_COOKIE", cookies.to_header
  @request.delete_header "action_dispatch.cookies"

  @request          = TestRequest.new scrub_env!(@request.env), @request.session, @controller.class
  @response         = build_response @response_klass
  @response.request = @request
  @controller.recycle!

  if body
    @request.set_header "RAW_POST_DATA", body
  end

  @request.set_header "REQUEST_METHOD", http_method

  if as
    @request.content_type = Mime[as].to_s
    format ||= as
  end

  parameters = (params || {}).symbolize_keys

  if format
    parameters[:format] = format
  end

  setup_request(controller_class_name, action, parameters, session, flash, xhr)
  process_controller_response(action, cookies, xhr)
end
put(action, **args) click to toggle source

Simulate a PUT request with the given parameters and set/volley the response. See get for more details.

# File lib/action_controller/test_case.rb, line 417
def put(action, **args)
  process(action, method: "PUT", **args)
end
query_parameter_names(generated_extras) click to toggle source
# File lib/action_controller/test_case.rb, line 510
def query_parameter_names(generated_extras)
  generated_extras[1] + [:controller, :action]
end
setup_controller_request_and_response() click to toggle source
# File lib/action_controller/test_case.rb, line 514
def setup_controller_request_and_response
  @controller = nil unless defined? @controller

  @response_klass = ActionDispatch::TestResponse

  if klass = self.class.controller_class
    if klass < ActionController::Live
      @response_klass = LiveTestResponse
    end
    unless @controller
      begin
        @controller = klass.new
      rescue
        warn "could not construct controller #{klass}" if $VERBOSE
      end
    end
  end

  @request          = TestRequest.create(@controller.class)
  @response         = build_response @response_klass
  @response.request = @request

  if @controller
    @controller.request = @request
    @controller.params = {}
  end
end

Private Instance Methods

check_required_ivars() click to toggle source
# File lib/action_controller/test_case.rb, line 625
def check_required_ivars
  # Sanity check for required instance variables so we can give an
  # understandable error message.
  [:@routes, :@controller, :@request, :@response].each do |iv_name|
    if !instance_variable_defined?(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
end
document_root_element() click to toggle source
# File lib/action_controller/test_case.rb, line 621
def document_root_element
  html_document.root
end
process_controller_response(action, cookies, xhr) click to toggle source
# File lib/action_controller/test_case.rb, line 577
def process_controller_response(action, cookies, xhr)
  begin
    @controller.recycle!
    @controller.dispatch(action, @request, @response)
  ensure
    @request = @controller.request
    @response = @controller.response

    if @request.have_cookie_jar?
      unless @request.cookie_jar.committed?
        @request.cookie_jar.write(@response)
        cookies.update(@request.cookie_jar.instance_variable_get(:@cookies))
      end
    end
    @response.prepare!

    if flash_value = @request.flash.to_session_value
      @request.session["flash"] = flash_value
    else
      @request.session.delete("flash")
    end

    if xhr
      @request.delete_header "HTTP_X_REQUESTED_WITH"
      @request.delete_header "HTTP_ACCEPT"
    end
    @request.query_string = ""

    @response.sent!
  end

  @response
end
scrub_env!(env) click to toggle source
# File lib/action_controller/test_case.rb, line 611
def scrub_env!(env)
  env.delete_if do |k, _|
    k.start_with?("rack.request", "action_dispatch.request", "action_dispatch.rescue")
  end
  env["rack.input"] = StringIO.new
  env.delete "CONTENT_LENGTH"
  env.delete "RAW_POST_DATA"
  env
end
setup_request(controller_class_name, action, parameters, session, flash, xhr) click to toggle source
# File lib/action_controller/test_case.rb, line 555
def setup_request(controller_class_name, action, parameters, session, flash, xhr)
  generated_extras = @routes.generate_extras(parameters.merge(controller: controller_class_name, action: action))
  generated_path = generated_path(generated_extras)
  query_string_keys = query_parameter_names(generated_extras)

  @request.assign_parameters(@routes, controller_class_name, action, parameters, generated_path, query_string_keys)

  @request.session.update(session) if session
  @request.flash.update(flash || {})

  if xhr
    @request.set_header "HTTP_X_REQUESTED_WITH", "XMLHttpRequest"
    @request.fetch_header("HTTP_ACCEPT") do |k|
      @request.set_header k, [Mime[:js], Mime[:html], Mime[:xml], "text/xml", "*/*"].join(", ")
    end
  end

  @request.fetch_header("SCRIPT_NAME") do |k|
    @request.set_header k, @controller.config.relative_url_root
  end
end