Method: ActionController::TestRequest#assign_parameters

Defined in:
actionpack/lib/action_controller/test_case.rb

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



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
138
139
140
141
# File 'actionpack/lib/action_controller/test_case.rb', line 80

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.b)
    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