From c11f4868a9fc85c966c811428405509e8f2f1c7b Mon Sep 17 00:00:00 2001 From: Ryan Carver Date: Tue, 16 Jun 2009 12:43:17 -0700 Subject: [PATCH] Don't require merb-core/two-oh for multipart support. Instead, copy the code into Webrat --- lib/webrat/merb_multipart_support.rb | 27 +++++++++++++++++++++++++++ lib/webrat/merb_session.rb | 25 +++++++------------------ 2 files changed, 34 insertions(+), 18 deletions(-) create mode 100644 lib/webrat/merb_multipart_support.rb diff --git a/lib/webrat/merb_multipart_support.rb b/lib/webrat/merb_multipart_support.rb new file mode 100644 index 0000000..646eb99 --- /dev/null +++ b/lib/webrat/merb_multipart_support.rb @@ -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 \ No newline at end of file diff --git a/lib/webrat/merb_session.rb b/lib/webrat/merb_session.rb index 6c6ab94..e4b9c56 100644 --- a/lib/webrat/merb_session.rb +++ b/lib/webrat/merb_session.rb @@ -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|