google-api-client 0.4.7 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,20 +17,33 @@
17
17
 
18
18
  require 'spec_helper'
19
19
 
20
- gem 'faraday', '~> 0.8.1'
21
20
  require 'faraday'
22
21
  require 'faraday/utils'
23
22
  require 'multi_json'
24
23
  require 'compat/multi_json'
25
-
26
- gem 'signet', '~> 0.4.0'
27
24
  require 'signet/oauth_1/client'
28
-
29
25
  require 'google/api_client'
30
26
  require 'google/api_client/version'
31
27
 
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
+
32
44
  describe Google::APIClient do
33
- CLIENT ||= Google::APIClient.new
45
+ include ConnectionHelpers
46
+ CLIENT = Google::APIClient.new unless defined?(CLIENT)
34
47
 
35
48
  after do
36
49
  # Reset client to not-quite-pristine state
@@ -67,7 +80,7 @@ describe Google::APIClient do
67
80
 
68
81
  it 'should raise an error for bogus methods' do
69
82
  (lambda do
70
- CLIENT.generate_request(42)
83
+ CLIENT.execute(42)
71
84
  end).should raise_error(TypeError)
72
85
  end
73
86
 
@@ -90,44 +103,49 @@ describe Google::APIClient do
90
103
 
91
104
  it 'should correctly determine the discovery URI if :user_ip is set' do
92
105
  CLIENT.user_ip = '127.0.0.1'
93
- request = CLIENT.generate_request(
106
+
107
+ conn = stub_connection do |stub|
108
+ stub.get('/discovery/v1/apis/prediction/v1.2/rest?userIp=127.0.0.1') do |env|
109
+ end
110
+ end
111
+ CLIENT.execute(
94
112
  :http_method => 'GET',
95
113
  :uri => CLIENT.discovery_uri('prediction', 'v1.2'),
96
- :authenticated => false
97
- )
98
- request.to_env(Faraday.default_connection)[:url].to_s.should === (
99
- 'https://www.googleapis.com/discovery/v1/apis/prediction/v1.2/rest' +
100
- '?userIp=127.0.0.1'
114
+ :authenticated => false,
115
+ :connection => conn
101
116
  )
117
+ conn.verify
102
118
  end
103
119
 
104
120
  it 'should correctly determine the discovery URI if :key is set' do
105
121
  CLIENT.key = 'qwerty'
106
- request = CLIENT.generate_request(
122
+ conn = stub_connection do |stub|
123
+ stub.get('/discovery/v1/apis/prediction/v1.2/rest?key=qwerty') do |env|
124
+ end
125
+ end
126
+ request = CLIENT.execute(
107
127
  :http_method => 'GET',
108
128
  :uri => CLIENT.discovery_uri('prediction', 'v1.2'),
109
- :authenticated => false
110
- )
111
- request.to_env(Faraday.default_connection)[:url].to_s.should === (
112
- 'https://www.googleapis.com/discovery/v1/apis/prediction/v1.2/rest' +
113
- '?key=qwerty'
114
- )
129
+ :authenticated => false,
130
+ :connection => conn
131
+ )
132
+ conn.verify
115
133
  end
116
134
 
117
135
  it 'should correctly determine the discovery URI if both are set' do
118
136
  CLIENT.key = 'qwerty'
119
137
  CLIENT.user_ip = '127.0.0.1'
120
- request = CLIENT.generate_request(
138
+ conn = stub_connection do |stub|
139
+ stub.get('/discovery/v1/apis/prediction/v1.2/rest?key=qwerty&userIp=127.0.0.1') do |env|
140
+ end
141
+ end
142
+ request = CLIENT.execute(
121
143
  :http_method => 'GET',
122
144
  :uri => CLIENT.discovery_uri('prediction', 'v1.2'),
123
- :authenticated => false
124
- )
125
- Addressable::URI.parse(
126
- request.to_env(Faraday.default_connection)[:url]
127
- ).query_values.should == {
128
- 'key' => 'qwerty',
129
- 'userIp' => '127.0.0.1'
130
- }
145
+ :authenticated => false,
146
+ :connection => conn
147
+ )
148
+ conn.verify
131
149
  end
132
150
 
133
151
  it 'should correctly generate API objects' do
@@ -169,7 +187,7 @@ describe Google::APIClient do
169
187
 
170
188
  it 'should raise an error for bogus methods' do
171
189
  (lambda do
172
- CLIENT.generate_request(CLIENT.discovered_api('prediction', 'v1.2'))
190
+ CLIENT.execute(:api_method => CLIENT.discovered_api('prediction', 'v1.2'))
173
191
  end).should raise_error(TypeError)
174
192
  end
175
193
 
@@ -183,43 +201,58 @@ describe Google::APIClient do
183
201
  end
184
202
 
185
203
  it 'should generate valid requests' do
186
- request = CLIENT.generate_request(
204
+ conn = stub_connection do |stub|
205
+ stub.post('/prediction/v1.2/training?data=12345') do |env|
206
+ env[:body].should == ''
207
+ end
208
+ end
209
+ request = CLIENT.execute(
187
210
  :api_method => @prediction.training.insert,
188
- :parameters => {'data' => '12345'}
211
+ :parameters => {'data' => '12345'},
212
+ :connection => conn
189
213
  )
190
- request.method.should == :post
191
- request.to_env(Faraday.default_connection)[:url].to_s.should ===
192
- 'https://www.googleapis.com/prediction/v1.2/training?data=12345'
193
- request.headers.should be_empty
194
- request.body.should == ''
214
+ conn.verify
195
215
  end
196
216
 
197
217
  it 'should generate valid requests when repeated parameters are passed' do
198
- request = CLIENT.generate_request(
218
+ pending("This is caused by Faraday's encoding of query parameters.")
219
+ conn = stub_connection do |stub|
220
+ stub.post('/prediction/v1.2/training?data=1&data=2') do |env|
221
+ env[:params]['data'].should include('1', '2')
222
+ end
223
+ end
224
+ request = CLIENT.execute(
199
225
  :api_method => @prediction.training.insert,
200
- :parameters => [['data', '1'], ['data','2']]
226
+ :parameters => [['data', '1'], ['data','2']],
227
+ :connection => conn
201
228
  )
202
- request.method.should == :post
203
- request.to_env(Faraday.default_connection)[:url].to_s.should ===
204
- 'https://www.googleapis.com/prediction/v1.2/training?data=1&data=2'
229
+ conn.verify
205
230
  end
206
231
 
207
232
  it 'should generate requests against the correct URIs' do
208
- request = CLIENT.generate_request(
233
+ conn = stub_connection do |stub|
234
+ stub.post('/prediction/v1.2/training?data=12345') do |env|
235
+ end
236
+ end
237
+ request = CLIENT.execute(
209
238
  :api_method => @prediction.training.insert,
210
- :parameters => {'data' => '12345'}
239
+ :parameters => {'data' => '12345'},
240
+ :connection => conn
211
241
  )
212
- request.to_env(Faraday.default_connection)[:url].to_s.should ===
213
- 'https://www.googleapis.com/prediction/v1.2/training?data=12345'
242
+ conn.verify
214
243
  end
215
244
 
216
245
  it 'should generate requests against the correct URIs' do
217
- request = CLIENT.generate_request(
246
+ conn = stub_connection do |stub|
247
+ stub.post('/prediction/v1.2/training?data=12345') do |env|
248
+ end
249
+ end
250
+ request = CLIENT.execute(
218
251
  :api_method => @prediction.training.insert,
219
- :parameters => {'data' => '12345'}
252
+ :parameters => {'data' => '12345'},
253
+ :connection => conn
220
254
  )
221
- request.to_env(Faraday.default_connection)[:url].to_s.should ===
222
- 'https://www.googleapis.com/prediction/v1.2/training?data=12345'
255
+ conn.verify
223
256
  end
224
257
 
225
258
  it 'should allow modification to the base URIs for testing purposes' do
@@ -228,37 +261,58 @@ describe Google::APIClient do
228
261
  Google::APIClient.new.discovered_api('prediction', 'v1.2')
229
262
  prediction_rebase.method_base =
230
263
  'https://testing-domain.example.com/prediction/v1.2/'
231
- request = CLIENT.generate_request(
264
+
265
+ conn = stub_connection do |stub|
266
+ stub.post('/prediction/v1.2/training') do |env|
267
+ env[:url].host.should == 'testing-domain.example.com'
268
+ end
269
+ end
270
+
271
+ request = CLIENT.execute(
232
272
  :api_method => prediction_rebase.training.insert,
233
- :parameters => {'data' => '123'}
234
- )
235
- request.to_env(Faraday.default_connection)[:url].to_s.should === (
236
- 'https://testing-domain.example.com/' +
237
- 'prediction/v1.2/training?data=123'
273
+ :parameters => {'data' => '123'},
274
+ :connection => conn
238
275
  )
276
+ conn.verify
239
277
  end
240
278
 
241
279
  it 'should generate OAuth 1 requests' do
242
280
  CLIENT.authorization = :oauth_1
243
281
  CLIENT.authorization.token_credential_key = '12345'
244
282
  CLIENT.authorization.token_credential_secret = '12345'
245
- request = CLIENT.generate_request(
283
+
284
+ conn = stub_connection do |stub|
285
+ stub.post('/prediction/v1.2/training?data=12345') do |env|
286
+ env[:request_headers].should have_key('Authorization')
287
+ env[:request_headers]['Authorization'].should =~ /^OAuth/
288
+ end
289
+ end
290
+
291
+ request = CLIENT.execute(
246
292
  :api_method => @prediction.training.insert,
247
- :parameters => {'data' => '12345'}
293
+ :parameters => {'data' => '12345'},
294
+ :connection => conn
248
295
  )
249
- request.headers.should have_key('Authorization')
250
- request.headers['Authorization'].should =~ /^OAuth/
296
+ conn.verify
251
297
  end
252
298
 
253
299
  it 'should generate OAuth 2 requests' do
254
300
  CLIENT.authorization = :oauth_2
255
301
  CLIENT.authorization.access_token = '12345'
256
- request = CLIENT.generate_request(
302
+
303
+ conn = stub_connection do |stub|
304
+ stub.post('/prediction/v1.2/training?data=12345') do |env|
305
+ env[:request_headers].should have_key('Authorization')
306
+ env[:request_headers]['Authorization'].should =~ /^Bearer/
307
+ end
308
+ end
309
+
310
+ request = CLIENT.execute(
257
311
  :api_method => @prediction.training.insert,
258
- :parameters => {'data' => '12345'}
312
+ :parameters => {'data' => '12345'},
313
+ :connection => conn
259
314
  )
260
- request.headers.should have_key('Authorization')
261
- request.headers['Authorization'].should =~ /^Bearer/
315
+ conn.verify
262
316
  end
263
317
 
264
318
  it 'should not be able to execute improperly authorized requests' do
@@ -306,15 +360,21 @@ describe Google::APIClient do
306
360
  end
307
361
 
308
362
  it 'should correctly handle unnamed parameters' do
363
+ conn = stub_connection do |stub|
364
+ stub.post('/prediction/v1.2/training') do |env|
365
+ env[:request_headers].should have_key('Content-Type')
366
+ env[:request_headers]['Content-Type'].should == 'application/json'
367
+ end
368
+ end
309
369
  CLIENT.authorization = :oauth_2
310
370
  CLIENT.authorization.access_token = '12345'
311
- result = CLIENT.execute(
312
- @prediction.training.insert,
313
- {},
314
- MultiJson.dump({"id" => "bucket/object"}),
315
- {'Content-Type' => 'application/json'}
371
+ CLIENT.execute(
372
+ :api_method => @prediction.training.insert,
373
+ :body => MultiJson.dump({"id" => "bucket/object"}),
374
+ :headers => {'Content-Type' => 'application/json'},
375
+ :connection => conn
316
376
  )
317
- result.request.headers['Content-Type'].should == 'application/json'
377
+ conn.verify
318
378
  end
319
379
  end
320
380
 
@@ -354,22 +414,26 @@ describe Google::APIClient do
354
414
  end
355
415
 
356
416
  it 'should generate requests against the correct URIs' do
357
- request = CLIENT.generate_request(
417
+ conn = stub_connection do |stub|
418
+ stub.get('/plus/v1/people/107807692475771887386/activities/public' +
419
+ '?collection=public&userId=107807692475771887386') do |env|
420
+ end
421
+ end
422
+
423
+ request = CLIENT.execute(
358
424
  :api_method => @plus.activities.list,
359
425
  :parameters => {
360
426
  'userId' => '107807692475771887386', 'collection' => 'public'
361
427
  },
362
- :authenticated => false
363
- )
364
- request.to_env(Faraday.default_connection)[:url].to_s.should === (
365
- 'https://www.googleapis.com/plus/v1/' +
366
- 'people/107807692475771887386/activities/public'
428
+ :authenticated => false,
429
+ :connection => conn
367
430
  )
431
+ conn.verify
368
432
  end
369
433
 
370
434
  it 'should correctly validate parameters' do
371
435
  (lambda do
372
- CLIENT.generate_request(
436
+ CLIENT.execute(
373
437
  :api_method => @plus.activities.list,
374
438
  :parameters => {'alt' => 'json'},
375
439
  :authenticated => false
@@ -379,17 +443,18 @@ describe Google::APIClient do
379
443
 
380
444
  it 'should correctly validate parameters' do
381
445
  (lambda do
382
- CLIENT.generate_request(
446
+ CLIENT.execute(
383
447
  :api_method => @plus.activities.list,
384
448
  :parameters => {
385
449
  'userId' => '107807692475771887386', 'collection' => 'bogus'
386
450
  },
387
451
  :authenticated => false
388
- )
452
+ ).to_env(Faraday.default_connection)
389
453
  end).should raise_error(ArgumentError)
390
454
  end
391
455
  end
392
-
456
+
457
+ =begin
393
458
  describe 'with the latitude API' do
394
459
  before do
395
460
  CLIENT.authorization = nil
@@ -452,6 +517,7 @@ describe Google::APIClient do
452
517
  result.response.status.should == 401
453
518
  end
454
519
  end
520
+ =end
455
521
 
456
522
  describe 'with the moderator API' do
457
523
  before do
@@ -490,26 +556,21 @@ describe Google::APIClient do
490
556
  end
491
557
 
492
558
  it 'should generate requests against the correct URIs' do
493
- request = CLIENT.generate_request(
494
- :api_method => 'moderator.profiles.get',
495
- :authenticated => false
496
- )
497
- request.to_env(Faraday.default_connection)[:url].to_s.should ===
498
- 'https://www.googleapis.com/moderator/v1/profiles/@me'
499
- end
500
-
501
- it 'should generate requests against the correct URIs' do
502
- request = CLIENT.generate_request(
559
+ conn = stub_connection do |stub|
560
+ stub.get('/moderator/v1/profiles/@me') do |env|
561
+ end
562
+ end
563
+ request = CLIENT.execute(
503
564
  :api_method => @moderator.profiles.get,
504
- :authenticated => false
565
+ :authenticated => false,
566
+ :connection => conn
505
567
  )
506
- request.to_env(Faraday.default_connection)[:url].to_s.should ===
507
- 'https://www.googleapis.com/moderator/v1/profiles/@me'
568
+ conn.verify
508
569
  end
509
570
 
510
571
  it 'should not be able to execute requests without authorization' do
511
572
  result = CLIENT.execute(
512
- 'moderator.profiles.get',
573
+ @moderator.profiles.get,
513
574
  {},
514
575
  '',
515
576
  [],
@@ -550,26 +611,21 @@ describe Google::APIClient do
550
611
  end
551
612
 
552
613
  it 'should generate requests against the correct URIs' do
553
- request = CLIENT.generate_request(
554
- :api_method => 'adsense.adclients.list',
555
- :authenticated => false
556
- )
557
- request.to_env(Faraday.default_connection)[:url].to_s.should ===
558
- 'https://www.googleapis.com/adsense/v1/adclients'
559
- end
560
-
561
- it 'should generate requests against the correct URIs' do
562
- request = CLIENT.generate_request(
614
+ conn = stub_connection do |stub|
615
+ stub.get('/adsense/v1/adclients') do |env|
616
+ end
617
+ end
618
+ request = CLIENT.execute(
563
619
  :api_method => @adsense.adclients.list,
564
- :authenticated => false
620
+ :authenticated => false,
621
+ :connection => conn
565
622
  )
566
- request.to_env(Faraday.default_connection)[:url].to_s.should ===
567
- 'https://www.googleapis.com/adsense/v1/adclients'
623
+ conn.verify
568
624
  end
569
625
 
570
626
  it 'should not be able to execute requests without authorization' do
571
627
  result = CLIENT.execute(
572
- :api_method => 'adsense.adclients.list',
628
+ :api_method => @adsense.adclients.list,
573
629
  :authenticated => false
574
630
  )
575
631
  result.response.status.should == 401
@@ -577,7 +633,7 @@ describe Google::APIClient do
577
633
 
578
634
  it 'should fail when validating missing required parameters' do
579
635
  (lambda do
580
- CLIENT.generate_request(
636
+ CLIENT.execute(
581
637
  :api_method => @adsense.reports.generate,
582
638
  :authenticated => false
583
639
  )
@@ -585,8 +641,12 @@ describe Google::APIClient do
585
641
  end
586
642
 
587
643
  it 'should succeed when validating parameters in a correct call' do
644
+ conn = stub_connection do |stub|
645
+ stub.get('/adsense/v1/reports?dimension=DATE&endDate=2010-01-01&metric=PAGE_VIEWS&startDate=2000-01-01') do |env|
646
+ end
647
+ end
588
648
  (lambda do
589
- CLIENT.generate_request(
649
+ CLIENT.execute(
590
650
  :api_method => @adsense.reports.generate,
591
651
  :parameters => {
592
652
  'startDate' => '2000-01-01',
@@ -594,14 +654,16 @@ describe Google::APIClient do
594
654
  'dimension' => 'DATE',
595
655
  'metric' => 'PAGE_VIEWS'
596
656
  },
597
- :authenticated => false
657
+ :authenticated => false,
658
+ :connection => conn
598
659
  )
599
660
  end).should_not raise_error
661
+ conn.verify
600
662
  end
601
663
 
602
664
  it 'should fail when validating parameters with invalid values' do
603
665
  (lambda do
604
- CLIENT.generate_request(
666
+ CLIENT.execute(
605
667
  :api_method => @adsense.reports.generate,
606
668
  :parameters => {
607
669
  'startDate' => '2000-01-01',
@@ -615,8 +677,14 @@ describe Google::APIClient do
615
677
  end
616
678
 
617
679
  it 'should succeed when validating repeated parameters in a correct call' do
680
+ conn = stub_connection do |stub|
681
+ stub.get('/adsense/v1/reports?dimension%5B%5D=DATE&dimension%5B%5D=PRODUCT_CODE'+
682
+ '&endDate=2010-01-01&metric%5B%5D=CLICKS&metric%5B%5D=PAGE_VIEWS&'+
683
+ 'startDate=2000-01-01') do |env|
684
+ end
685
+ end
618
686
  (lambda do
619
- CLIENT.generate_request(
687
+ CLIENT.execute(
620
688
  :api_method => @adsense.reports.generate,
621
689
  :parameters => {
622
690
  'startDate' => '2000-01-01',
@@ -624,14 +692,16 @@ describe Google::APIClient do
624
692
  'dimension' => ['DATE', 'PRODUCT_CODE'],
625
693
  'metric' => ['PAGE_VIEWS', 'CLICKS']
626
694
  },
627
- :authenticated => false
695
+ :authenticated => false,
696
+ :connection => conn
628
697
  )
629
698
  end).should_not raise_error
699
+ conn.verify
630
700
  end
631
701
 
632
702
  it 'should fail when validating incorrect repeated parameters' do
633
703
  (lambda do
634
- CLIENT.generate_request(
704
+ CLIENT.execute(
635
705
  :api_method => @adsense.reports.generate,
636
706
  :parameters => {
637
707
  'startDate' => '2000-01-01',