Class: ActionController::TestRequest

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

Overview

:nodoc:

Constant Summary collapse

DEFAULT_ENV =
ActionDispatch::TestRequest::DEFAULT_ENV.dup

Constants inherited from ActionDispatch::Request

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

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::Cache::Request

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

Instance Method Summary collapse

Methods inherited from ActionDispatch::TestRequest

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

Methods inherited from ActionDispatch::Request

#GET, #POST, #authorization, #body, #body_stream, #content_length, #cookie_jar, #delete?, #flash, #form_data?, #fullpath, #get?, #head?, #headers, #ip, #key?, #local?, #media_type, #method, #method_symbol, #original_fullpath, #original_url, #patch?, #post?, #put?, #raw_post, #remote_ip, #request_method, #request_method_symbol, #reset_session, #server_software, #session=, #session_options=, #uuid, #xml_http_request?

Methods included from ActionDispatch::Http::URL

#domain, extract_domain, extract_subdomain, extract_subdomains, #host, #host_with_port, #optional_port, #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

Methods included from ActionDispatch::Http::Parameters

#parameters, #path_parameters, #path_parameters=, #reset_parameters, #symbolized_path_parameters

Methods included from ActionDispatch::Http::MimeNegotiation

#accepts, #content_mime_type, #content_type, #format, #format=, #formats, #formats=, #negotiate_mime

Methods included from ActiveSupport::Concern

#append_features, extended, #included

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 = {}) ⇒ TestRequest

Returns a new instance of TestRequest.



180
181
182
183
184
185
# File 'actionpack/lib/action_controller/test_case.rb', line 180

def initialize(env = {})
  super

  self.session = TestSession.new
  self.session_options = TestSession::DEFAULT_OPTIONS.merge(:id => SecureRandom.hex(16))
end

Instance Method Details

#assign_parameters(routes, controller_path, action, parameters = {}) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'actionpack/lib/action_controller/test_case.rb', line 187

def assign_parameters(routes, controller_path, action, parameters = {})
  parameters = parameters.symbolize_keys.merge(:controller => controller_path, :action => action)
  extra_keys = routes.extra_keys(parameters)
  non_path_parameters = get? ? query_parameters : request_parameters
  parameters.each do |key, value|
    if value.is_a?(Array) && (value.frozen? || value.any?(&:frozen?))
      value = value.map{ |v| v.duplicable? ? v.dup : v }
    elsif value.is_a?(Hash) && (value.frozen? || value.any?{ |k,v| v.frozen? })
      value = Hash[value.map{ |k,v| [k, v.duplicable? ? v.dup : v] }]
    elsif value.frozen? && value.duplicable?
      value = value.dup
    end

    if extra_keys.include?(key.to_sym)
      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_s] = value
    end
  end

  # Clear the combined params hash in case it was already referenced.
  @env.delete("action_dispatch.request.parameters")

  params = self.request_parameters.dup
  %w(controller action only_path).each do |k|
    params.delete(k)
    params.delete(k.to_sym)
  end
  data = params.to_query

  @env['CONTENT_LENGTH'] = data.length.to_s
  @env['rack.input'] = StringIO.new(data)
end

#recycle!Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'actionpack/lib/action_controller/test_case.rb', line 227

def recycle!
  @formats = nil
  @env.delete_if { |k, v| k =~ /^(action_dispatch|rack)\.request/ }
  @env.delete_if { |k, v| k =~ /^action_dispatch\.rescue/ }
  @symbolized_path_params = nil
  @method = @request_method = nil
  @fullpath = @ip = @remote_ip = @protocol = nil
  @env['action_dispatch.request.query_parameters'] = {}
  @set_cookies ||= {}
  @set_cookies.update(Hash[cookie_jar.instance_variable_get("@set_cookies").map{ |k,o| [k,o[:value]] }])
  deleted_cookies = cookie_jar.instance_variable_get("@delete_cookies")
  @set_cookies.reject!{ |k,v| deleted_cookies.include?(k) }
  cookie_jar.update(rack_cookies)
  cookie_jar.update(cookies)
  cookie_jar.update(@set_cookies)
  cookie_jar.recycle!
end