Changeset 1285
- Timestamp:
- 01/11/08 22:43:57 (8 months ago)
- Files:
-
- trunk/app_generators/merb/templates/spec/spec_helper.rb (modified) (1 diff)
- trunk/lib/merb/test/multipart.rb (modified) (2 diffs)
- trunk/spec/merb/multipart_spec.rb (modified) (1 diff)
- trunk/spec/spec_helper.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/app_generators/merb/templates/spec/spec_helper.rb
r1260 r1285 10 10 config.include(Merb::Test::Helper) 11 11 config.include(Merb::Test::RspecMatchers) 12 config.include(Merb::Test::Multipart::TestHelper) 12 13 end 13 14 trunk/lib/merb/test/multipart.rb
r762 r1285 39 39 end 40 40 41 def push_params(params )41 def push_params(params, prefix = nil) 42 42 params.sort_by {|k| k.to_s}.each do |key, value| 43 param_key = prefix.nil? ? key : "#{prefix}[#{key}]" 43 44 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) 45 46 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 47 54 end 48 55 end … … 53 60 return query, CONTENT_TYPE 54 61 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 56 105 end 57 106 end trunk/spec/merb/multipart_spec.rb
r762 r1285 48 48 end 49 49 end 50 51 require 'tempfile' 52 require File.dirname(__FILE__) + '/../fixtures/controllers/render_spec_controllers' 53 54 describe 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 112 end trunk/spec/spec_helper.rb
r1228 r1285 21 21 config.include(Merb::Test::Helper) 22 22 config.include(Merb::Test::RspecMatchers) 23 config.include(Merb::Test::Multipart::TestHelper) 23 24 # config.include(Merb::Test::MerbRspecControllerRedirect) 24 25 end
