Changeset 1296
- Timestamp:
- 01/14/08 03:46:45 (9 months ago)
- Files:
-
- trunk/lib/merb/test/helper.rb (modified) (7 diffs)
- trunk/spec/merb/multipart_spec.rb (modified) (3 diffs)
- trunk/spec/merb/request_spec.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/lib/merb/test/helper.rb
r1288 r1296 16 16 17 17 # 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) 19 41 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 28 53 # Makes a get request routed to +path+ with any options encoded into the 29 54 # request url … … 33 58 # }}} 34 59 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) 36 62 end 37 63 … … 43 69 # }}} 44 70 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) 46 73 end 47 74 … … 53 80 # }}} 54 81 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) 56 84 end 57 85 … … 62 90 # }}} 63 91 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) 65 94 end 66 95 … … 149 178 protected 150 179 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 151 200 def multipart_request(path, params = {}, &block) 152 201 response = StringIO.new … … 165 214 end 166 215 216 # Used for yielding a controller with request and multipart helpers 167 217 def dispatch_fake_request(request, response, status = 200, &block) 168 218 klass = request.controller_class trunk/spec/merb/multipart_spec.rb
r1293 r1296 1 1 require File.dirname(__FILE__) + '/../spec_helper' 2 require 'tempfile'3 2 4 3 describe Merb::Test::Multipart::Param, '.to_multipart' do … … 17 16 18 17 describe Merb::Test::Multipart::Post, '.push_params(params) param parsing' do 19 before :eachdo18 before(:each) do 20 19 @fake_return_param = mock('fake return_param') 21 20 end 22 21 23 22 it "should create Param from params when param doesn't respond to read" do 24 23 params = { 'normal' => 'normal_param' } … … 50 49 end 51 50 52 describe Merb::Test::Helper, "Multipart form helpers" do53 before :each do54 @path = "/render_object_controller/render_object.xml"55 Merb::Router.prepare { |r| r.default_routes }56 @mp = Merb::Test::Multipart::Post57 @file = Tempfile.new("spec_file.stuff")58 end59 60 it "should setup a multipart request" do61 multipart_request(@path, :foo => 'bario')62 controller.params.keys.should include("foo")63 controller.params["foo"].should == "bario"64 end65 66 it "should take a nested hash and do the right thing" do67 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 end75 76 it "should take a nested hash with a file upload and do the right thing" do77 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("/").last87 file_params["size"].should == 088 file_params["tempfile"].should be_a_kind_of(File)89 end90 91 it "should expose the controller inside the block" do92 multipart_request(@path, :foo => "bar") do93 controller.should_receive(:dispatch).and_return(true)94 end95 end96 97 it "should post the information to the controller" do98 multipart_post(@path, :foo => "bar")99 controller.params["foo"].should == "bar"100 controller.request.should be_post101 end102 103 it "should put the information to the controller" do104 multipart_put(@path, :foo => "bar")105 controller.params["foo"].should == "bar"106 controller.request.should be_put107 end108 endtrunk/spec/merb/request_spec.rb
r1104 r1296 273 273 request.method.should == :get 274 274 end 275 275 276 276 277 it "multipart_params should return an empty hash if the request is not multipart" do
