Sha256: dacef25e75e088ce1cb25bb41b9f7dd2e6ada2170eab9c61dd5a86c8acde49b1

Contents?: true

Size: 1.69 KB

Versions: 52

Compression:

Stored size: 1.69 KB

Contents

module Rack
  module Test

    module Utils
      include Rack::Utils

      def requestify(value, prefix = nil)
        case value
        when Array
          value.map do |v|
            requestify(v, "#{prefix}[]")
          end.join("&")
        when Hash
          value.map do |k, v|
            requestify(v, prefix ? "#{prefix}[#{escape(k)}]" : escape(k))
          end.join("&")
        else
          "#{prefix}=#{escape(value)}"
        end
      end

      module_function :requestify

      def multipart_requestify(params, first=true)
        p = Hash.new

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

          if Hash === value
            multipart_requestify(value, false).each do |subkey, subvalue|
              p[k + subkey] = subvalue
            end
          else
            p[k] = value
          end
        end

        return p
      end

      module_function :multipart_requestify

      def multipart_body(params)
        multipart_requestify(params).map do |key, value|
          if value.respond_to?(:original_filename)
            ::File.open(value.path, "rb") do |f|
              f.set_encoding(Encoding::BINARY) if f.respond_to?(:set_encoding)

              <<-EOF
--#{MULTIPART_BOUNDARY}\r
Content-Disposition: form-data; name="#{key}"; filename="#{escape(value.original_filename)}"\r
Content-Type: #{value.content_type}\r
Content-Length: #{::File.stat(value.path).size}\r
\r
#{f.read}\r
EOF
            end
          else
<<-EOF
--#{MULTIPART_BOUNDARY}\r
Content-Disposition: form-data; name="#{key}"\r
\r
#{value}\r
EOF
          end
        end.join("")+"--#{MULTIPART_BOUNDARY}--\r"
      end

      module_function :multipart_body

    end

  end
end

Version data entries

52 entries across 52 versions & 3 rubygems

Version Path
tdreyno-middleman-0.2.4 vendor/rack-test/lib/rack/test/utils.rb
tdreyno-middleman-0.2.5 vendor/rack-test/lib/rack/test/utils.rb
tdreyno-middleman-0.2.6 vendor/rack-test/lib/rack/test/utils.rb
tdreyno-middleman-0.2.7 vendor/rack-test/lib/rack/test/utils.rb
tdreyno-middleman-0.2.8 vendor/rack-test/lib/rack/test/utils.rb
tdreyno-middleman-0.2.9 vendor/rack-test/lib/rack/test/utils.rb
tdreyno-middleman-0.3.0 vendor/rack-test/lib/rack/test/utils.rb
tdreyno-middleman-0.3.1 vendor/rack-test/lib/rack/test/utils.rb
tdreyno-middleman-0.3.10 vendor/rack-test/lib/rack/test/utils.rb
tdreyno-middleman-0.3.2 vendor/rack-test/lib/rack/test/utils.rb
tdreyno-middleman-0.3.3 vendor/rack-test/lib/rack/test/utils.rb
tdreyno-middleman-0.3.4 vendor/rack-test/lib/rack/test/utils.rb
tdreyno-middleman-0.3.7 vendor/rack-test/lib/rack/test/utils.rb
tdreyno-middleman-0.3.8 vendor/rack-test/lib/rack/test/utils.rb
tdreyno-middleman-0.4.1 vendor/rack-test/lib/rack/test/utils.rb
tdreyno-middleman-0.5.0 vendor/rack-test/lib/rack/test/utils.rb
tdreyno-middleman-0.5.2 vendor/rack-test/lib/rack/test/utils.rb
tdreyno-middleman-0.5.3 vendor/rack-test/lib/rack/test/utils.rb
tdreyno-middleman-0.5.4 vendor/rack-test/lib/rack/test/utils.rb
tdreyno-middleman-0.5.5 vendor/rack-test/lib/rack/test/utils.rb