Module: Rack::Test::Utils

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

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.build_file_part(parameter_name, uploaded_file) ⇒ Object


125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rack/test/utils.rb', line 125

def build_file_part(parameter_name, uploaded_file)
  uploaded_file.set_encoding(Encoding::BINARY)
  buffer = String.new
  buffer << (<<-EOF)
--#{MULTIPART_BOUNDARY}\r
Content-Disposition: form-data; name="#{parameter_name}"; filename="#{escape_path(uploaded_file.original_filename)}"\r
Content-Type: #{uploaded_file.content_type}\r
Content-Length: #{uploaded_file.size}\r
\r
EOF
  uploaded_file.append_to(buffer)
  buffer << "\r\n"
end

.build_multipart(params, first = true, multipart = false) ⇒ Object


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rack/test/utils.rb', line 30

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

    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

  flattened_params = {}

  params.each do |key, value|
    k = first ? key.to_s : "[#{key}]"

    case value
    when Array
      value.map do |v|
        if v.is_a?(Hash)
          nested_params = {}
          build_multipart(v, false).each do |subkey, subvalue|
            nested_params[subkey] = subvalue
          end
          flattened_params["#{k}[]"] ||= []
          flattened_params["#{k}[]"] << nested_params
        else
          flattened_params["#{k}[]"] = value
        end
      end
    when Hash
      build_multipart(value, false).each do |subkey, subvalue|
        flattened_params[k + subkey] = subvalue
      end
    else
      flattened_params[k] = value
    end
  end

  if first
    build_parts(flattened_params)
  else
    flattened_params
  end
end

.build_nested_query(value, prefix = nil) ⇒ Object


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rack/test/utils.rb', line 7

def build_nested_query(value, prefix = nil)
  case value
  when Array
    if value.empty?
      "#{prefix}[]="
    else
      value.map do |v|
        prefix = "#{prefix}[]" unless unescape(prefix) =~ /\[\]$/
        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

.build_parts(parameters) ⇒ Object


86
87
88
# File 'lib/rack/test/utils.rb', line 86

def build_parts(parameters)
  get_parts(parameters).join + "--#{MULTIPART_BOUNDARY}--\r"
end

.build_primitive_part(parameter_name, value) ⇒ Object


115
116
117
118
119
120
121
122
# File 'lib/rack/test/utils.rb', line 115

def build_primitive_part(parameter_name, value)
  <<-EOF
--#{MULTIPART_BOUNDARY}\r
Content-Disposition: form-data; name="#{parameter_name}"\r
\r
#{value}\r
EOF
end

.get_parts(parameters) ⇒ Object


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/rack/test/utils.rb', line 91

def get_parts(parameters)
  parameters.map do |name, value|
    if name =~ /\[\]\Z/ && value.is_a?(Array) && value.all? { |v| v.is_a?(Hash) }
      value.map do |hash|
        new_value = {}
        hash.each { |k, v| new_value[name + k] = v }
        get_parts(new_value).join
      end.join
    else
      [value].flatten.map do |v|
        if v.respond_to?(:original_filename)
          build_file_part(name, v)
        else
          primitive_part = build_primitive_part(name, v)
          # :nocov:
          Rack::Test.encoding_aware_strings? ? primitive_part.force_encoding('BINARY') : primitive_part
          # :nocov:
        end
      end.join
    end
  end
end