Hi *,
I need to write a web service that accepts file uploads (even large ones). My WS is REST based, so I have a create method that accepts only POST. My problem is passing the file to the method, so if someone already solved this problem please let me know.
My method is like this:

 def create
   radio = Radio.create!( params[ :radio ] )
ad = Ad.new( params[ :ad ].merge( :user => current_user, :resource => radio ) )
   ad.save!
   respond_to do |format|
     format.html
     format.xml { render :xml => ad.to_xml, :status => :created }
   end
 rescue ActiveRecord::RecordInvalid
   respond_to do |format|
     format.html { render :action => :new }
format.xml { render :xml => "<errors> <error> #{e.message} </ error> </errors>", :status => :unprocessable_entity }
   end
 end

and this is the spec:

  it "should upload radios remotely" do
@request.env[ "HTTP_AUTHORIZATION" ] = "Basic " + Base64::encode64( "quentin:test" )
    puts @request.accepts
    file = MockUpload.new( 'example_radio.mp3' )
    Radio.should_receive( :create! ).with( 'audio' => file )
    post :create, :contest_slug => 'pago', :format => :xml,
      :radio => { :audio => file },
:ad => { :title => 'test 1', :description => 'test test test test test test test',
        :contest => contests( :pago ), :copyright_disclaimer => true }
  end

It answers with a 406 code, so there's something wrong with my code (request.accepts returns */*). My concern is about what gets saved inside the Radio object, I don't think I can pass a raw binary object (AFAIK), and also the way I'm doing things, couldn't find any example on the net, I'm in the dark about how to pass stuff and how to get it, let's not even talk about how to spec it. About rspeccing the whole thing things get even more messy, I think I need to write controller tests that check that the file I begin with is exactly the same I have after using the WS, but isn't it something I shouldn't do in RSpec ? I mean, using real data. In this case I can have the controller behaving exactly as I think it should by calling the right methods in the right way, but having a corrupted file in the fs .... I can't test data in the model either, because corruption will happen exactly inside the controller. I'm new to this stuff, so I'm pretty clueless. Every help is very appreciated.

TIA,
 ngw
_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to