google-api-client 0.9.5 → 0.9.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/google-api-client.gemspec +4 -3
- data/lib/google/api_client/client_secrets.rb +1 -1
- data/lib/google/apis/core/download.rb +10 -1
- data/lib/google/apis/core/json_representation.rb +2 -2
- data/lib/google/apis/version.rb +1 -1
- data/samples/cli/.env +4 -0
- data/samples/web/.env +2 -0
- data/samples/web/views/home.erb +0 -1
- metadata +5 -71
- data/api_names_out.yaml +0 -28434
- data/generated/google/apis/sheets_v1.rb +0 -43
- data/generated/google/apis/sheets_v1/classes.rb +0 -4542
- data/generated/google/apis/sheets_v1/representations.rb +0 -1703
- data/generated/google/apis/sheets_v1/service.rb +0 -363
- data/script/generate +0 -105
- data/script/package +0 -8
- data/script/release +0 -15
- data/spec/fixtures/files/api_names.yaml +0 -3
- data/spec/fixtures/files/auth_stored_credentials.json +0 -8
- data/spec/fixtures/files/child/.gitignore +0 -0
- data/spec/fixtures/files/client_secrets.json +0 -1
- data/spec/fixtures/files/invalid.json +0 -1
- data/spec/fixtures/files/test.blah +0 -1
- data/spec/fixtures/files/test.txt +0 -1
- data/spec/fixtures/files/test_api.json +0 -440
- data/spec/google/api_client/auth/storage_spec.rb +0 -120
- data/spec/google/api_client/auth/storages/file_store_spec.rb +0 -39
- data/spec/google/api_client/auth/storages/redis_store_spec.rb +0 -68
- data/spec/google/api_client/client_secrets_spec.rb +0 -389
- data/spec/google/apis/core/api_command_spec.rb +0 -209
- data/spec/google/apis/core/batch_spec.rb +0 -142
- data/spec/google/apis/core/download_spec.rb +0 -103
- data/spec/google/apis/core/hashable_spec.rb +0 -60
- data/spec/google/apis/core/http_command_spec.rb +0 -303
- data/spec/google/apis/core/json_representation_spec.rb +0 -199
- data/spec/google/apis/core/service_spec.rb +0 -313
- data/spec/google/apis/core/upload_spec.rb +0 -300
- data/spec/google/apis/generated_spec.rb +0 -27
- data/spec/google/apis/generator/generator_spec.rb +0 -324
- data/spec/google/apis/logging_spec.rb +0 -100
- data/spec/google/apis/options_spec.rb +0 -40
- data/spec/integration_tests/adsense_spec.rb +0 -29
- data/spec/integration_tests/drive_spec.rb +0 -35
- data/spec/integration_tests/pubsub_spec.rb +0 -48
- data/spec/integration_tests/url_shortener_spec.rb +0 -45
- data/spec/spec_helper.rb +0 -153
- data/spec/spec_helper/load_path_spec.rb +0 -33
@@ -1,313 +0,0 @@
|
|
1
|
-
# Copyright 2015 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
|
-
require 'google/apis/options'
|
17
|
-
require 'google/apis/core/base_service'
|
18
|
-
require 'google/apis/core/json_representation'
|
19
|
-
require 'hurley/test'
|
20
|
-
require 'ostruct'
|
21
|
-
|
22
|
-
RSpec.describe Google::Apis::Core::BaseService do
|
23
|
-
include TestHelpers
|
24
|
-
|
25
|
-
let(:service) { Google::Apis::Core::BaseService.new('https://www.googleapis.com/', '') }
|
26
|
-
|
27
|
-
before do
|
28
|
-
Google::Apis::ClientOptions.default.application_name = 'test'
|
29
|
-
Google::Apis::ClientOptions.default.application_version = '1.0'
|
30
|
-
end
|
31
|
-
|
32
|
-
it 'should inherit default options' do
|
33
|
-
expect(service.client_options.application_name).to eql 'test'
|
34
|
-
end
|
35
|
-
|
36
|
-
it 'should include application in user agent' do
|
37
|
-
agent = service.send(:user_agent)
|
38
|
-
expect(agent).to match /^test\/1.0/
|
39
|
-
end
|
40
|
-
|
41
|
-
it 'should inherit authorization' do
|
42
|
-
Google::Apis::RequestOptions.default.authorization = 'a token'
|
43
|
-
expect(service.authorization).to eql 'a token'
|
44
|
-
end
|
45
|
-
|
46
|
-
it 'should allow overiding authorization' do
|
47
|
-
Google::Apis::RequestOptions.default.authorization = 'a token'
|
48
|
-
service.authorization = 'another token'
|
49
|
-
expect(service.authorization).to eql 'another token'
|
50
|
-
end
|
51
|
-
|
52
|
-
shared_examples 'with options' do
|
53
|
-
it 'should inherit service options' do
|
54
|
-
service.request_options.retries = 2
|
55
|
-
expect(command.options.retries).to eql 2
|
56
|
-
end
|
57
|
-
|
58
|
-
it 'should include per-request options' do
|
59
|
-
expect(command.options.authorization).to eql 'foo'
|
60
|
-
end
|
61
|
-
|
62
|
-
it 'should allow per-request options to override service options' do
|
63
|
-
service.request_options.authorization = 'bar'
|
64
|
-
expect(command.options.authorization).to eql 'foo'
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
context 'when making raw http requests' do
|
69
|
-
context 'with :get methods' do
|
70
|
-
before(:example) do
|
71
|
-
stub_request(:get, 'https://www.googleapis.com/zoo/animals').to_return(body: 'hello world')
|
72
|
-
end
|
73
|
-
|
74
|
-
it 'should return body for get' do
|
75
|
-
response = service.http(:get, 'https://www.googleapis.com/zoo/animals')
|
76
|
-
expect(response).to eq 'hello world'
|
77
|
-
end
|
78
|
-
|
79
|
-
it 'should return allow downloads' do
|
80
|
-
response = service.http(:get, 'https://www.googleapis.com/zoo/animals', download_dest: StringIO.new)
|
81
|
-
expect(response.string).to eq 'hello world'
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
context 'with :post methods' do
|
86
|
-
before(:example) do
|
87
|
-
stub_request(:post, 'https://www.googleapis.com/zoo/animals').to_return(body: '')
|
88
|
-
end
|
89
|
-
|
90
|
-
it 'should post body' do
|
91
|
-
service.http(:post, 'https://www.googleapis.com/zoo/animals', body: 'hello')
|
92
|
-
expect(a_request(:post, 'https://www.googleapis.com/zoo/animals')
|
93
|
-
.with(body: 'hello')).to have_been_made
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
context 'when making simple commands' do
|
99
|
-
let(:command) { service.send(:make_simple_command, :get, 'zoo/animals', authorization: 'foo') }
|
100
|
-
|
101
|
-
it 'should return the correct command type' do
|
102
|
-
expect(command).to be_an_instance_of(Google::Apis::Core::ApiCommand)
|
103
|
-
end
|
104
|
-
|
105
|
-
it 'should build a correct URL' do
|
106
|
-
url = command.url.expand({}).to_s
|
107
|
-
expect(url).to eql 'https://www.googleapis.com/zoo/animals'
|
108
|
-
end
|
109
|
-
|
110
|
-
include_examples 'with options'
|
111
|
-
end
|
112
|
-
|
113
|
-
context 'when making download commands' do
|
114
|
-
let(:command) { service.send(:make_download_command, :get, 'zoo/animals', authorization: 'foo') }
|
115
|
-
|
116
|
-
it 'should return the correct command type' do
|
117
|
-
expect(command).to be_an_instance_of(Google::Apis::Core::DownloadCommand)
|
118
|
-
end
|
119
|
-
|
120
|
-
it 'should build a correct URL' do
|
121
|
-
url = command.url.expand({}).to_s
|
122
|
-
expect(url).to eql 'https://www.googleapis.com/zoo/animals'
|
123
|
-
end
|
124
|
-
|
125
|
-
it 'should include alt=media in params' do
|
126
|
-
expect(command.query).to include('alt' => 'media')
|
127
|
-
end
|
128
|
-
|
129
|
-
include_examples 'with options'
|
130
|
-
end
|
131
|
-
|
132
|
-
context 'when making upload commands' do
|
133
|
-
let(:command) { service.send(:make_upload_command, :post, 'zoo/animals', authorization: 'foo') }
|
134
|
-
|
135
|
-
it 'should return the correct command type' do
|
136
|
-
expect(command).to be_an_instance_of(Google::Apis::Core::ResumableUploadCommand)
|
137
|
-
end
|
138
|
-
|
139
|
-
it 'should build a correct URL' do
|
140
|
-
url = command.url.expand({}).to_s
|
141
|
-
expect(url).to eql 'https://www.googleapis.com/upload/zoo/animals'
|
142
|
-
end
|
143
|
-
|
144
|
-
include_examples 'with options'
|
145
|
-
end
|
146
|
-
|
147
|
-
context 'with batch' do
|
148
|
-
before(:example) do
|
149
|
-
response = <<EOF.gsub(/\n/, "\r\n")
|
150
|
-
--batch123
|
151
|
-
Content-Type: application/http
|
152
|
-
|
153
|
-
HTTP/1.1 200 OK
|
154
|
-
Content-Type: text/plain; charset=UTF-8
|
155
|
-
|
156
|
-
Hello
|
157
|
-
--batch123--
|
158
|
-
EOF
|
159
|
-
stub_request(:post, 'https://www.googleapis.com/batch')
|
160
|
-
.to_return(headers: { 'Content-Type' => 'multipart/mixed; boundary=batch123' }, body: response)
|
161
|
-
end
|
162
|
-
|
163
|
-
it 'should add commands to a batch' do
|
164
|
-
expect do |b|
|
165
|
-
service.batch do |service|
|
166
|
-
command = service.send(:make_simple_command, :get, 'zoo/animals', {})
|
167
|
-
service.send(:execute_or_queue_command, command, &b)
|
168
|
-
end
|
169
|
-
end.to yield_with_args('Hello', nil)
|
170
|
-
end
|
171
|
-
|
172
|
-
it 'should disallow uploads in batch' do
|
173
|
-
expect do |b|
|
174
|
-
service.batch do |service|
|
175
|
-
command = service.send(:make_upload_command, :post, 'zoo/animals', {})
|
176
|
-
service.send(:execute_or_queue_command, command, &b)
|
177
|
-
end
|
178
|
-
end.to raise_error(Google::Apis::ClientError)
|
179
|
-
end
|
180
|
-
|
181
|
-
it 'should disallow downloads in batch' do
|
182
|
-
expect do |b|
|
183
|
-
service.batch do |service|
|
184
|
-
command = service.send(:make_download_command, :get, 'zoo/animals', {})
|
185
|
-
service.send(:execute_or_queue_command, command, &b)
|
186
|
-
end
|
187
|
-
end.to raise_error(Google::Apis::ClientError)
|
188
|
-
end
|
189
|
-
end
|
190
|
-
|
191
|
-
context 'with batch uploads' do
|
192
|
-
before(:example) do
|
193
|
-
response = <<EOF.gsub(/\n/, "\r\n")
|
194
|
-
--batch123
|
195
|
-
Content-Type: application/http
|
196
|
-
|
197
|
-
HTTP/1.1 200 OK
|
198
|
-
Content-Type: text/plain; charset=UTF-8
|
199
|
-
|
200
|
-
Hello
|
201
|
-
--batch123--
|
202
|
-
EOF
|
203
|
-
stub_request(:put, 'https://www.googleapis.com/upload/')
|
204
|
-
.to_return(headers: { 'Content-Type' => 'multipart/mixed; boundary=batch123' }, body: response)
|
205
|
-
end
|
206
|
-
|
207
|
-
it 'should add upload to a batch' do
|
208
|
-
expect do |b|
|
209
|
-
service.batch_upload do |service|
|
210
|
-
command = service.send(:make_upload_command, :post, 'zoo/animals', {})
|
211
|
-
command.upload_source = StringIO.new('test')
|
212
|
-
command.upload_content_type = 'text/plain'
|
213
|
-
service.send(:execute_or_queue_command, command, &b)
|
214
|
-
end
|
215
|
-
end.to yield_with_args('Hello', nil)
|
216
|
-
end
|
217
|
-
|
218
|
-
it 'should use multipart upload' do
|
219
|
-
expect do |b|
|
220
|
-
service.batch_upload do |service|
|
221
|
-
command = service.send(:make_upload_command, :post, 'zoo/animals', {})
|
222
|
-
command.upload_source = StringIO.new('test')
|
223
|
-
command.upload_content_type = 'text/plain'
|
224
|
-
expect(command).to be_an_instance_of(Google::Apis::Core::MultipartUploadCommand)
|
225
|
-
service.send(:execute_or_queue_command, command, &b)
|
226
|
-
end
|
227
|
-
end.to yield_with_args('Hello', nil)
|
228
|
-
end
|
229
|
-
|
230
|
-
it 'should disallow downloads in batch' do
|
231
|
-
expect do |b|
|
232
|
-
service.batch_upload do |service|
|
233
|
-
command = service.send(:make_download_command, :get, 'zoo/animals', {})
|
234
|
-
service.send(:execute_or_queue_command, command, &b)
|
235
|
-
end
|
236
|
-
end.to raise_error(Google::Apis::ClientError)
|
237
|
-
end
|
238
|
-
|
239
|
-
it 'should disallow simple commands in batch' do
|
240
|
-
expect do |b|
|
241
|
-
service.batch_upload do |service|
|
242
|
-
command = service.send(:make_simple_command, :get, 'zoo/animals', {})
|
243
|
-
service.send(:execute_or_queue_command, command, &b)
|
244
|
-
end
|
245
|
-
end.to raise_error(Google::Apis::ClientError)
|
246
|
-
end
|
247
|
-
|
248
|
-
context 'with fetch_all' do
|
249
|
-
let(:responses) do
|
250
|
-
data = {}
|
251
|
-
data[nil] = OpenStruct.new(next_page_token: 'p1', items: ['a', 'b', 'c'], alt_items: [1, 2 , 3])
|
252
|
-
data['p1'] = OpenStruct.new(next_page_token: 'p2', items: ['d', 'e', 'f'], alt_items: [4, 5, 6])
|
253
|
-
data['p2'] = OpenStruct.new(next_page_token: nil, items: ['g', 'h', 'i'], alt_items: [7,8, 9])
|
254
|
-
data
|
255
|
-
end
|
256
|
-
|
257
|
-
let(:items) { service.fetch_all { |token| responses[token] } }
|
258
|
-
|
259
|
-
it 'should fetch pages until next page token is nil' do
|
260
|
-
expect(items).to contain_exactly('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i')
|
261
|
-
end
|
262
|
-
|
263
|
-
it 'should stop on repeated page token' do
|
264
|
-
responses['p2'].next_page_token = 'p2'
|
265
|
-
expect(items).to contain_exactly('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i')
|
266
|
-
end
|
267
|
-
|
268
|
-
it 'should allow selecting another field for items' do
|
269
|
-
expect(service.fetch_all(items: :alt_items) { |token| responses[token] } ).to contain_exactly(1, 2, 3, 4, 5, 6, 7, 8, 9)
|
270
|
-
end
|
271
|
-
|
272
|
-
it 'should allow limiting the number of items to fetch' do
|
273
|
-
expect(service.fetch_all(max: 5) { |token| responses[token] } ).to contain_exactly('a', 'b', 'c', 'd', 'e')
|
274
|
-
end
|
275
|
-
|
276
|
-
it 'should yield the next token' do
|
277
|
-
expect do |b|
|
278
|
-
service.fetch_all do |token|
|
279
|
-
b.to_proc.call(token)
|
280
|
-
responses[token]
|
281
|
-
end.count
|
282
|
-
end.to yield_successive_args(nil, 'p1', 'p2')
|
283
|
-
end
|
284
|
-
|
285
|
-
it 'should cache results' do
|
286
|
-
count = 0
|
287
|
-
items = service.fetch_all do |token|
|
288
|
-
count = count + 1
|
289
|
-
responses[token]
|
290
|
-
end
|
291
|
-
|
292
|
-
items.each{ |i| puts i }
|
293
|
-
items.each{ |i| puts i }
|
294
|
-
|
295
|
-
expect(count).to eq 3
|
296
|
-
end
|
297
|
-
|
298
|
-
it 'should allow disabling caching' do
|
299
|
-
count = 0
|
300
|
-
items = service.fetch_all(cache: false) do |token|
|
301
|
-
count = count + 1
|
302
|
-
responses[token]
|
303
|
-
end
|
304
|
-
|
305
|
-
items.each{ |i| puts i }
|
306
|
-
items.each{ |i| puts i }
|
307
|
-
|
308
|
-
expect(count).to eq 6
|
309
|
-
end
|
310
|
-
|
311
|
-
end
|
312
|
-
end
|
313
|
-
end
|
@@ -1,300 +0,0 @@
|
|
1
|
-
# Copyright 2015 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
|
-
require 'google/apis/core/upload'
|
17
|
-
require 'google/apis/core/json_representation'
|
18
|
-
require 'hurley/test'
|
19
|
-
|
20
|
-
# TODO: JSON Response decoding
|
21
|
-
# TODO: Upload from IO
|
22
|
-
# TODO: Upload from file
|
23
|
-
|
24
|
-
RSpec.describe Google::Apis::Core::UploadIO do
|
25
|
-
context 'from_file' do
|
26
|
-
let(:upload_io) { Google::Apis::Core::UploadIO.from_file(file) }
|
27
|
-
|
28
|
-
context 'with text file' do
|
29
|
-
let(:file) { File.join(FIXTURES_DIR, 'files', 'test.txt') }
|
30
|
-
it 'should infer content type from file' do
|
31
|
-
expect(upload_io.content_type).to eql('text/plain')
|
32
|
-
end
|
33
|
-
|
34
|
-
it 'should allow overriding the mime type' do
|
35
|
-
io = Google::Apis::Core::UploadIO.from_file(file, content_type: 'application/json')
|
36
|
-
expect(io.content_type).to eql('application/json')
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
context 'with unknown type' do
|
41
|
-
let(:file) { File.join(FIXTURES_DIR, 'files', 'test.blah') }
|
42
|
-
it 'should use the default mime type' do
|
43
|
-
expect(upload_io.content_type).to eql('application/octet-stream')
|
44
|
-
end
|
45
|
-
|
46
|
-
it 'should allow overriding the mime type' do
|
47
|
-
io = Google::Apis::Core::UploadIO.from_file(file, content_type: 'application/json')
|
48
|
-
expect(io.content_type).to eql('application/json')
|
49
|
-
end
|
50
|
-
|
51
|
-
it 'should setup length of the stream' do
|
52
|
-
upload_io = Google::Apis::Core::UploadIO.from_file(file)
|
53
|
-
expect(upload_io.length).to eq File.size(file)
|
54
|
-
end
|
55
|
-
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
context 'from_io' do
|
60
|
-
|
61
|
-
context 'with i/o stream' do
|
62
|
-
let(:io) { StringIO.new 'Hello google' }
|
63
|
-
|
64
|
-
it 'should setup default content-type' do
|
65
|
-
upload_io = Google::Apis::Core::UploadIO.from_io(io)
|
66
|
-
expect(upload_io.content_type).to eql Google::Apis::Core::UploadIO::OCTET_STREAM_CONTENT_TYPE
|
67
|
-
end
|
68
|
-
|
69
|
-
it 'should allow overring the mime type' do
|
70
|
-
upload_io = Google::Apis::Core::UploadIO.from_io(io, content_type: 'application/x-gzip')
|
71
|
-
expect(upload_io.content_type).to eq('application/x-gzip')
|
72
|
-
end
|
73
|
-
|
74
|
-
it 'should setup length of the stream' do
|
75
|
-
upload_io = Google::Apis::Core::UploadIO.from_io(io)
|
76
|
-
expect(upload_io.length).to eq 'Hello google'.length
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
RSpec.describe Google::Apis::Core::RawUploadCommand do
|
84
|
-
include TestHelpers
|
85
|
-
include_context 'HTTP client'
|
86
|
-
|
87
|
-
let(:command) do
|
88
|
-
command = Google::Apis::Core::RawUploadCommand.new(:post, 'https://www.googleapis.com/zoo/animals')
|
89
|
-
command.upload_source = file
|
90
|
-
command.upload_content_type = 'text/plain'
|
91
|
-
command
|
92
|
-
end
|
93
|
-
|
94
|
-
shared_examples 'should upload' do
|
95
|
-
before(:example) do
|
96
|
-
stub_request(:post, 'https://www.googleapis.com/zoo/animals').to_return(body: '')
|
97
|
-
end
|
98
|
-
|
99
|
-
it 'should send content' do
|
100
|
-
command.execute(client)
|
101
|
-
expect(a_request(:post, 'https://www.googleapis.com/zoo/animals')
|
102
|
-
.with(body: "Hello world\n")).to have_been_made
|
103
|
-
end
|
104
|
-
|
105
|
-
it 'should send upload protocol' do
|
106
|
-
command.execute(client)
|
107
|
-
expect(a_request(:post, 'https://www.googleapis.com/zoo/animals')
|
108
|
-
.with { |req| req.headers['X-Goog-Upload-Protocol'] == 'raw' }).to have_been_made
|
109
|
-
end
|
110
|
-
|
111
|
-
it 'should send content-type header' do
|
112
|
-
command.execute(client)
|
113
|
-
expect(a_request(:post, 'https://www.googleapis.com/zoo/animals')
|
114
|
-
.with { |req| req.headers['X-Goog-Upload-Header-Content-Type'] == 'text/plain' }).to have_been_made
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
context('with StringIO input') do
|
119
|
-
let(:file) { StringIO.new("Hello world\n") }
|
120
|
-
include_examples 'should upload'
|
121
|
-
end
|
122
|
-
|
123
|
-
context('with IO input') do
|
124
|
-
let(:file) { File.open(File.join(FIXTURES_DIR, 'files', 'test.txt'), 'r') }
|
125
|
-
include_examples 'should upload'
|
126
|
-
|
127
|
-
it 'should not close stream' do
|
128
|
-
expect(file.closed?).to be false
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
context('with Tempfile input') do
|
133
|
-
let(:file) do
|
134
|
-
temp_file = Tempfile.new("tempfile")
|
135
|
-
temp_file.write("Hello world\n")
|
136
|
-
temp_file.rewind
|
137
|
-
temp_file
|
138
|
-
end
|
139
|
-
include_examples 'should upload'
|
140
|
-
|
141
|
-
it 'should not close stream' do
|
142
|
-
expect(file.closed?).to be false
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
146
|
-
context('with file path input') do
|
147
|
-
let(:file) { File.join(FIXTURES_DIR, 'files', 'test.txt') }
|
148
|
-
include_examples 'should upload'
|
149
|
-
end
|
150
|
-
|
151
|
-
context('with invalid input') do
|
152
|
-
let(:file) { -> {} }
|
153
|
-
it 'should raise client error' do
|
154
|
-
expect { command.execute(client) }.to raise_error(Google::Apis::ClientError)
|
155
|
-
end
|
156
|
-
end
|
157
|
-
end
|
158
|
-
|
159
|
-
RSpec.describe Google::Apis::Core::MultipartUploadCommand do
|
160
|
-
include TestHelpers
|
161
|
-
include_context 'HTTP client'
|
162
|
-
|
163
|
-
let(:command) do
|
164
|
-
command = Google::Apis::Core::MultipartUploadCommand.new(:post, 'https://www.googleapis.com/zoo/animals')
|
165
|
-
command.upload_source = StringIO.new('Hello world')
|
166
|
-
command.upload_content_type = 'text/plain'
|
167
|
-
command.body = 'metadata'
|
168
|
-
command
|
169
|
-
end
|
170
|
-
|
171
|
-
before(:example) do
|
172
|
-
stub_request(:post, 'https://www.googleapis.com/zoo/animals').to_return(body: %(Hello world))
|
173
|
-
end
|
174
|
-
|
175
|
-
it 'should send content' do
|
176
|
-
expected_body = <<EOF.gsub(/\n/, "\r\n")
|
177
|
-
--RubyApiClientUpload
|
178
|
-
Content-Type: application/json
|
179
|
-
|
180
|
-
metadata
|
181
|
-
--RubyApiClientUpload
|
182
|
-
Content-Length: 11
|
183
|
-
Content-Type: text/plain
|
184
|
-
Content-Transfer-Encoding: binary
|
185
|
-
|
186
|
-
Hello world
|
187
|
-
--RubyApiClientUpload--
|
188
|
-
|
189
|
-
EOF
|
190
|
-
command.execute(client)
|
191
|
-
expect(a_request(:post, 'https://www.googleapis.com/zoo/animals')
|
192
|
-
.with(body: expected_body)).to have_been_made
|
193
|
-
end
|
194
|
-
|
195
|
-
it 'should send upload protocol' do
|
196
|
-
command.execute(client)
|
197
|
-
expect(a_request(:post, 'https://www.googleapis.com/zoo/animals')
|
198
|
-
.with { |req| req.headers['X-Goog-Upload-Protocol'] == 'multipart' }).to have_been_made
|
199
|
-
end
|
200
|
-
end
|
201
|
-
|
202
|
-
RSpec.describe Google::Apis::Core::ResumableUploadCommand do
|
203
|
-
include TestHelpers
|
204
|
-
include_context 'HTTP client'
|
205
|
-
|
206
|
-
let(:command) do
|
207
|
-
command = Google::Apis::Core::ResumableUploadCommand.new(:post, 'https://www.googleapis.com/zoo/animals')
|
208
|
-
command.upload_source = StringIO.new('Hello world')
|
209
|
-
command.upload_content_type = 'text/plain'
|
210
|
-
command
|
211
|
-
end
|
212
|
-
|
213
|
-
context 'with uninterrupted upload' do
|
214
|
-
before(:example) do
|
215
|
-
stub_request(:post, 'https://www.googleapis.com/zoo/animals')
|
216
|
-
.to_return(headers: { 'X-Goog-Upload-Status' => 'active', 'X-Goog-Upload-URL' => 'https://www.googleapis.com/zoo/animals' })
|
217
|
-
.to_return(headers: { 'X-Goog-Upload-Status' => 'final' }, body: %(OK))
|
218
|
-
end
|
219
|
-
|
220
|
-
it 'should send upload protocol' do
|
221
|
-
command.execute(client)
|
222
|
-
expect(a_request(:post, 'https://www.googleapis.com/zoo/animals')
|
223
|
-
.with { |req| req.headers['X-Goog-Upload-Protocol'] == 'resumable' }).to have_been_made
|
224
|
-
end
|
225
|
-
|
226
|
-
it 'should send start command' do
|
227
|
-
command.execute(client)
|
228
|
-
expect(a_request(:post, 'https://www.googleapis.com/zoo/animals')
|
229
|
-
.with { |req| req.headers['X-Goog-Upload-Command'] == 'start' }).to have_been_made
|
230
|
-
end
|
231
|
-
|
232
|
-
it 'should send upload command' do
|
233
|
-
command.execute(client)
|
234
|
-
expect(a_request(:post, 'https://www.googleapis.com/zoo/animals')
|
235
|
-
.with { |req| req.headers['X-Goog-Upload-Command'].include?('upload') }).to have_been_made
|
236
|
-
end
|
237
|
-
|
238
|
-
it 'should send upload content' do
|
239
|
-
command.execute(client)
|
240
|
-
expect(a_request(:post, 'https://www.googleapis.com/zoo/animals')
|
241
|
-
.with(body: 'Hello world')).to have_been_made
|
242
|
-
end
|
243
|
-
end
|
244
|
-
|
245
|
-
context 'with retriable error on start' do
|
246
|
-
before(:example) do
|
247
|
-
stub_request(:post, 'https://www.googleapis.com/zoo/animals')
|
248
|
-
.to_timeout
|
249
|
-
.to_return(headers: { 'X-Goog-Upload-Status' => 'active', 'X-Goog-Upload-URL' => 'https://www.googleapis.com/zoo/animals' })
|
250
|
-
.to_return(headers: { 'X-Goog-Upload-Status' => 'active', 'X-Goog-Upload-Size-Received' => '6' })
|
251
|
-
.to_return(headers: { 'X-Goog-Upload-Status' => 'final' }, body: %(OK))
|
252
|
-
end
|
253
|
-
|
254
|
-
it 'should retry start command and continue' do
|
255
|
-
command.execute(client)
|
256
|
-
expect(a_request(:post, 'https://www.googleapis.com/zoo/animals')
|
257
|
-
.with(body: 'Hello world')).to have_been_made
|
258
|
-
end
|
259
|
-
end
|
260
|
-
|
261
|
-
context 'with non-retriable authorization error on start' do
|
262
|
-
before(:example) do
|
263
|
-
stub_request(:post, 'https://www.googleapis.com/zoo/animals')
|
264
|
-
.to_return(status: [401, 'unauthorized'], headers: { 'X-Goog-Upload-Status' => 'final' }, body: %(unauthorized))
|
265
|
-
end
|
266
|
-
|
267
|
-
it 'should propagate the original error' do
|
268
|
-
expect { command.execute(client) }.to raise_error Google::Apis::AuthorizationError
|
269
|
-
end
|
270
|
-
end
|
271
|
-
|
272
|
-
context 'with interruption' do
|
273
|
-
before(:example) do
|
274
|
-
stub_request(:post, 'https://www.googleapis.com/zoo/animals')
|
275
|
-
.to_return(headers: { 'X-Goog-Upload-Status' => 'active', 'X-Goog-Upload-URL' => 'https://www.googleapis.com/zoo/animals' })
|
276
|
-
.to_return(status: [500, 'Server error'])
|
277
|
-
.to_return(headers: { 'X-Goog-Upload-Status' => 'active', 'X-Goog-Upload-Size-Received' => '6' })
|
278
|
-
.to_return(headers: { 'X-Goog-Upload-Status' => 'final' }, body: %(OK))
|
279
|
-
end
|
280
|
-
|
281
|
-
it 'should send remaining upload content after failure' do
|
282
|
-
command.execute(client)
|
283
|
-
expect(a_request(:post, 'https://www.googleapis.com/zoo/animals')
|
284
|
-
.with(body: 'world')).to have_been_made
|
285
|
-
end
|
286
|
-
end
|
287
|
-
|
288
|
-
context 'with cancelled upload' do
|
289
|
-
before(:example) do
|
290
|
-
stub_request(:post, 'https://www.googleapis.com/zoo/animals')
|
291
|
-
.to_return(headers: { 'X-Goog-Upload-Status' => 'active', 'X-Goog-Upload-URL' => 'https://www.googleapis.com/zoo/animals' })
|
292
|
-
.to_return(status: [500, 'Server error'])
|
293
|
-
.to_return(headers: { 'X-Goog-Upload-Status' => 'cancelled' })
|
294
|
-
end
|
295
|
-
|
296
|
-
it 'should raise error' do
|
297
|
-
expect { command.execute(client) }.to raise_error Google::Apis::ClientError
|
298
|
-
end
|
299
|
-
end
|
300
|
-
end
|