Don't require merb-core/two-oh for multipart support. Instead, copy the code into Webrat

This commit is contained in:
Ryan Carver 2009-06-16 12:43:17 -07:00 committed by Bryan Helmkamp
parent 1769075c25
commit c11f4868a9
2 changed files with 34 additions and 18 deletions

View File

@ -0,0 +1,27 @@
module Webrat
# These methods are copied from merb-core/two-oh.rb which defines new
# multipart_post and multipart_put methods for Merb::Test::MultipartRequestHelper.
# We can't require two-oh.rb because it alters Merb's own behavior, causing
# failing specs in Merb when Webrat is required.
module MerbMultipartSupport
def multipart_request(path, params = {}, env = {})
multipart = Merb::Test::MultipartRequestHelper::Post.new(params)
body, head = multipart.to_multipart
env["CONTENT_TYPE"] = head
env["CONTENT_LENGTH"] = body.size
env[:input] = StringIO.new(body)
request(path, env)
end
def multipart_post(path, params = {}, env = {})
env[:method] = "POST"
multipart_request(path, params, env)
end
def multipart_put(path, params = {}, env = {}, &block)
env[:method] = "PUT"
multipart_request(path, params, env)
end
end
end

View File

@ -4,13 +4,7 @@ require "cgi"
gem "extlib" gem "extlib"
require "extlib" require "extlib"
require "merb-core" require "merb-core"
require "webrat/merb_multipart_support"
begin
# Require Merb::Test::MultipartRequestHelper with multipart support.
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
@ -18,6 +12,10 @@ module Webrat
class MerbSession < Session #:nodoc: class MerbSession < Session #:nodoc:
include Merb::Test::MakeRequest include Merb::Test::MakeRequest
# Include Webrat's own version of multipart_post/put because the officially
# supported methods in Merb don't perform the request correctly.
include MerbMultipartSupport
attr_accessor :response attr_accessor :response
def get(url, data, headers = nil) def get(url, data, headers = nil)
@ -44,13 +42,11 @@ module Webrat
@response.status @response.status
end end
include Merb::Test::MultipartRequestHelper
def do_request(url, data, headers, method) def do_request(url, data, headers, method)
if method == "POST" && supports_multipart? && has_file?(data) if method == "POST" && has_file?(data)
@response = multipart_post(url, data, :headers => headers) @response = multipart_post(url, data, :headers => headers)
elsif method == "PUT" && supports_multipart? && has_file?(data) elsif method == "PUT" && has_file?(data)
@response = multipart_put(url, data, :headers => headers) @response = multipart_put(url, data, :headers => headers)
else else
@ -63,13 +59,6 @@ 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|