Class: ActionController::TestRequest

Inherits:
ActionDispatch::TestRequest show all
Defined in:
actionpack/lib/action_controller/test_case.rb

Overview

ActionController::TestCase will be deprecated and moved to a gem in the future. Please use ActionDispatch::IntegrationTest going forward.

Constant Summary collapse

DEFAULT_ENV =

:nodoc:

ActionDispatch::TestRequest::DEFAULT_ENV.dup
ENCODER =
Class.new do
  include Rack::Test::Utils

  def should_multipart?(params)
    # FIXME: lifted from Rack-Test. We should push this separation upstream.
    multipart = false
    query = lambda { |value|
      case value
      when Array
        value.each(&query)
      when Hash
        value.values.each(&query)
      when Rack::Test::UploadedFile
        multipart = true
      end
    }
    params.values.each(&query)
    multipart
  end

  public :build_multipart

  def content_type
    "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}"
  end
end.new

Constants inherited from ActionDispatch::Request

ActionDispatch::Request::ACTION_DISPATCH_REQUEST_ID, ActionDispatch::Request::ENV_METHODS, ActionDispatch::Request::HTTP_METHODS, ActionDispatch::Request::HTTP_METHOD_LOOKUP, ActionDispatch::Request::LOCALHOST, ActionDispatch::Request::PASS_NOT_FOUND, ActionDispatch::Request::RFC2518, ActionDispatch::Request::RFC2616, ActionDispatch::Request::RFC3253, ActionDispatch::Request::RFC3648, ActionDispatch::Request::RFC3744, ActionDispatch::Request::RFC4791, ActionDispatch::Request::RFC5323, ActionDispatch::Request::RFC5789

Constants included from ActionDispatch::PermissionsPolicy::Request

ActionDispatch::PermissionsPolicy::Request::POLICY

Constants included from ActionDispatch::ContentSecurityPolicy::Request

ActionDispatch::ContentSecurityPolicy::Request::NONCE, ActionDispatch::ContentSecurityPolicy::Request::NONCE_DIRECTIVES, ActionDispatch::ContentSecurityPolicy::Request::NONCE_GENERATOR, ActionDispatch::ContentSecurityPolicy::Request::POLICY, ActionDispatch::ContentSecurityPolicy::Request::POLICY_REPORT_ONLY

Constants included from ActionDispatch::Http::URL

ActionDispatch::Http::URL::HOST_REGEXP, ActionDispatch::Http::URL::IP_HOST_REGEXP, ActionDispatch::Http::URL::PROTOCOL_REGEXP

Constants included from ActionDispatch::Http::FilterParameters

ActionDispatch::Http::FilterParameters::ENV_MATCH, ActionDispatch::Http::FilterParameters::NULL_ENV_FILTER, ActionDispatch::Http::FilterParameters::NULL_PARAM_FILTER

Constants included from ActionDispatch::Http::Parameters

ActionDispatch::Http::Parameters::DEFAULT_PARSERS, ActionDispatch::Http::Parameters::PARAMETERS_KEY

Constants included from ActionDispatch::Http::MimeNegotiation

ActionDispatch::Http::MimeNegotiation::RESCUABLE_MIME_FORMAT_ERRORS

Constants included from ActionDispatch::Http::Cache::Request

ActionDispatch::Http::Cache::Request::HTTP_IF_MODIFIED_SINCE, ActionDispatch::Http::Cache::Request::HTTP_IF_NONE_MATCH

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ActionDispatch::TestRequest

#accept=, #action=, #host=, #if_modified_since=, #if_none_match=, #path=, #port=, #remote_addr=, #request_method=, #request_uri=, #user_agent=

Methods inherited from ActionDispatch::Request

#GET, #POST, #authorization, #body, #body_stream, #commit_cookie_jar!, #commit_csrf_token, #commit_flash, #content_length, #controller_class_for, #controller_instance, #controller_instance=, empty, #engine_script_name, #engine_script_name=, #form_data?, #fullpath, #headers, #http_auth_salt, #inspect, #ip, #key?, #local?, #logger, #media_type, #method, #method_symbol, #original_fullpath, #original_url, #raw_post, #raw_request_method, #remote_ip, #remote_ip=, #request_id, #request_id=, #request_method, #request_method=, #request_method_symbol, #request_parameters=, #reset_csrf_token, #reset_session, #route_uri_pattern, #route_uri_pattern=, #routes, #routes=, #send_early_hints, #server_software, #session=, #session_options=, #show_exceptions?, #xml_http_request?

Methods included from ActionDispatch::PermissionsPolicy::Request

#permissions_policy, #permissions_policy=

Methods included from ActionDispatch::ContentSecurityPolicy::Request

#content_security_policy, #content_security_policy=, #content_security_policy_nonce, #content_security_policy_nonce_directives, #content_security_policy_nonce_directives=, #content_security_policy_nonce_generator, #content_security_policy_nonce_generator=, #content_security_policy_report_only, #content_security_policy_report_only=

Methods included from ActionDispatch::Http::URL

#domain, extract_domain, extract_subdomain, extract_subdomains, full_url_for, #host, #host_with_port, #optional_port, path_for, #port, #port_string, #protocol, #raw_host_with_port, #server_port, #standard_port, #standard_port?, #subdomain, #subdomains, #url, url_for

Methods included from ActionDispatch::Http::FilterParameters

#filtered_env, #filtered_parameters, #filtered_path, #parameter_filter

Methods included from ActionDispatch::Http::Parameters

#parameters, #path_parameters, #path_parameters=

Methods included from ActiveSupport::Concern

#append_features, #class_methods, extended, #included, #prepend_features, #prepended

Methods included from ActionDispatch::Http::MimeNegotiation

#accepts, #content_mime_type, #format, #format=, #formats, #formats=, #has_content_type?, #negotiate_mime, #should_apply_vary_header?, #variant, #variant=

Methods included from ActionDispatch::Http::Cache::Request

#etag_matches?, #fresh?, #if_modified_since, #if_none_match, #if_none_match_etags, #not_modified?

Constructor Details

#initialize(env, session, controller_class) ⇒ TestRequest

Returns a new instance of TestRequest.



57
58
59
60
61
62
63
64
65
66
# File 'actionpack/lib/action_controller/test_case.rb', line 57

def initialize(env, session, controller_class)
  super(env)

  self.session = session
  self.session_options = TestSession::DEFAULT_OPTIONS.dup
  @controller_class = controller_class
  @custom_param_parsers = {
    xml: lambda { |raw_post| Hash.from_xml(raw_post)["hash"] }
  }
end

Instance Attribute Details

#controller_classObject (readonly)

Returns the value of attribute controller_class



42
43
44
# File 'actionpack/lib/action_controller/test_case.rb', line 42

def controller_class
  @controller_class
end

Class Method Details

.create(controller_class) ⇒ Object

Create a new test request with default ‘env` values.



45
46
47
48
49
50
# File 'actionpack/lib/action_controller/test_case.rb', line 45

def self.create(controller_class)
  env = {}
  env = Rails.application.env_config.merge(env) if defined?(Rails.application) && Rails.application
  env["rack.request.cookie_hash"] = {}.with_indifferent_access
  new(default_env.merge(env), new_session, controller_class)
end

.new_sessionObject



38
39
40
# File 'actionpack/lib/action_controller/test_case.rb', line 38

def self.new_session
  TestSession.new
end

Instance Method Details

#assign_parameters(routes, controller_path, action, parameters, generated_path, query_string_keys) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'actionpack/lib/action_controller/test_case.rb', line 76

def assign_parameters(routes, controller_path, action, parameters, generated_path, query_string_keys)
  non_path_parameters = {}
  path_parameters = {}

  parameters.each do |key, value|
    if query_string_keys.include?(key)
      non_path_parameters[key] = value
    else
      if value.is_a?(Array)
        value = value.map(&:to_param)
      else
        value = value.to_param
      end

      path_parameters[key.to_sym] = value
    end
  end

  if get?
    if query_string.blank?
      self.query_string = non_path_parameters.to_query
    end
  else
    if ENCODER.should_multipart?(non_path_parameters)
      self.content_type = ENCODER.content_type
      data = ENCODER.build_multipart non_path_parameters
    else
      fetch_header("CONTENT_TYPE") do |k|
        set_header k, "application/x-www-form-urlencoded"
      end

      case content_mime_type.to_sym
      when nil
        raise "Unknown Content-Type: #{content_type}"
      when :json
        data = ActiveSupport::JSON.encode(non_path_parameters)
      when :xml
        data = non_path_parameters.to_xml
      when :url_encoded_form
        data = non_path_parameters.to_query
      else
        @custom_param_parsers[content_mime_type.symbol] = ->(_) { non_path_parameters }
        data = non_path_parameters.to_query
      end
    end

    data_stream = StringIO.new(data)
    set_header "CONTENT_LENGTH", data_stream.length.to_s
    set_header "rack.input", data_stream
  end

  fetch_header("PATH_INFO") do |k|
    set_header k, generated_path
  end
  fetch_header("ORIGINAL_FULLPATH") do |k|
    set_header k, fullpath
  end
  path_parameters[:controller] = controller_path
  path_parameters[:action] = action

  self.path_parameters = path_parameters
end

#content_type=(type) ⇒ Object



72
73
74
# File 'actionpack/lib/action_controller/test_case.rb', line 72

def content_type=(type)
  set_header "CONTENT_TYPE", type
end

#query_string=(string) ⇒ Object



68
69
70
# File 'actionpack/lib/action_controller/test_case.rb', line 68

def query_string=(string)
  set_header Rack::QUERY_STRING, string
end