Module: Rack::Test::Utils

Extended by:
Utils
Includes:
Utils
Included in:
Session, Utils
Defined in:
lib/rack/test/utils.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#build_multipart(params, _first = true, multipart = false) ⇒ Object

Build a multipart body for the given params.

Raises:

  • (ArgumentError)

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rack/test/utils.rb', line 34

def build_multipart(params, _first = true, multipart = false)
  raise ArgumentError, 'value must be a Hash' unless params.is_a?(Hash)

  unless multipart
    query = lambda { |value|
      case value
      when Array
        value.each(&query)
      when Hash
        value.values.each(&query)
      when UploadedFile
        multipart = true
      end
    }
    params.values.each(&query)
    return nil unless multipart
  end

  params = normalize_multipart_params(params, true)

  buffer = String.new
  build_parts(buffer, params)
  buffer
end

#build_nested_query(value, prefix = nil) ⇒ Object

Build a query string for the given value and prefix. The value can be an array or hash of parameters.


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rack/test/utils.rb', line 11

def build_nested_query(value, prefix = nil)
  case value
  when Array
    if value.empty?
      "#{prefix}[]="
    else
      prefix += "[]" unless unescape(prefix).end_with?('[]')
      value.map do |v|
        build_nested_query(v, prefix.to_s)
      end.join('&')
    end
  when Hash
    value.map do |k, v|
      build_nested_query(v, prefix ? "#{prefix}[#{escape(k)}]" : escape(k))
    end.join('&')
  when NilClass
    prefix.to_s
  else
    "#{prefix}=#{escape(value)}"
  end
end