diff options
author | Joshua Peek <josh@joshpeek.com> | 2009-05-19 22:42:59 -0500 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2009-05-19 22:43:22 -0500 |
commit | 6761759a90c08b9db8d5720a8ee4c4329a547caf (patch) | |
tree | b79aad2a0ebae2cd291222a25341c39cf26c1791 /actionpack/lib/action_dispatch/vendor/rack-test/rack/test/utils.rb | |
parent | df0faea378034b031f49995f06c8bf108a0b5530 (diff) | |
download | rails-6761759a90c08b9db8d5720a8ee4c4329a547caf.tar.gz rails-6761759a90c08b9db8d5720a8ee4c4329a547caf.tar.bz2 rails-6761759a90c08b9db8d5720a8ee4c4329a547caf.zip |
Temporarily bundle rack-test while MockSession is baking
Diffstat (limited to 'actionpack/lib/action_dispatch/vendor/rack-test/rack/test/utils.rb')
-rw-r--r-- | actionpack/lib/action_dispatch/vendor/rack-test/rack/test/utils.rb | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/actionpack/lib/action_dispatch/vendor/rack-test/rack/test/utils.rb b/actionpack/lib/action_dispatch/vendor/rack-test/rack/test/utils.rb new file mode 100644 index 0000000000..d25b849709 --- /dev/null +++ b/actionpack/lib/action_dispatch/vendor/rack-test/rack/test/utils.rb @@ -0,0 +1,75 @@ +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 |