Changeset 1285

Show
Ignore:
Timestamp:
01/11/08 22:43:57 (8 months ago)
Author:
has.s..@gmail.com
Message:

Adds some helper methods for multipart testing. Most notably multipart_post, and multipart_put.

See specs/merb/multipart_spec.rb for details.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/app_generators/merb/templates/spec/spec_helper.rb

    r1260 r1285  
    1010    config.include(Merb::Test::Helper) 
    1111    config.include(Merb::Test::RspecMatchers) 
     12    config.include(Merb::Test::Multipart::TestHelper) 
    1213end 
    1314 
  • trunk/lib/merb/test/multipart.rb

    r762 r1285  
    3939        end 
    4040         
    41         def push_params(params
     41        def push_params(params, prefix = nil
    4242          params.sort_by {|k| k.to_s}.each do |key, value| 
     43            param_key = prefix.nil? ? key : "#{prefix}[#{key}]" 
    4344            if value.respond_to?(:read) 
    44               @multipart_params << FileParam.new(key, value.path, value.read) 
     45              @multipart_params << FileParam.new(param_key, value.path, value.read) 
    4546            else 
    46               @multipart_params << Param.new(key, value) 
     47              if value.is_a?(Hash) || value.is_a?(Mash) 
     48                value.keys.each do |k| 
     49                  push_params(value, param_key) 
     50                end 
     51              else 
     52                @multipart_params << Param.new(param_key, value) 
     53              end 
    4754            end 
    4855          end 
     
    5360          return query, CONTENT_TYPE 
    5461        end 
    55       end   
     62      end  
     63       
     64      module TestHelper 
     65 
     66        def multipart_request(path, params = {}, &block) 
     67          request = request_with_multipart_params(path, params) 
     68          check_request_for_route(request) 
     69          klass = request.controller_class 
     70          @controller = klass.build(request, response, 200) 
     71          yield @controller if block_given? 
     72          @controller.dispatch(request.action) 
     73        end 
     74 
     75        def multipart_post(path, params = {}, &block) 
     76          multipart_request(path, params.merge!(:request_method => 'POST'), &block) 
     77        end 
     78 
     79        def multipart_put(path, params = {}, &block) 
     80          multipart_request(path, params.merge!(:request_method => 'PUT'), &block) 
     81        end 
     82 
     83        def request_with_multipart_params(path, params = {}) 
     84          request_method = params.delete(:request_method) || "GET" 
     85          request = Merb::Test::FakeRequest.new(:request_uri => path) 
     86          m = Merb::Test::Multipart::Post.new(params) 
     87          body, head = m.to_multipart 
     88          request['REQUEST_METHOD'] = request_method 
     89          request['CONTENT_TYPE'] = head 
     90          request['CONTENT_LENGTH'] = body.length 
     91          request.post_body = body 
     92          request = Merb::Request.new(request) 
     93        end 
     94 
     95        def check_request_for_route(request) 
     96          if request.route_params.empty? 
     97            raise ControllerExceptions::BadRequest, "No routes match the request" 
     98          elsif request.controller_name.nil? 
     99            raise ControllerExceptions::BadRequest, "Route matched, but route did not specify a controller"  
     100          end 
     101        end 
     102 
     103      end 
     104        
    56105    end     
    57106  end 
  • trunk/spec/merb/multipart_spec.rb

    r762 r1285  
    4848  end 
    4949end 
     50 
     51require 'tempfile' 
     52require File.dirname(__FILE__) + '/../fixtures/controllers/render_spec_controllers' 
     53 
     54describe Merb::Test::Multipart::TestHelper do 
     55   
     56  before(:each) do 
     57    @path = "/render_object_controller/render_object.xml" 
     58    Merb::Router.prepare { |r| r.default_routes } 
     59    @mp = Merb::Test::Multipart::Post  
     60    @file = Tempfile.new("spec_file.stuff") 
     61  end   
     62   
     63  it "should setup a multipart request" do 
     64    multipart_request(@path, :foo => 'bario') 
     65    controller.params.keys.should include("foo") 
     66    controller.params["foo"].should == "bario" 
     67  end 
     68   
     69  it "should take a nested hash and do the right thing" do 
     70    multipart_request(@path, :foo => "bario", :bar => { :a => "a", :b => "b"}) 
     71    controller.params.keys.should include("foo") 
     72    controller.params.keys.should include("bar") 
     73    controller.params["foo"].should == "bario" 
     74    controller.params["bar"].should be_a_kind_of(Mash) 
     75    controller.params["bar"]["a"].should == "a" 
     76    controller.params["bar"]["b"].should == "b" 
     77  end 
     78   
     79  it "should take a nested hash with a file upload and do the right thing" do 
     80    multipart_request(@path, :bar => { :a => 'a', :my_file => @file}) 
     81    controller.params.keys.should include('bar') 
     82    controller.params["bar"].keys.should include('a') 
     83    controller.params["bar"].keys.should include('my_file') 
     84    controller.params["bar"]["a"].should == "a" 
     85     
     86    controller.params["bar"]["my_file"].should be_a_kind_of(Mash) 
     87     
     88    file_params = controller.params["bar"]["my_file"] 
     89    file_params["filename"].should == @file.path.split("/").last 
     90    file_params["size"].should == 0 
     91    file_params["tempfile"].should be_a_kind_of(File) 
     92  end 
     93   
     94  it "should expose the controller inside the block" do 
     95    multipart_request(@path, :foo => "bar") do 
     96      controller.should_receive(:dispatch).and_return(true) 
     97    end     
     98  end 
     99   
     100  it "should post the information to the controller" do 
     101    multipart_post(@path, :foo => "bar") 
     102    controller.params["foo"].should == "bar" 
     103    controller.request.should be_post 
     104  end 
     105   
     106  it "should put the information to the controller" do 
     107    multipart_put(@path, :foo => "bar") 
     108    controller.params["foo"].should == "bar" 
     109    controller.request.should be_put 
     110  end 
     111   
     112end 
  • trunk/spec/spec_helper.rb

    r1228 r1285  
    2121  config.include(Merb::Test::Helper) 
    2222  config.include(Merb::Test::RspecMatchers) 
     23  config.include(Merb::Test::Multipart::TestHelper) 
    2324  # config.include(Merb::Test::MerbRspecControllerRedirect)   
    2425end