Don't require merb-core/two-oh for multipart support. Instead, copy the code into Webrat
This commit is contained in:
parent
1769075c25
commit
c11f4868a9
|
@ -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
|
|
@ -4,13 +4,7 @@ require "cgi"
|
|||
gem "extlib"
|
||||
require "extlib"
|
||||
require "merb-core"
|
||||
|
||||
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
|
||||
require "webrat/merb_multipart_support"
|
||||
|
||||
# HashWithIndifferentAccess = Mash
|
||||
|
||||
|
@ -18,6 +12,10 @@ module Webrat
|
|||
class MerbSession < Session #:nodoc:
|
||||
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
|
||||
|
||||
def get(url, data, headers = nil)
|
||||
|
@ -44,13 +42,11 @@ module Webrat
|
|||
@response.status
|
||||
end
|
||||
|
||||
include Merb::Test::MultipartRequestHelper
|
||||
|
||||
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)
|
||||
|
||||
elsif method == "PUT" && supports_multipart? && has_file?(data)
|
||||
elsif method == "PUT" && has_file?(data)
|
||||
@response = multipart_put(url, data, :headers => headers)
|
||||
|
||||
else
|
||||
|
@ -63,13 +59,6 @@ module Webrat
|
|||
|
||||
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.
|
||||
def has_file?(data)
|
||||
data.each do |key, value|
|
||||
|
|
Loading…
Reference in New Issue