spec/cases/uploadable_io_spec.rb in koala-1.1.0 vs spec/cases/uploadable_io_spec.rb in koala-1.2.0beta1

- old
+ new

@@ -11,72 +11,76 @@ describe "Koala::UploadableIO" do def rails_3_mocks tempfile = stub('Tempfile', :path => "foo") uploaded_file = stub('ActionDispatch::Http::UploadedFile', :content_type => true, - :tempfile => tempfile + :tempfile => tempfile, + :original_filename => "bar" ) tempfile.stub!(:respond_to?).with(:path).and_return(true) [tempfile, uploaded_file] end - + def sinatra_mocks - {:type => "type", :tempfile => "Tempfile"} + {:type => "type", :tempfile => "Tempfile", :filename => "foo.bar"} end - + describe "the constructor" do describe "when given a file path" do before(:each) do - @koala_io_params = [File.open(BEACH_BALL_PATH)] + @path = BEACH_BALL_PATH + @koala_io_params = [@path] end describe "and a content type" do before :each do - @koala_io_params.concat([stub("image/jpg")]) + @stub_type = stub("image/jpg") + @koala_io_params.concat([@stub_type]) end - it "should return an UploadIO with the same file path" do - stub_path = @koala_io_params[0] = "/stub/path/to/file" - Koala::UploadableIO.new(*@koala_io_params).io_or_path.should == stub_path + it "returns an UploadIO with the same file path" do + Koala::UploadableIO.new(*@koala_io_params).io_or_path.should == @path end - it "should return an UploadIO with the same content type" do - stub_type = @koala_io_params[1] = stub('Content Type') - Koala::UploadableIO.new(*@koala_io_params).content_type.should == stub_type + it "returns an UploadIO with the same content type" do + Koala::UploadableIO.new(*@koala_io_params).content_type.should == @stub_type end - it "should detect that NetHTTPService must be used" do - @koala_io_params[0] = mock - @koala_io_params[0].stub!(:read) - Koala::UploadableIO.new(*@koala_io_params).requires_base_http_service.should be_true + it "returns an UploadIO with the file's name" do + Koala::UploadableIO.new(*@koala_io_params).filename.should == File.basename(@path) end end describe "and no content type" do it_should_behave_like "determining a mime type" end end describe "when given a File object" do before(:each) do - @koala_io_params = [File.open(BEACH_BALL_PATH)] + @file = File.open(BEACH_BALL_PATH) + @koala_io_params = [@file] end describe "and a content type" do before :each do @koala_io_params.concat(["image/jpg"]) end - it "should return an UploadIO with the same io" do + it "returns an UploadIO with the same io" do Koala::UploadableIO.new(*@koala_io_params).io_or_path.should == @koala_io_params[0] end - it "should return an UplaodIO with the same content_type" do + it "returns an UploadableIO with the same content_type" do content_stub = @koala_io_params[1] = stub('Content Type') Koala::UploadableIO.new(*@koala_io_params).content_type.should == content_stub end + + it "returns an UploadableIO with the right filename" do + Koala::UploadableIO.new(*@koala_io_params).filename.should == File.basename(@file.path) + end end describe "and no content type" do it_should_behave_like "determining a mime type" end @@ -85,42 +89,51 @@ describe "when given a Rails 3 ActionDispatch::Http::UploadedFile" do before(:each) do @tempfile, @uploaded_file = rails_3_mocks end - it "should get the content type via the content_type method" do + it "gets the path from the tempfile associated with the UploadedFile" do + expected_path = stub('Tempfile') + @tempfile.should_receive(:path).and_return(expected_path) + Koala::UploadableIO.new(@uploaded_file).io_or_path.should == expected_path + end + + it "gets the content type via the content_type method" do expected_content_type = stub('Content Type') @uploaded_file.should_receive(:content_type).and_return(expected_content_type) Koala::UploadableIO.new(@uploaded_file).content_type.should == expected_content_type end - it "should get the path from the tempfile associated with the UploadedFile" do - expected_path = stub('Tempfile') - @tempfile.should_receive(:path).and_return(expected_path) - Koala::UploadableIO.new(@uploaded_file).io_or_path.should == expected_path + it "gets the filename from the UploadedFile" do + Koala::UploadableIO.new(@uploaded_file).filename.should == @uploaded_file.original_filename end end describe "when given a Sinatra file parameter hash" do before(:each) do @file_hash = sinatra_mocks end - it "should get the content type from the :type key" do + it "gets the io_or_path from the :tempfile key" do + expected_file = stub('File') + @file_hash[:tempfile] = expected_file + + uploadable = Koala::UploadableIO.new(@file_hash) + uploadable.io_or_path.should == expected_file + end + + it "gets the content type from the :type key" do expected_content_type = stub('Content Type') @file_hash[:type] = expected_content_type uploadable = Koala::UploadableIO.new(@file_hash) uploadable.content_type.should == expected_content_type end - it "should get the io_or_path from the :tempfile key" do - expected_file = stub('File') - @file_hash[:tempfile] = expected_file - + it "gets the content type from the :type key" do uploadable = Koala::UploadableIO.new(@file_hash) - uploadable.io_or_path.should == expected_file + uploadable.filename.should == @file_hash[:filename] end end describe "for files with with recognizable MIME types" do # what that means is tested below @@ -146,22 +159,22 @@ it "should call the constructor with the content type, file name, and a dummy file name" do UploadIO.should_receive(:new).with(BEACH_BALL_PATH, "content/type", anything).and_return(@upload_io) Koala::UploadableIO.new(BEACH_BALL_PATH, "content/type").to_upload_io.should == @upload_io end end - + context "if a filename was provided" do it "should call the constructor with the content type, file name, and the filename" do filename = "file" UploadIO.should_receive(:new).with(BEACH_BALL_PATH, "content/type", filename).and_return(@upload_io) Koala::UploadableIO.new(BEACH_BALL_PATH, "content/type", filename).to_upload_io end end end describe "getting a file" do - it "should return the File if initialized with a file" do + it "returns the File if initialized with a file" do f = File.new(BEACH_BALL_PATH) Koala::UploadableIO.new(f).to_file.should == f end it "should open up and return a file corresponding to the path if io_or_path is a path" do @@ -173,21 +186,21 @@ describe "#binary_content?" do it "returns true for Rails 3 file uploads" do Koala::UploadableIO.binary_content?(rails_3_mocks.last).should be_true end - + it "returns true for Sinatra file uploads" do Koala::UploadableIO.binary_content?(rails_3_mocks.last).should be_true end - + it "returns true for File objects" do Koala::UploadableIO.binary_content?(File.open(BEACH_BALL_PATH)).should be_true end - + it "returns false for everything else" do Koala::UploadableIO.binary_content?(StringIO.new).should be_false Koala::UploadableIO.binary_content?(BEACH_BALL_PATH).should be_false Koala::UploadableIO.binary_content?(nil).should be_false end end -end # describe UploadableIO \ No newline at end of file +end # describe UploadableIO