2012-06-04 23:51:11 +00:00
|
|
|
# Copyright 2012 Google Inc.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
require 'google/api_client'
|
|
|
|
|
|
|
|
fixtures_path = File.expand_path('../../../fixtures', __FILE__)
|
|
|
|
|
|
|
|
describe Google::APIClient::UploadIO do
|
|
|
|
it 'should reject invalid file paths' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(lambda do
|
2012-06-04 23:51:11 +00:00
|
|
|
media = Google::APIClient::UploadIO.new('doesnotexist', 'text/plain')
|
2014-08-14 15:54:16 +00:00
|
|
|
end).to raise_error
|
2012-06-04 23:51:11 +00:00
|
|
|
end
|
2012-07-31 20:15:45 +00:00
|
|
|
|
2012-06-04 23:51:11 +00:00
|
|
|
describe 'with a file' do
|
|
|
|
before do
|
|
|
|
@file = File.expand_path('files/sample.txt', fixtures_path)
|
|
|
|
@media = Google::APIClient::UploadIO.new(@file, 'text/plain')
|
|
|
|
end
|
2012-07-31 20:15:45 +00:00
|
|
|
|
2012-06-04 23:51:11 +00:00
|
|
|
it 'should report the correct file length' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(@media.length).to eq(File.size(@file))
|
2012-06-04 23:51:11 +00:00
|
|
|
end
|
2012-07-31 20:15:45 +00:00
|
|
|
|
2012-06-04 23:51:11 +00:00
|
|
|
it 'should have a mime type' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(@media.content_type).to eq('text/plain')
|
2012-06-04 23:51:11 +00:00
|
|
|
end
|
|
|
|
end
|
2012-07-31 20:15:45 +00:00
|
|
|
|
2012-06-04 23:51:11 +00:00
|
|
|
describe 'with StringIO' do
|
|
|
|
before do
|
|
|
|
@content = "hello world"
|
2012-06-07 01:03:23 +00:00
|
|
|
@media = Google::APIClient::UploadIO.new(StringIO.new(@content), 'text/plain', 'test.txt')
|
2012-06-04 23:51:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should report the correct file length' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(@media.length).to eq(@content.length)
|
2012-06-04 23:51:11 +00:00
|
|
|
end
|
2012-07-31 20:15:45 +00:00
|
|
|
|
2012-06-04 23:51:11 +00:00
|
|
|
it 'should have a mime type' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(@media.content_type).to eq('text/plain')
|
2012-06-04 23:51:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-18 19:15:02 +00:00
|
|
|
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
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(@io.read).to eq("23456")
|
2013-06-18 19:15:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should maintain position' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(@io.read(1)).to eq('2')
|
|
|
|
expect(@io.read(2)).to eq('34')
|
|
|
|
expect(@io.read(2)).to eq('56')
|
2013-06-18 19:15:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should allow rewinds' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(@io.read(2)).to eq('23')
|
2013-06-18 19:15:02 +00:00
|
|
|
@io.rewind()
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(@io.read(2)).to eq('23')
|
2013-06-18 19:15:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should allow setting position' do
|
|
|
|
@io.pos = 3
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(@io.read).to eq('56')
|
2013-06-18 19:15:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not allow position to be set beyond range' do
|
|
|
|
@io.pos = 10
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(@io.read).to eq('')
|
2013-06-18 19:15:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return empty string when read amount is zero' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(@io.read(0)).to eq('')
|
2013-06-18 19:15:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return empty string at EOF if amount is nil' do
|
|
|
|
@io.read
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(@io.read).to eq('')
|
2013-06-18 19:15:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return nil at EOF if amount is positive int' do
|
|
|
|
@io.read
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(@io.read(1)).to eq(nil)
|
2013-06-18 19:15:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2012-06-04 23:51:11 +00:00
|
|
|
describe Google::APIClient::ResumableUpload do
|
2012-12-30 19:27:45 +00:00
|
|
|
CLIENT = Google::APIClient.new(:application_name => 'API Client Tests') unless defined?(CLIENT)
|
2012-07-31 20:56:13 +00:00
|
|
|
|
|
|
|
after do
|
|
|
|
# Reset client to not-quite-pristine state
|
|
|
|
CLIENT.key = nil
|
|
|
|
CLIENT.user_ip = nil
|
|
|
|
end
|
2012-07-31 20:15:45 +00:00
|
|
|
|
2012-06-04 23:51:11 +00:00
|
|
|
before do
|
2012-07-31 20:56:13 +00:00
|
|
|
@drive = CLIENT.discovered_api('drive', 'v1')
|
2012-06-04 23:51:11 +00:00
|
|
|
@file = File.expand_path('files/sample.txt', fixtures_path)
|
|
|
|
@media = Google::APIClient::UploadIO.new(@file, 'text/plain')
|
|
|
|
@uploader = Google::APIClient::ResumableUpload.new(
|
2012-09-11 18:04:21 +00:00
|
|
|
:media => @media,
|
|
|
|
:api_method => @drive.files.insert,
|
|
|
|
:uri => 'https://www.googleapis.com/upload/drive/v1/files/12345')
|
2012-06-04 23:51:11 +00:00
|
|
|
end
|
2012-07-31 20:15:45 +00:00
|
|
|
|
2012-06-04 23:51:11 +00:00
|
|
|
it 'should consider 20x status as complete' do
|
2012-09-11 18:04:21 +00:00
|
|
|
request = @uploader.to_http_request
|
2012-09-24 23:09:17 +00:00
|
|
|
@uploader.process_http_response(mock_result(200))
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(@uploader.complete?).to eq(true)
|
2012-06-04 23:51:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should consider 30x status as incomplete' do
|
2012-09-11 18:04:21 +00:00
|
|
|
request = @uploader.to_http_request
|
2012-09-24 23:09:17 +00:00
|
|
|
@uploader.process_http_response(mock_result(308))
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(@uploader.complete?).to eq(false)
|
|
|
|
expect(@uploader.expired?).to eq(false)
|
2012-06-04 23:51:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should consider 40x status as fatal' do
|
2012-09-11 18:04:21 +00:00
|
|
|
request = @uploader.to_http_request
|
2012-09-24 23:09:17 +00:00
|
|
|
@uploader.process_http_response(mock_result(404))
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(@uploader.expired?).to eq(true)
|
2012-06-04 23:51:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should detect changes to location' do
|
2012-09-11 18:04:21 +00:00
|
|
|
request = @uploader.to_http_request
|
2012-09-24 23:09:17 +00:00
|
|
|
@uploader.process_http_response(mock_result(308, 'location' => 'https://www.googleapis.com/upload/drive/v1/files/abcdef'))
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(@uploader.uri.to_s).to eq('https://www.googleapis.com/upload/drive/v1/files/abcdef')
|
2012-06-04 23:51:11 +00:00
|
|
|
end
|
2012-07-31 20:15:45 +00:00
|
|
|
|
2012-09-11 18:04:21 +00:00
|
|
|
it 'should resume from the saved range reported by the server' do
|
2012-06-04 23:51:11 +00:00
|
|
|
@uploader.chunk_size = 200
|
2012-09-24 23:09:17 +00:00
|
|
|
@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
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(headers['Content-Range']).to eq("bytes 100-299/#{@media.length}")
|
|
|
|
expect(headers['Content-length']).to eq("200")
|
2012-06-04 23:51:11 +00:00
|
|
|
end
|
2012-07-31 20:15:45 +00:00
|
|
|
|
2012-06-04 23:51:11 +00:00
|
|
|
it 'should resync the offset after 5xx errors' do
|
|
|
|
@uploader.chunk_size = 200
|
2012-09-24 23:09:17 +00:00
|
|
|
@uploader.to_http_request
|
|
|
|
@uploader.process_http_response(mock_result(500)) # Invalidates range
|
|
|
|
method, url, headers, body = @uploader.to_http_request # Resync
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(headers['Content-Range']).to eq("bytes */#{@media.length}")
|
|
|
|
expect(headers['Content-length']).to eq("0")
|
2012-09-24 23:09:17 +00:00
|
|
|
@uploader.process_http_response(mock_result(308, 'range' => '0-99'))
|
|
|
|
method, url, headers, body = @uploader.to_http_request # Send next chunk at correct range
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(headers['Content-Range']).to eq("bytes 100-299/#{@media.length}")
|
|
|
|
expect(headers['Content-length']).to eq("200")
|
2012-06-04 23:51:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def mock_result(status, headers = {})
|
|
|
|
reference = Google::APIClient::Reference.new(:api_method => @drive.files.insert)
|
2013-08-12 15:19:09 +00:00
|
|
|
double('result', :status => status, :headers => headers, :reference => reference)
|
2012-06-04 23:51:11 +00:00
|
|
|
end
|
2012-07-31 20:15:45 +00:00
|
|
|
|
2012-06-04 23:51:11 +00:00
|
|
|
end
|