From 997ff97405f119ceac7a15b503201b10c2f9bfee Mon Sep 17 00:00:00 2001 From: Simon Rozet Date: Thu, 25 Jun 2009 22:52:09 +0200 Subject: [PATCH] MerbSession now use RackSession under the hood --- History.txt | 6 ++ lib/webrat/core/elements/field.rb | 5 +- lib/webrat/merb_adapter.rb | 70 ++----------------- lib/webrat/merb_multipart_support.rb | 27 ------- spec/private/merb/attaches_file_spec.rb | 93 ------------------------- spec/private/merb/merb_adapter_spec.rb | 61 ---------------- 6 files changed, 13 insertions(+), 249 deletions(-) delete mode 100644 lib/webrat/merb_multipart_support.rb delete mode 100644 spec/private/merb/attaches_file_spec.rb delete mode 100644 spec/private/merb/merb_adapter_spec.rb diff --git a/History.txt b/History.txt index 592baf0..2da27d1 100644 --- a/History.txt +++ b/History.txt @@ -1,3 +1,9 @@ +== Git + +* Minor enhancements + + * Update the Merb support to be based directly on Rack (Simon Rozet) + == 0.5.1 / 2009-08-18 * Minor enhancements diff --git a/lib/webrat/core/elements/field.rb b/lib/webrat/core/elements/field.rb index 8d8afc2..c2812f3 100644 --- a/lib/webrat/core/elements/field.rb +++ b/lib/webrat/core/elements/field.rb @@ -363,10 +363,7 @@ module Webrat else ActionController::TestUploadedFile.new(@value) end - when :merb - # TODO: support content_type - File.new(@value) - when :rack + when :rack, :merb Rack::Test::UploadedFile.new(@value, content_type) end end diff --git a/lib/webrat/merb_adapter.rb b/lib/webrat/merb_adapter.rb index 65caa2a..50633c9 100644 --- a/lib/webrat/merb_adapter.rb +++ b/lib/webrat/merb_adapter.rb @@ -1,72 +1,14 @@ require "webrat" require "merb-core" -require "webrat/merb_multipart_support" +require "webrat/rack" module Webrat - class MerbAdapter #: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 initialize(context=nil); end - - def get(url, data, headers = nil) - do_request(url, data, headers, "GET") + class MerbAdapter < RackAdapter #:nodoc: + def initialize(context=nil) + app = context.respond_to?(:app) ? + context.app : Merb::Rack::Application.new + super(Rack::Test::Session.new(Rack::MockSession.new(app, "www.example.com"))) end - - def post(url, data, headers = nil) - do_request(url, data, headers, "POST") - end - - def put(url, data, headers = nil) - do_request(url, data, headers, "PUT") - end - - def delete(url, data, headers = nil) - do_request(url, data, headers, "DELETE") - end - - def response_body - @response.body.to_s - end - - def response_code - @response.status - end - - def do_request(url, data, headers, method) - if method == "POST" && has_file?(data) - @response = multipart_post(url, data, :headers => headers) - - elsif method == "PUT" && has_file?(data) - @response = multipart_put(url, data, :headers => headers) - - else - @response = request(url, - :params => (data && data.any?) ? data : nil, - :headers => headers, - :method => method) - end - end - - protected - - # Recursively search the data for a file attachment. - def has_file?(data) - data.each do |key, value| - if value.is_a?(Hash) - return has_file?(value) - else - return true if value.is_a?(File) - end - end - return false - end - end end diff --git a/lib/webrat/merb_multipart_support.rb b/lib/webrat/merb_multipart_support.rb deleted file mode 100644 index 646eb99..0000000 --- a/lib/webrat/merb_multipart_support.rb +++ /dev/null @@ -1,27 +0,0 @@ -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/spec/private/merb/attaches_file_spec.rb b/spec/private/merb/attaches_file_spec.rb deleted file mode 100644 index 5c17cfa..0000000 --- a/spec/private/merb/attaches_file_spec.rb +++ /dev/null @@ -1,93 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') - -describe "attach_file with merb" do - before do - Webrat.configuration.mode = :merb - @filename = __FILE__ - end - - it "should fail if no file field found" do - with_html <<-HTML - -
-
- - HTML - lambda { attach_file("Doc", "/some/path") }.should raise_error(Webrat::NotFoundError) - end - - it "should submit empty strings for blank file fields" do - with_html <<-HTML - -
- - -
- - HTML - webrat_session.should_receive(:post).with("/widgets", { "widget" => { "file" => "" } }) - click_button - end - - it "should submit the attached file" do - with_html <<-HTML - -
- - - -
- - HTML - webrat_session.should_receive(:post).with { |path, params| - path.should == "/widgets" - params.should have_key("widget") - params["widget"].should have_key("file") - params["widget"]["file"].should be_an_instance_of(File) - params["widget"]["file"].path.should == @filename - } - attach_file "Document", @filename - click_button - end - - it "should support collections" do - with_html <<-HTML - -
- - - - - -
- - HTML - webrat_session.should_receive(:post).with { |path, params| - path.should == "/widgets" - params.should have_key("widget") - params["widget"].should have_key("files") - params["widget"]["files"][0].should be_an_instance_of(File) - params["widget"]["files"][0].path.should == @filename - params["widget"]["files"][1].should be_an_instance_of(File) - params["widget"]["files"][1].path.should == @filename - } - attach_file "Document", @filename - attach_file "Spreadsheet", @filename - click_button - end - - xit "should allow the content type to be specified" do - with_html <<-HTML - -
- - - -
- - HTML - ActionController::TestUploadedFile.should_receive(:new).with(@filename, "image/png").any_number_of_times - attach_file "Picture", @filename, "image/png" - click_button - end -end diff --git a/spec/private/merb/merb_adapter_spec.rb b/spec/private/merb/merb_adapter_spec.rb deleted file mode 100644 index 9257e35..0000000 --- a/spec/private/merb/merb_adapter_spec.rb +++ /dev/null @@ -1,61 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper") - -require "webrat/merb" - -describe Webrat::MerbAdapter do - it "should not pass empty params if data is and empty hash" do - session = Webrat::MerbAdapter.new - response = OpenStruct.new - response.status = 200 - session.should_receive(:request).with('url', {:params=> nil, :method=>"GET", :headers=>nil}).and_return(response) - session.get('url', {}, nil) - end - - %w{post put delete}.each do |request_method| - it "should call do request with method #{request_method.upcase} for a #{request_method} call" do - session = Webrat::MerbAdapter.new - response = OpenStruct.new - response.status = 200 - - session.should_receive(:request).with('url', {:params=>nil, :method=>request_method.upcase, :headers=>nil}).and_return(response) - session.send(request_method, 'url', {}, nil) - end - end - - %w{post put}.each do |request_method| - it "should call do request with method #{request_method.upcase} with a file attachment" do - session = Webrat::MerbAdapter.new - response = OpenStruct.new - response.status = 200 - - file = File.new(__FILE__) - session.should_receive(:request).with { |path, env| - path.should == "url" - env[:method].should == request_method.upcase - env[:headers].should be_nil - env[:input].should be_an_instance_of(StringIO) - env["CONTENT_LENGTH"].should be_an_instance_of(Fixnum) - env["CONTENT_TYPE"].should match(/multipart.*boundary/) - }.and_return(response) - session.send(request_method, 'url', { :file => file }, nil) - end - end - - context "a session with a response" do - before do - @session = Webrat::MerbAdapter.new - @response = OpenStruct.new - @response.status = 200 - @response.body = 'test response' - @session.instance_variable_set(:@response, @response) - end - - it "should return body of a request as a response_body" do - @session.response_body.should == @response.body - end - - it "should return status of a request as a response_code" do - @session.response_code.should == @response.status - end - end -end