Add application name to client, update style of assigning client to match other recen...
[arvados.git] / spec / google / api_client / media_spec.rb
1 # Copyright 2012 Google Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 require 'spec_helper'
16
17 require 'google/api_client'
18 require 'google/api_client/version'
19
20 fixtures_path = File.expand_path('../../../fixtures', __FILE__)
21
22 describe Google::APIClient::UploadIO do
23   it 'should reject invalid file paths' do
24     (lambda do
25       media = Google::APIClient::UploadIO.new('doesnotexist', 'text/plain')
26     end).should raise_error
27   end
28
29   describe 'with a file' do
30     before do
31       @file = File.expand_path('files/sample.txt', fixtures_path)
32       @media = Google::APIClient::UploadIO.new(@file, 'text/plain')
33     end
34
35     it 'should report the correct file length' do
36       @media.length.should == File.size(@file)
37     end
38
39     it 'should have a mime type' do
40       @media.content_type.should == 'text/plain'
41     end
42   end
43
44   describe 'with StringIO' do
45     before do
46       @content = "hello world"
47       @media = Google::APIClient::UploadIO.new(StringIO.new(@content), 'text/plain', 'test.txt')
48     end
49
50     it 'should report the correct file length' do
51       @media.length.should == @content.length
52     end
53
54     it 'should have a mime type' do
55       @media.content_type.should == 'text/plain'
56     end
57   end
58 end
59
60 describe Google::APIClient::ResumableUpload do
61   CLIENT = Google::APIClient.new(:application_name => 'API Client Tests') unless defined?(CLIENT)
62
63   after do
64     # Reset client to not-quite-pristine state
65     CLIENT.key = nil
66     CLIENT.user_ip = nil
67   end
68
69   before do
70     @drive = CLIENT.discovered_api('drive', 'v1')
71     @file = File.expand_path('files/sample.txt', fixtures_path)
72     @media = Google::APIClient::UploadIO.new(@file, 'text/plain')
73     @uploader = Google::APIClient::ResumableUpload.new(
74       :media => @media,
75       :api_method => @drive.files.insert,
76       :uri => 'https://www.googleapis.com/upload/drive/v1/files/12345')
77   end
78
79   it 'should consider 20x status as complete' do
80     request = @uploader.to_http_request
81     @uploader.process_http_response(mock_result(200))
82     @uploader.complete?.should == true
83   end
84
85   it 'should consider 30x status as incomplete' do
86     request = @uploader.to_http_request
87     @uploader.process_http_response(mock_result(308))
88     @uploader.complete?.should == false
89     @uploader.expired?.should == false
90   end
91
92   it 'should consider 40x status as fatal' do
93     request = @uploader.to_http_request
94     @uploader.process_http_response(mock_result(404))
95     @uploader.expired?.should == true
96   end
97
98   it 'should detect changes to location' do
99     request = @uploader.to_http_request
100     @uploader.process_http_response(mock_result(308, 'location' => 'https://www.googleapis.com/upload/drive/v1/files/abcdef'))
101     @uploader.uri.to_s.should == 'https://www.googleapis.com/upload/drive/v1/files/abcdef'
102   end
103
104   it 'should resume from the saved range reported by the server' do    
105     @uploader.chunk_size = 200
106     @uploader.to_http_request # Send bytes 0-199, only 0-99 saved
107     @uploader.process_http_response(mock_result(308, 'range' => '0-99'))
108     method, url, headers, body = @uploader.to_http_request # Send bytes 100-299
109     headers['Content-Range'].should == "bytes 100-299/#{@media.length}"
110     headers['Content-length'].should == "200"
111   end
112
113   it 'should resync the offset after 5xx errors' do
114     @uploader.chunk_size = 200
115     @uploader.to_http_request
116     @uploader.process_http_response(mock_result(500)) # Invalidates range
117     method, url, headers, body = @uploader.to_http_request # Resync
118     headers['Content-Range'].should == "bytes */#{@media.length}"
119     headers['Content-length'].should == "0"
120     @uploader.process_http_response(mock_result(308, 'range' => '0-99'))
121     method, url, headers, body = @uploader.to_http_request # Send next chunk at correct range
122     headers['Content-Range'].should == "bytes 100-299/#{@media.length}"
123     headers['Content-length'].should == "200"
124   end
125
126   def mock_result(status, headers = {})
127     reference = Google::APIClient::Reference.new(:api_method => @drive.files.insert)
128     stub('result', :status => status, :headers => headers, :reference => reference)
129   end
130
131 end