Changeset 1286

Show
Ignore:
Timestamp:
01/12/08 00:52:49 (8 months ago)
Author:
has.s..@gmail.com
Message:

Adds allows for the get, post, put, and delete helpers to yield the controller instead of the request.

This allows for stubbing on the controller prior to dispatching the action.

Files:

Legend:

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

    r1191 r1286  
    11require 'merb/test/fake_request' 
    22require 'merb/test/hpricot' 
     3require 'merb/test/multipart' 
    34include HpricotTestHelper 
    45 
     
    1516       
    1617      # For integration/functional testing 
    17       def request(verb, path
     18      def request(verb, path, &block
    1819        response = StringIO.new 
    1920        @request = Merb::Test::FakeRequest.with(path, :request_method => (verb.to_s.upcase rescue 'GET')) 
    20    
    21         yield @request if block_given? 
    22    
    23         @controller, @action = Merb::Dispatcher.handle @request, response 
    24       end 
    25  
     21        @request = Merb::Request.new(@request) 
     22 
     23        check_request_for_route(@request) 
     24         
     25        dispatch_fake_request(@request, response, &block) 
     26      end 
    2627       
    2728      # Makes a get request routed to +path+ with any options encoded into the  
     
    6263      def delete(path, opts= {}, &block) 
    6364        request("DELETE", path_with_options(path,opts), &block) 
     65      end 
     66             
     67      # Posts multipart form data to a path 
     68      # pass the +path+ to send and the parameters.  For file uploads, just include a file as the option value. 
     69      # ===Example 
     70      # multipart_post("/my_collection", :foo => "bar", :user => { :login => "joe", :image => File.open("my_image.png")}) 
     71      def multipart_post(path, params = {}, &block) 
     72        multipart_request(path, params.merge!(:request_method => 'POST'), &block) 
     73      end 
     74 
     75      # Posts multipart form data to a path 
     76      # Same as +multipart_post+ but used for PUT(ting) data to the server 
     77      def multipart_put(path, params = {}, &block) 
     78        multipart_request(path, params.merge!(:request_method => 'PUT'), &block) 
    6479      end 
    6580       
     
    131146        path 
    132147      end 
     148       
     149      protected 
     150       
     151      def multipart_request(path, params = {}, &block) 
     152        response = StringIO.new 
     153        request = request_with_multipart_params(path, params) 
     154        check_request_for_route(request) 
     155        dispatch_fake_request(request, response, &block) 
     156      end 
     157       
     158       
     159      def check_request_for_route(request) 
     160        if request.route_params.empty? 
     161          raise ControllerExceptions::BadRequest, "No routes match the request" 
     162        elsif request.controller_name.nil? 
     163          raise ControllerExceptions::BadRequest, "Route matched, but route did not specify a controller"  
     164        end 
     165      end 
     166       
     167      def dispatch_fake_request(request, response, &block) 
     168        klass = request.controller_class 
     169        @controller = klass.build(request, response, 200) 
     170         
     171        @controller.send(:setup_session) 
     172        @controller.stub!(:setup_session).and_return(true) 
     173         
     174        yield @controller if block_given? 
     175 
     176        @controller.dispatch(request.action) 
     177        [@controller, request.action] 
     178         
     179        rescue => exception 
     180          exception = Dispatcher.send(:controller_exception, exception) 
     181          Dispatcher.send(:dispatch_exception, request, response, exception) 
     182      end 
     183       
     184      def request_with_multipart_params(path, params = {}) 
     185        request_method = params.delete(:request_method) || "GET" 
     186        request = Merb::Test::FakeRequest.new(:request_uri => path) 
     187        m = Merb::Test::Multipart::Post.new(params) 
     188        body, head = m.to_multipart 
     189        request['REQUEST_METHOD'] = request_method 
     190        request['CONTENT_TYPE'] = head 
     191        request['CONTENT_LENGTH'] = body.length 
     192        request.post_body = body 
     193        Merb::Request.new(request) 
     194      end 
     195       
    133196    end 
    134197  end 
  • trunk/lib/merb/test/multipart.rb

    r1285 r1286  
    6161        end 
    6262      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         
     63      
    10564    end     
    10665  end 
  • trunk/spec/merb/multipart_spec.rb

    r1285 r1286  
    5252require File.dirname(__FILE__) + '/../fixtures/controllers/render_spec_controllers' 
    5353 
    54 describe Merb::Test::Multipart::TestHelper do 
     54describe Merb::Test::Helper, "Multipart form helpers" do 
    5555   
    5656  before(:each) do 
  • trunk/spec/spec_helper.rb

    r1285 r1286  
    2121  config.include(Merb::Test::Helper) 
    2222  config.include(Merb::Test::RspecMatchers) 
    23   config.include(Merb::Test::Multipart::TestHelper) 
    2423  # config.include(Merb::Test::MerbRspecControllerRedirect)   
    2524end