Changeset 1296

Show
Ignore:
Timestamp:
01/14/08 03:46:45 (9 months ago)
Author:
has.s..@gmail.com
Message:

Reverts behaviour of the request test helper method to previous behaviour, but adds an option
to allow yielding of controllers.

Usage:
get(path){|request| ... }
get(path, :yields => :controller){|controller| ...}

You can also supply a fake request to use.
get(path, :yields => :controller, :fake_request => @my_fake_request){|controller| ... }

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/merb/test/helper.rb

    r1288 r1296  
    1616       
    1717      # For integration/functional testing 
    18       def request(verb, path, &block) 
     18      # 
     19      # This helper is the basis of the <tt>get</tt>, <tt>post</tt>, <tt>put</tt>, and <tt>delete</tt> helper 
     20      # 
     21      # By default a fake request is yielded to the block for local modification.  
     22      # +opts+ takes any options that you want pass to the methods, plus some reserved ones 
     23      #  
     24      # ===Yielding 
     25      # You can get the request helper to yield either a fake request object, or a controller 
     26      # Do this with the +:yield+ option.  Values can be +:request+, or +:controller+.  +:request+ is default 
     27      # When you yield the controller, it is available inside the block with the controller method, so you don't need to 
     28      # explicitly set it in the block chute. 
     29      # ====Example 
     30      #   request( :get, '/', :yields => :controller) do |controller| 
     31      #     controller.stub!(:render) 
     32      #   end 
     33      #  
     34      # You can also pass in a fake request object which may be useful if your yielding a controller. 
     35      # Use the opts[:fake_request] to do this.   
     36      # ====Example 
     37      #   request( :get, '/', :yields => :controller, :fake_request => @my_fake_request) do 
     38      #     controller.stub!(:render) 
     39      #   end 
     40      def request(verb, path, opts = {}, &block) 
    1941        response = StringIO.new 
    20         @request = Merb::Test::FakeRequest.with(path, :request_method => (verb.to_s.upcase rescue 'GET')) 
    21         @request = Merb::Request.new(@request) 
    22  
    23         check_request_for_route(@request) 
    24          
    25         dispatch_fake_request(@request, response, &block) 
    26       end 
    27        
     42         
     43        request = opts.delete(:fake_request) || Merb::Test::FakeRequest.with(path, opts.merge(:request_method => (verb.to_s.upcase rescue 'GET'))) 
     44        yield_to_controller = opts.delete(:yields) 
     45         
     46        if yield_to_controller == :controller 
     47          request_yielding_controller(request, response, &block) 
     48        else 
     49          request_yielding_request(request, response, &block) 
     50        end 
     51      end 
     52                 
    2853      # Makes a get request routed to +path+ with any options encoded into the  
    2954      # request url 
     
    3358      # }}} 
    3459      def get(path, opts = {}, &block) 
    35         request("GET",path_with_options(path,opts), &block) 
     60        # request("GET",path_with_options(path,opts), opts &block) 
     61        request("GET", path, opts, &block) 
    3662      end 
    3763       
     
    4369      # }}} 
    4470      def post(path, opts = {}, &block) 
    45         request("POST", path_with_options(path,opts), &block) 
     71        # request("POST", path_with_options(path,opts), &block) 
     72        request("POST",path, opts, &block) 
    4673      end 
    4774       
     
    5380      # }}} 
    5481      def put(path,opts = {}, &block) 
    55         request("PUT", path_with_options(path,opts), &block) 
     82        # request("PUT", path_with_options(path,opts), &block) 
     83        request("PUT",path, opts, &block) 
    5684      end 
    5785       
     
    6290      # }}} 
    6391      def delete(path, opts= {}, &block) 
    64         request("DELETE", path_with_options(path,opts), &block) 
     92        # request("DELETE", path_with_options(path,opts), &block) 
     93        request("DELETE",path, opts, &block) 
    6594      end 
    6695             
     
    149178      protected 
    150179       
     180      def request_yielding_request(request, response, &block) 
     181        # response = StringIO.new 
     182        # @request = Merb::Test::FakeRequest.with(path, :request_method => (verb.to_s.upcase rescue 'GET')) 
     183        @request = request 
     184         
     185        yield @request if block_given? 
     186       
     187        @controller, @action = Merb::Dispatcher.handle @request, response 
     188      end 
     189       
     190      def request_yielding_controller(request, response, &block) 
     191        # response = StringIO.new 
     192        # @request = Merb::Test::FakeRequest.with(path, :request_method => (verb.to_s.upcase rescue 'GET')) 
     193        @request = Merb::Request.new(request) 
     194 
     195        check_request_for_route(@request) 
     196         
     197        dispatch_fake_request(@request, response, &block) 
     198      end 
     199       
    151200      def multipart_request(path, params = {}, &block) 
    152201        response = StringIO.new 
     
    165214      end 
    166215       
     216      # Used for yielding a controller with request and multipart helpers 
    167217      def dispatch_fake_request(request, response, status = 200, &block) 
    168218        klass = request.controller_class 
  • trunk/spec/merb/multipart_spec.rb

    r1293 r1296  
    11require File.dirname(__FILE__) + '/../spec_helper' 
    2 require 'tempfile' 
    32 
    43describe Merb::Test::Multipart::Param, '.to_multipart' do 
     
    1716 
    1817describe Merb::Test::Multipart::Post, '.push_params(params) param parsing' do 
    19   before :each do 
     18  before(:each) do 
    2019    @fake_return_param = mock('fake return_param') 
    2120  end 
    22    
     21 
    2322  it "should create Param from params when param doesn't respond to read" do 
    2423    params = { 'normal' => 'normal_param' } 
     
    5049end 
    5150 
    52 describe Merb::Test::Helper, "Multipart form helpers" do 
    53   before :each do 
    54     @path = "/render_object_controller/render_object.xml" 
    55     Merb::Router.prepare { |r| r.default_routes } 
    56     @mp = Merb::Test::Multipart::Post  
    57     @file = Tempfile.new("spec_file.stuff") 
    58   end 
    59    
    60   it "should setup a multipart request" do 
    61     multipart_request(@path, :foo => 'bario') 
    62     controller.params.keys.should include("foo") 
    63     controller.params["foo"].should == "bario" 
    64   end 
    65    
    66   it "should take a nested hash and do the right thing" do 
    67     multipart_request(@path, :foo => "bario", :bar => { :a => "a", :b => "b"}) 
    68     controller.params.keys.should include("foo") 
    69     controller.params.keys.should include("bar") 
    70     controller.params["foo"].should == "bario" 
    71     controller.params["bar"].should be_a_kind_of(Mash) 
    72     controller.params["bar"]["a"].should == "a" 
    73     controller.params["bar"]["b"].should == "b" 
    74   end 
    75    
    76   it "should take a nested hash with a file upload and do the right thing" do 
    77     multipart_request(@path, :bar => { :a => 'a', :my_file => @file}) 
    78     controller.params.keys.should include('bar') 
    79     controller.params["bar"].keys.should include('a') 
    80     controller.params["bar"].keys.should include('my_file') 
    81     controller.params["bar"]["a"].should == "a" 
    82      
    83     controller.params["bar"]["my_file"].should be_a_kind_of(Mash) 
    84      
    85     file_params = controller.params["bar"]["my_file"] 
    86     file_params["filename"].should == @file.path.split("/").last 
    87     file_params["size"].should == 0 
    88     file_params["tempfile"].should be_a_kind_of(File) 
    89   end 
    90    
    91   it "should expose the controller inside the block" do 
    92     multipart_request(@path, :foo => "bar") do 
    93       controller.should_receive(:dispatch).and_return(true) 
    94     end 
    95   end 
    96    
    97   it "should post the information to the controller" do 
    98     multipart_post(@path, :foo => "bar") 
    99     controller.params["foo"].should == "bar" 
    100     controller.request.should be_post 
    101   end 
    102    
    103   it "should put the information to the controller" do 
    104     multipart_put(@path, :foo => "bar") 
    105     controller.params["foo"].should == "bar" 
    106     controller.request.should be_put 
    107   end 
    108 end 
  • trunk/spec/merb/request_spec.rb

    r1104 r1296  
    273273    request.method.should == :get 
    274274  end 
     275 
    275276   
    276277  it "multipart_params should return an empty hash if the request is not multipart" do