google-api-client 0.6.4 → 0.7.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
@@ -15,3 +15,4 @@
15
15
  require 'google/api_client/auth/pkcs12'
16
16
  require 'google/api_client/auth/jwt_asserter'
17
17
  require 'google/api_client/auth/key_utils'
18
+ require 'google/api_client/auth/compute_service_account'
@@ -21,9 +21,10 @@ if !defined?(::Google::APIClient::VERSION)
21
21
  class APIClient
22
22
  module VERSION
23
23
  MAJOR = 0
24
- MINOR = 6
25
- TINY = 4
26
- STRING = [MAJOR, MINOR, TINY].join('.')
24
+ MINOR = 7
25
+ TINY = 0
26
+ PATCH = 'rc2'
27
+ STRING = [MAJOR, MINOR, TINY, PATCH].compact.join('.')
27
28
  end
28
29
  end
29
30
  end
@@ -234,7 +234,7 @@ describe Google::APIClient::BatchRequest do
234
234
  it 'should convert to a correct HTTP request' do
235
235
  batch = Google::APIClient::BatchRequest.new { |result| }
236
236
  batch.add(@call1, '1').add(@call2, '2')
237
- request = batch.to_env(Faraday.default_connection)
237
+ request = batch.to_env(CLIENT.connection)
238
238
  boundary = Google::APIClient::BatchRequest::BATCH_BOUNDARY
239
239
  request[:method].to_s.downcase.should == 'post'
240
240
  request[:url].to_s.should == 'https://www.googleapis.com/batch'
@@ -18,29 +18,12 @@
18
18
  require 'spec_helper'
19
19
 
20
20
  require 'faraday'
21
- require 'faraday/utils'
22
21
  require 'multi_json'
23
22
  require 'compat/multi_json'
24
23
  require 'signet/oauth_1/client'
25
24
  require 'google/api_client'
26
25
  require 'google/api_client/version'
27
26
 
28
- def TestHandler
29
- def initialize(&block)
30
- @block = block
31
- end
32
-
33
- def call(env)
34
- @block.call(env)
35
- end
36
- end
37
-
38
- def mock_connection(&block)
39
- connection = Faraday.new do |builder|
40
- use TestHandler block
41
- end
42
- end
43
-
44
27
  describe Google::APIClient do
45
28
  include ConnectionHelpers
46
29
  CLIENT = Google::APIClient.new(:application_name => 'API Client Tests') unless defined?(CLIENT)
@@ -231,16 +214,15 @@ describe Google::APIClient do
231
214
  conn.verify
232
215
  end
233
216
 
234
- it 'should generate valid requests when repeated parameters are passed' do
235
- pending("This is caused by Faraday's encoding of query parameters.")
217
+ it 'should generate valid requests when multivalued parameters are passed' do
236
218
  conn = stub_connection do |stub|
237
219
  stub.post('/prediction/v1.2/training?data=1&data=2') do |env|
238
- env[:params]['data'].should include('1', '2')
220
+ env.params['data'].should include('1', '2')
239
221
  end
240
222
  end
241
223
  request = CLIENT.execute(
242
224
  :api_method => @prediction.training.insert,
243
- :parameters => [['data', '1'], ['data','2']],
225
+ :parameters => {'data' => ['1', '2']},
244
226
  :connection => conn
245
227
  )
246
228
  conn.verify
@@ -465,92 +447,29 @@ describe Google::APIClient do
465
447
  'userId' => '107807692475771887386', 'collection' => 'bogus'
466
448
  },
467
449
  :authenticated => false
468
- ).to_env(Faraday.default_connection)
450
+ ).to_env(CLIENT.connection)
469
451
  end).should raise_error(ArgumentError)
470
452
  end
471
453
  end
472
-
473
- describe 'with the latitude API' do
474
- before do
475
- CLIENT.authorization = nil
476
- @latitude = CLIENT.discovered_api('latitude')
477
- end
478
-
479
- it 'should correctly determine the discovery URI' do
480
- CLIENT.discovery_uri('latitude').should ===
481
- 'https://www.googleapis.com/discovery/v1/apis/latitude/v1/rest'
482
- end
483
-
484
- it 'should find APIs that are in the discovery document' do
485
- CLIENT.discovered_api('latitude').name.should == 'latitude'
486
- CLIENT.discovered_api('latitude').version.should == 'v1'
487
- end
488
-
489
- it 'should return a batch path' do
490
- CLIENT.discovered_api('latitude').batch_path.should_not be_nil
491
- end
492
-
493
- it 'should find methods that are in the discovery document' do
494
- CLIENT.discovered_method(
495
- 'latitude.currentLocation.get', 'latitude'
496
- ).name.should == 'get'
497
- end
498
-
499
- it 'should define the origin API in discovered methods' do
500
- CLIENT.discovered_method(
501
- 'latitude.currentLocation.get', 'latitude'
502
- ).api.name.should == 'latitude'
503
- end
504
-
505
- it 'should not find methods that are not in the discovery document' do
506
- CLIENT.discovered_method('latitude.bogus', 'latitude').should == nil
507
- end
508
-
509
- it 'should generate requests against the correct URIs' do
510
- request = CLIENT.generate_request(
511
- :api_method => @latitude.current_location.get,
512
- :authenticated => false
513
- )
514
- request.to_env(Faraday.default_connection)[:url].to_s.should ===
515
- 'https://www.googleapis.com/latitude/v1/currentLocation'
516
- end
517
-
518
- it 'should generate requests against the correct URIs' do
519
- request = CLIENT.generate_request(
520
- :api_method => @latitude.current_location.get,
521
- :authenticated => false
522
- )
523
- request.to_env(Faraday.default_connection)[:url].to_s.should ===
524
- 'https://www.googleapis.com/latitude/v1/currentLocation'
525
- end
526
-
527
- it 'should not be able to execute requests without authorization' do
528
- result = CLIENT.execute(
529
- :api_method => @latitude.current_location.get,
530
- :authenticated => false
531
- )
532
- result.response.status.should == 401
533
- end
534
- end
535
454
 
536
455
  describe 'with the adsense API' do
537
456
  before do
538
457
  CLIENT.authorization = nil
539
- @adsense = CLIENT.discovered_api('adsense', 'v1')
458
+ @adsense = CLIENT.discovered_api('adsense', 'v1.3')
540
459
  end
541
460
 
542
461
  it 'should correctly determine the discovery URI' do
543
- CLIENT.discovery_uri('adsense').should ===
544
- 'https://www.googleapis.com/discovery/v1/apis/adsense/v1/rest'
462
+ CLIENT.discovery_uri('adsense', 'v1.3').to_s.should ===
463
+ 'https://www.googleapis.com/discovery/v1/apis/adsense/v1.3/rest'
545
464
  end
546
465
 
547
466
  it 'should find APIs that are in the discovery document' do
548
- CLIENT.discovered_api('adsense').name.should == 'adsense'
549
- CLIENT.discovered_api('adsense').version.should == 'v1'
467
+ CLIENT.discovered_api('adsense', 'v1.3').name.should == 'adsense'
468
+ CLIENT.discovered_api('adsense', 'v1.3').version.should == 'v1.3'
550
469
  end
551
470
 
552
471
  it 'should return a batch path' do
553
- CLIENT.discovered_api('adsense').batch_path.should_not be_nil
472
+ CLIENT.discovered_api('adsense', 'v1.3').batch_path.should_not be_nil
554
473
  end
555
474
 
556
475
  it 'should find methods that are in the discovery document' do
@@ -565,7 +484,7 @@ describe Google::APIClient do
565
484
 
566
485
  it 'should generate requests against the correct URIs' do
567
486
  conn = stub_connection do |stub|
568
- stub.get('/adsense/v1/adclients') do |env|
487
+ stub.get('/adsense/v1.3/adclients') do |env|
569
488
  end
570
489
  end
571
490
  request = CLIENT.execute(
@@ -595,7 +514,7 @@ describe Google::APIClient do
595
514
 
596
515
  it 'should succeed when validating parameters in a correct call' do
597
516
  conn = stub_connection do |stub|
598
- stub.get('/adsense/v1/reports?dimension=DATE&endDate=2010-01-01&metric=PAGE_VIEWS&startDate=2000-01-01') do |env|
517
+ stub.get('/adsense/v1.3/reports?dimension=DATE&endDate=2010-01-01&metric=PAGE_VIEWS&startDate=2000-01-01') do |env|
599
518
  end
600
519
  end
601
520
  (lambda do
@@ -630,9 +549,8 @@ describe Google::APIClient do
630
549
  end
631
550
 
632
551
  it 'should succeed when validating repeated parameters in a correct call' do
633
- pending("This is caused by Faraday's encoding of query parameters.")
634
552
  conn = stub_connection do |stub|
635
- stub.get('/adsense/v1/reports?dimension=DATE&dimension=PRODUCT_CODE'+
553
+ stub.get('/adsense/v1.3/reports?dimension=DATE&dimension=PRODUCT_CODE'+
636
554
  '&endDate=2010-01-01&metric=CLICKS&metric=PAGE_VIEWS&'+
637
555
  'startDate=2000-01-01') do |env|
638
556
  end
@@ -667,6 +585,29 @@ describe Google::APIClient do
667
585
  )
668
586
  end).should raise_error(ArgumentError)
669
587
  end
588
+
589
+ it 'should generate valid requests when multivalued parameters are passed' do
590
+ conn = stub_connection do |stub|
591
+ stub.get('/adsense/v1.3/reports?dimension=DATE&dimension=PRODUCT_CODE'+
592
+ '&endDate=2010-01-01&metric=CLICKS&metric=PAGE_VIEWS&'+
593
+ 'startDate=2000-01-01') do |env|
594
+ env.params['dimension'].should include('DATE', 'PRODUCT_CODE')
595
+ env.params['metric'].should include('CLICKS', 'PAGE_VIEWS')
596
+ end
597
+ end
598
+ request = CLIENT.execute(
599
+ :api_method => @adsense.reports.generate,
600
+ :parameters => {
601
+ 'startDate' => '2000-01-01',
602
+ 'endDate' => '2010-01-01',
603
+ 'dimension' => ['DATE', 'PRODUCT_CODE'],
604
+ 'metric' => ['PAGE_VIEWS', 'CLICKS']
605
+ },
606
+ :authenticated => false,
607
+ :connection => conn
608
+ )
609
+ conn.verify
610
+ end
670
611
  end
671
612
 
672
613
  describe 'with the Drive API' do
@@ -0,0 +1,86 @@
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
+ describe Google::APIClient::Gzip do
21
+
22
+ def create_connection(&block)
23
+ Faraday.new do |b|
24
+ b.response :gzip
25
+ b.adapter :test do |stub|
26
+ stub.get '/', &block
27
+ end
28
+ end
29
+ end
30
+
31
+ it 'should ignore non-zipped content' do
32
+ conn = create_connection do |env|
33
+ [200, {}, 'Hello world']
34
+ end
35
+ result = conn.get('/')
36
+ result.body.should == "Hello world"
37
+ end
38
+
39
+ it 'should decompress gziped content' do
40
+ conn = create_connection do |env|
41
+ [200, { 'Content-Encoding' => 'gzip'}, Base64.decode64('H4sICLVGwlEAA3RtcADzSM3JyVcozy/KSeECANXgObcMAAAA')]
42
+ end
43
+ result = conn.get('/')
44
+ result.body.should == "Hello world\n"
45
+ end
46
+
47
+ describe 'with API Client' do
48
+
49
+ before do
50
+ @client = Google::APIClient.new(:application_name => 'test')
51
+ @client.authorization = nil
52
+ end
53
+
54
+
55
+ it 'should send gzip in user agent' do
56
+ conn = create_connection do |env|
57
+ agent = env[:request_headers]['User-Agent']
58
+ agent.should_not be_nil
59
+ agent.should include 'gzip'
60
+ [200, {}, 'Hello world']
61
+ end
62
+ @client.execute(:uri => 'http://www.example.com/', :connection => conn)
63
+ end
64
+
65
+ it 'should send gzip in accept-encoding' do
66
+ conn = create_connection do |env|
67
+ encoding = env[:request_headers]['Accept-Encoding']
68
+ encoding.should_not be_nil
69
+ encoding.should include 'gzip'
70
+ [200, {}, 'Hello world']
71
+ end
72
+ @client.execute(:uri => 'http://www.example.com/', :connection => conn)
73
+ end
74
+
75
+ it 'should not send gzip in accept-encoding if disabled for request' do
76
+ conn = create_connection do |env|
77
+ encoding = env[:request_headers]['Accept-Encoding']
78
+ encoding.should_not include('gzip') unless encoding.nil?
79
+ [200, {}, 'Hello world']
80
+ end
81
+ response = @client.execute(:uri => 'http://www.example.com/', :gzip => false, :connection => conn)
82
+ puts response.status
83
+ end
84
+
85
+ end
86
+ end
@@ -57,6 +57,54 @@ describe Google::APIClient::UploadIO do
57
57
  end
58
58
  end
59
59
 
60
+ describe Google::APIClient::RangedIO do
61
+ before do
62
+ @source = StringIO.new("1234567890abcdef")
63
+ @io = Google::APIClient::RangedIO.new(@source, 1, 5)
64
+ end
65
+
66
+ it 'should return the correct range when read entirely' do
67
+ @io.read.should == "23456"
68
+ end
69
+
70
+ it 'should maintain position' do
71
+ @io.read(1).should == '2'
72
+ @io.read(2).should == '34'
73
+ @io.read(2).should == '56'
74
+ end
75
+
76
+ it 'should allow rewinds' do
77
+ @io.read(2).should == '23'
78
+ @io.rewind()
79
+ @io.read(2).should == '23'
80
+ end
81
+
82
+ it 'should allow setting position' do
83
+ @io.pos = 3
84
+ @io.read.should == '56'
85
+ end
86
+
87
+ it 'should not allow position to be set beyond range' do
88
+ @io.pos = 10
89
+ @io.read.should == ''
90
+ end
91
+
92
+ it 'should return empty string when read amount is zero' do
93
+ @io.read(0).should == ''
94
+ end
95
+
96
+ it 'should return empty string at EOF if amount is nil' do
97
+ @io.read
98
+ @io.read.should == ''
99
+ end
100
+
101
+ it 'should return nil at EOF if amount is positive int' do
102
+ @io.read
103
+ @io.read(1).should == nil
104
+ end
105
+
106
+ end
107
+
60
108
  describe Google::APIClient::ResumableUpload do
61
109
  CLIENT = Google::APIClient.new(:application_name => 'API Client Tests') unless defined?(CLIENT)
62
110
 
@@ -125,7 +173,7 @@ describe Google::APIClient::ResumableUpload do
125
173
 
126
174
  def mock_result(status, headers = {})
127
175
  reference = Google::APIClient::Reference.new(:api_method => @drive.files.insert)
128
- stub('result', :status => status, :headers => headers, :reference => reference)
176
+ double('result', :status => status, :headers => headers, :reference => reference)
129
177
  end
130
178
 
131
179
  end
@@ -0,0 +1,30 @@
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
+ describe Google::APIClient::Request do
21
+ CLIENT = Google::APIClient.new(:application_name => 'API Client Tests') unless defined?(CLIENT)
22
+
23
+ it 'should normalize parameter names to strings' do
24
+ request = Google::APIClient::Request.new(:uri => 'https://www.google.com', :parameters => {
25
+ :a => '1', 'b' => '2'
26
+ })
27
+ request.parameters['a'].should == '1'
28
+ request.parameters['b'].should == '2'
29
+ end
30
+ end
@@ -34,8 +34,8 @@ describe Google::APIClient::Result do
34
34
  })
35
35
  @request = @reference.to_http_request
36
36
 
37
- # Response stub
38
- @response = stub("response")
37
+ # Response double
38
+ @response = double("response")
39
39
  @response.stub(:status).and_return(200)
40
40
  @response.stub(:headers).and_return({
41
41
  'etag' => '12345',
@@ -78,11 +78,10 @@ describe Google::APIClient::Result do
78
78
  end
79
79
 
80
80
  it 'should escape the next page token when calling next_page' do
81
- pending("This is caused by Faraday's encoding of query parameters.")
82
81
  reference = @result.next_page
83
82
  Hash[reference.parameters].should include('pageToken')
84
83
  Hash[reference.parameters]['pageToken'].should == 'NEXT+PAGE+TOKEN'
85
- url = reference.to_env(Faraday.default_connection)[:url]
84
+ url = reference.to_env(CLIENT.connection)[:url]
86
85
  url.to_s.should include('pageToken=NEXT%2BPAGE%2BTOKEN')
87
86
  end
88
87
 
@@ -141,3 +141,24 @@ describe Google::APIClient::JWTAsserter do
141
141
  end
142
142
  end
143
143
 
144
+ describe Google::APIClient::ComputeServiceAccount do
145
+ include ConnectionHelpers
146
+
147
+ it 'should query metadata server' do
148
+ conn = stub_connection do |stub|
149
+ stub.get('/computeMetadata/v1beta1/instance/service-accounts/default/token') do |env|
150
+ env.url.host.should == 'metadata'
151
+ [200, {}, '{
152
+ "access_token" : "1/abcdef1234567890",
153
+ "token_type" : "Bearer",
154
+ "expires_in" : 3600
155
+ }']
156
+ end
157
+ end
158
+ service_account = Google::APIClient::ComputeServiceAccount.new
159
+ auth = service_account.fetch_access_token!({ :connection => conn })
160
+ auth.should_not == nil?
161
+ auth["access_token"].should == "1/abcdef1234567890"
162
+ conn.verify
163
+ end
164
+ end