RSpec 3 syntax
[arvados.git] / spec / google / api_client / media_spec.rb
index fd3be586975c96b4742d12ae6172618fccccaeaa..f32b31efc0e553aa8a5bded9fa31137483b92c0c 100644 (file)
 require 'spec_helper'
 
 require 'google/api_client'
-require 'google/api_client/version'
 
 fixtures_path = File.expand_path('../../../fixtures', __FILE__)
 
-describe Google::APIClient::UploadIO do
+RSpec.describe Google::APIClient::UploadIO do
   it 'should reject invalid file paths' do
-    (lambda do
+    expect(lambda do
       media = Google::APIClient::UploadIO.new('doesnotexist', 'text/plain')
-    end).should raise_error
+    end).to raise_error
   end
 
   describe 'with a file' do
@@ -33,11 +32,11 @@ describe Google::APIClient::UploadIO do
     end
 
     it 'should report the correct file length' do
-      @media.length.should == File.size(@file)
+      expect(@media.length).to eq(File.size(@file))
     end
 
     it 'should have a mime type' do
-      @media.content_type.should == 'text/plain'
+      expect(@media.content_type).to eq('text/plain')
     end
   end
 
@@ -48,17 +47,65 @@ describe Google::APIClient::UploadIO do
     end
 
     it 'should report the correct file length' do
-      @media.length.should == @content.length
+      expect(@media.length).to eq(@content.length)
     end
 
     it 'should have a mime type' do
-      @media.content_type.should == 'text/plain'
+      expect(@media.content_type).to eq('text/plain')
     end
   end
 end
 
-describe Google::APIClient::ResumableUpload do
-  CLIENT = Google::APIClient.new
+RSpec.describe Google::APIClient::RangedIO do
+  before do
+    @source = StringIO.new("1234567890abcdef")
+    @io = Google::APIClient::RangedIO.new(@source, 1, 5)
+  end
+  
+  it 'should return the correct range when read entirely' do
+    expect(@io.read).to eq("23456")
+  end
+  
+  it 'should maintain position' do
+    expect(@io.read(1)).to eq('2')
+    expect(@io.read(2)).to eq('34')
+    expect(@io.read(2)).to eq('56')
+  end
+  
+  it 'should allow rewinds' do
+    expect(@io.read(2)).to eq('23')
+    @io.rewind()
+    expect(@io.read(2)).to eq('23')
+  end
+  
+  it 'should allow setting position' do
+    @io.pos = 3
+    expect(@io.read).to eq('56')
+  end
+  
+  it 'should not allow position to be set beyond range' do
+    @io.pos = 10
+    expect(@io.read).to eq('')
+  end
+  
+  it 'should return empty string when read amount is zero' do
+    expect(@io.read(0)).to eq('')
+  end
+  
+  it 'should return empty string at EOF if amount is nil' do
+    @io.read
+    expect(@io.read).to eq('')
+  end
+  
+  it 'should return nil at EOF if amount is positive int' do
+    @io.read
+    expect(@io.read(1)).to eq(nil)
+  end
+    
+end
+
+RSpec.describe Google::APIClient::ResumableUpload do
+  CLIENT = Google::APIClient.new(:application_name => 'API Client Tests') unless defined?(CLIENT)
 
   after do
     # Reset client to not-quite-pristine state
@@ -71,73 +118,61 @@ describe Google::APIClient::ResumableUpload do
     @file = File.expand_path('files/sample.txt', fixtures_path)
     @media = Google::APIClient::UploadIO.new(@file, 'text/plain')
     @uploader = Google::APIClient::ResumableUpload.new(
-      mock_result(308),
-      @media,
-      'https://www.googleapis.com/upload/drive/v1/files/12345')
+      :media => @media,
+      :api_method => @drive.files.insert,
+      :uri => 'https://www.googleapis.com/upload/drive/v1/files/12345')
   end
 
   it 'should consider 20x status as complete' do
-    api_client = stub('api', :execute => mock_result(200))
-    @uploader.send_chunk(api_client)
-    @uploader.complete?.should == true
+    request = @uploader.to_http_request
+    @uploader.process_http_response(mock_result(200))
+    expect(@uploader.complete?).to eq(true)
   end
 
   it 'should consider 30x status as incomplete' do
-    api_client = stub('api', :execute => mock_result(308))
-    @uploader.send_chunk(api_client)
-    @uploader.complete?.should == false
-    @uploader.expired?.should == false
+    request = @uploader.to_http_request
+    @uploader.process_http_response(mock_result(308))
+    expect(@uploader.complete?).to eq(false)
+    expect(@uploader.expired?).to eq(false)
   end
 
   it 'should consider 40x status as fatal' do
-    api_client = stub('api', :execute => mock_result(404))
-    @uploader.send_chunk(api_client)
-    @uploader.expired?.should == true
+    request = @uploader.to_http_request
+    @uploader.process_http_response(mock_result(404))
+    expect(@uploader.expired?).to eq(true)
   end
 
   it 'should detect changes to location' do
-    api_client = stub('api', :execute => mock_result(308, 'location' => 'https://www.googleapis.com/upload/drive/v1/files/abcdef'))
-    @uploader.send_chunk(api_client)
-    @uploader.location.should == 'https://www.googleapis.com/upload/drive/v1/files/abcdef'
+    request = @uploader.to_http_request
+    @uploader.process_http_response(mock_result(308, 'location' => 'https://www.googleapis.com/upload/drive/v1/files/abcdef'))
+    expect(@uploader.uri.to_s).to eq('https://www.googleapis.com/upload/drive/v1/files/abcdef')
   end
 
-  it 'should resume from the saved range reported by the server' do
-    api_client = mock('api')
-    api_client.should_receive(:execute).and_return(mock_result(308, 'range' => '0-99'))
-    api_client.should_receive(:execute).with(
-      hash_including(:headers => hash_including(
-        "Content-Range" => "bytes 100-299/#{@media.length}",
-        "Content-Length" => "200"
-      ))).and_return(mock_result(308))
-
+  it 'should resume from the saved range reported by the server' do    
     @uploader.chunk_size = 200
-    @uploader.send_chunk(api_client) # Send bytes 0-199, only 0-99 saved
-    @uploader.send_chunk(api_client) # Send bytes 100-299
+    @uploader.to_http_request # Send bytes 0-199, only 0-99 saved
+    @uploader.process_http_response(mock_result(308, 'range' => '0-99'))
+    method, url, headers, body = @uploader.to_http_request # Send bytes 100-299
+    expect(headers['Content-Range']).to eq("bytes 100-299/#{@media.length}")
+    expect(headers['Content-length']).to eq("200")
   end
 
   it 'should resync the offset after 5xx errors' do
-    api_client = mock('api')
-    api_client.should_receive(:execute).and_return(mock_result(500))
-    api_client.should_receive(:execute).with(
-      hash_including(:headers => hash_including(
-        "Content-Range" => "bytes */#{@media.length}",
-        "Content-Length" => "0"
-      ))).and_return(mock_result(308, 'range' => '0-99'))
-    api_client.should_receive(:execute).with(
-      hash_including(:headers => hash_including(
-        "Content-Range" => "bytes 100-299/#{@media.length}",
-        "Content-Length" => "200"
-      ))).and_return(mock_result(308))
-
     @uploader.chunk_size = 200
-    @uploader.send_chunk(api_client) # 500, invalidate
-    @uploader.send_chunk(api_client) # Just resyncs, doesn't actually upload
-    @uploader.send_chunk(api_client) # Send next chunk at correct range
+    @uploader.to_http_request
+    @uploader.process_http_response(mock_result(500)) # Invalidates range
+    method, url, headers, body = @uploader.to_http_request # Resync
+    expect(headers['Content-Range']).to eq("bytes */#{@media.length}")
+    expect(headers['Content-length']).to eq("0")
+    @uploader.process_http_response(mock_result(308, 'range' => '0-99'))
+    method, url, headers, body = @uploader.to_http_request # Send next chunk at correct range
+    expect(headers['Content-Range']).to eq("bytes 100-299/#{@media.length}")
+    expect(headers['Content-length']).to eq("200")
   end
 
   def mock_result(status, headers = {})
     reference = Google::APIClient::Reference.new(:api_method => @drive.files.insert)
-    stub('result', :status => status, :headers => headers, :reference => reference)
+    double('result', :status => status, :headers => headers, :reference => reference)
   end
 
 end