Improve forward and backward compatibility for Merb uploads

This commit is contained in:
Ryan Carver 2009-04-24 14:23:00 -07:00
parent 26edfbc7cb
commit 1a110fe908

View File

@ -5,7 +5,12 @@ gem "extlib"
require "extlib" require "extlib"
require "merb-core" require "merb-core"
begin
# Require Merb::Test::MultipartRequestHelper with multipart support.
require "merb-core/two-oh" require "merb-core/two-oh"
rescue LoadError => e
# Maybe Merb got rid of this. We'll do more checking for multiparth support.
end
# HashWithIndifferentAccess = Mash # HashWithIndifferentAccess = Mash
@ -38,13 +43,14 @@ module Webrat
def response_code def response_code
@response.status @response.status
end end
include Merb::Test::MultipartRequestHelper include Merb::Test::MultipartRequestHelper
def do_request(url, data, headers, method) def do_request(url, data, headers, method)
if method == "POST" && has_file?(data) if method == "POST" && supports_multipart? && has_file?(data)
@response = multipart_post(url, data, :headers => headers) @response = multipart_post(url, data, :headers => headers)
elsif method == "PUT" && has_file?(data) elsif method == "PUT" && supports_multipart? && has_file?(data)
@response = multipart_put(url, data, :headers => headers) @response = multipart_put(url, data, :headers => headers)
else else
@ -57,6 +63,13 @@ module Webrat
protected protected
# multipart_post and multipart_put which use request to do their
# business through multipart_request. Older implementations of
# multipart_post and multipart_put use the controller directly.
def supports_multipart?
respond_to?(:multipart_request)
end
# Recursively search the data for a file attachment. # Recursively search the data for a file attachment.
def has_file?(data) def has_file?(data)
data.each do |key, value| data.each do |key, value|