google-api-client 0.4.4 → 0.4.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -25,22 +25,22 @@ describe Google::APIClient::UploadIO do
25
25
  media = Google::APIClient::UploadIO.new('doesnotexist', 'text/plain')
26
26
  end).should raise_error
27
27
  end
28
-
28
+
29
29
  describe 'with a file' do
30
30
  before do
31
31
  @file = File.expand_path('files/sample.txt', fixtures_path)
32
32
  @media = Google::APIClient::UploadIO.new(@file, 'text/plain')
33
33
  end
34
-
34
+
35
35
  it 'should report the correct file length' do
36
36
  @media.length.should == File.size(@file)
37
37
  end
38
-
38
+
39
39
  it 'should have a mime type' do
40
40
  @media.content_type.should == 'text/plain'
41
41
  end
42
42
  end
43
-
43
+
44
44
  describe 'with StringIO' do
45
45
  before do
46
46
  @content = "hello world"
@@ -50,7 +50,7 @@ describe Google::APIClient::UploadIO do
50
50
  it 'should report the correct file length' do
51
51
  @media.length.should == @content.length
52
52
  end
53
-
53
+
54
54
  it 'should have a mime type' do
55
55
  @media.content_type.should == 'text/plain'
56
56
  end
@@ -58,9 +58,16 @@ describe Google::APIClient::UploadIO do
58
58
  end
59
59
 
60
60
  describe Google::APIClient::ResumableUpload do
61
+ CLIENT ||= Google::APIClient.new
62
+
63
+ after do
64
+ # Reset client to not-quite-pristine state
65
+ CLIENT.key = nil
66
+ CLIENT.user_ip = nil
67
+ end
68
+
61
69
  before do
62
- @client = Google::APIClient.new
63
- @drive = @client.discovered_api('drive', 'v1')
70
+ @drive = CLIENT.discovered_api('drive', 'v1')
64
71
  @file = File.expand_path('files/sample.txt', fixtures_path)
65
72
  @media = Google::APIClient::UploadIO.new(@file, 'text/plain')
66
73
  @uploader = Google::APIClient::ResumableUpload.new(
@@ -68,7 +75,7 @@ describe Google::APIClient::ResumableUpload do
68
75
  @media,
69
76
  'https://www.googleapis.com/upload/drive/v1/files/12345')
70
77
  end
71
-
78
+
72
79
  it 'should consider 20x status as complete' do
73
80
  api_client = stub('api', :execute => mock_result(200))
74
81
  @uploader.send_chunk(api_client)
@@ -93,8 +100,8 @@ describe Google::APIClient::ResumableUpload do
93
100
  @uploader.send_chunk(api_client)
94
101
  @uploader.location.should == 'https://www.googleapis.com/upload/drive/v1/files/abcdef'
95
102
  end
96
-
97
- it 'should resume from the saved range reported by the server' do
103
+
104
+ it 'should resume from the saved range reported by the server' do
98
105
  api_client = mock('api')
99
106
  api_client.should_receive(:execute).and_return(mock_result(308, 'range' => '0-99'))
100
107
  api_client.should_receive(:execute).with(
@@ -107,7 +114,7 @@ describe Google::APIClient::ResumableUpload do
107
114
  @uploader.send_chunk(api_client) # Send bytes 0-199, only 0-99 saved
108
115
  @uploader.send_chunk(api_client) # Send bytes 100-299
109
116
  end
110
-
117
+
111
118
  it 'should resync the offset after 5xx errors' do
112
119
  api_client = mock('api')
113
120
  api_client.should_receive(:execute).and_return(mock_result(500))
@@ -132,5 +139,5 @@ describe Google::APIClient::ResumableUpload do
132
139
  reference = Google::APIClient::Reference.new(:api_method => @drive.files.insert)
133
140
  stub('result', :status => status, :headers => headers, :reference => reference)
134
141
  end
135
-
142
+
136
143
  end
@@ -18,14 +18,12 @@ require 'google/api_client'
18
18
  require 'google/api_client/version'
19
19
 
20
20
  describe Google::APIClient::Result do
21
- before do
22
- @client = Google::APIClient.new
23
- end
21
+ CLIENT ||= Google::APIClient.new
24
22
 
25
23
  describe 'with the plus API' do
26
24
  before do
27
- @client.authorization = nil
28
- @plus = @client.discovered_api('plus', 'v1')
25
+ CLIENT.authorization = nil
26
+ @plus = CLIENT.discovered_api('plus', 'v1')
29
27
  @reference = Google::APIClient::Reference.new({
30
28
  :api_method => @plus.activities.list,
31
29
  :parameters => {
@@ -71,16 +69,20 @@ describe Google::APIClient::Result do
71
69
  @result = Google::APIClient::Result.new(@reference, @request, @response)
72
70
  end
73
71
 
72
+ it 'should indicate a successful response' do
73
+ @result.error?.should be_false
74
+ end
75
+
74
76
  it 'should return the correct next page token' do
75
77
  @result.next_page_token.should == 'NEXT+PAGE+TOKEN'
76
78
  end
77
79
 
78
80
  it 'should escape the next page token when calling next_page' do
79
81
  reference = @result.next_page
80
- reference.parameters.should include('pageToken')
81
- reference.parameters['pageToken'].should == 'NEXT+PAGE+TOKEN'
82
- path = reference.to_request.path.to_s
83
- path.should include 'pageToken=NEXT%2BPAGE%2BTOKEN'
82
+ Hash[reference.parameters].should include('pageToken')
83
+ Hash[reference.parameters]['pageToken'].should == 'NEXT+PAGE+TOKEN'
84
+ url = reference.to_request.to_env(Faraday.default_connection)[:url]
85
+ url.to_s.should include('pageToken=NEXT%2BPAGE%2BTOKEN')
84
86
  end
85
87
 
86
88
  it 'should return content type correctly' do
@@ -146,5 +148,38 @@ describe Google::APIClient::Result do
146
148
  @result.data.items.should be_empty
147
149
  end
148
150
  end
151
+
152
+ describe 'with JSON error response' do
153
+ before do
154
+ @response.stub(:body).and_return(
155
+ <<-END_OF_STRING
156
+ {
157
+ "error": {
158
+ "errors": [
159
+ {
160
+ "domain": "global",
161
+ "reason": "parseError",
162
+ "message": "Parse Error"
163
+ }
164
+ ],
165
+ "code": 400,
166
+ "message": "Parse Error"
167
+ }
168
+ }
169
+ END_OF_STRING
170
+ )
171
+ @response.stub(:status).and_return(400)
172
+ @result = Google::APIClient::Result.new(@reference, @request, @response)
173
+ end
174
+
175
+ it 'should return error status correctly' do
176
+ @result.error?.should be_true
177
+ end
178
+
179
+ it 'should return the correct error message' do
180
+ @result.error_message.should == 'Parse Error'
181
+ end
182
+
183
+ end
149
184
  end
150
185
  end
@@ -17,21 +17,21 @@ require 'spec_helper'
17
17
  require 'google/api_client'
18
18
 
19
19
  describe Google::APIClient::JWTAsserter do
20
-
20
+
21
21
  before do
22
22
  @key = OpenSSL::PKey::RSA.new 2048
23
23
  end
24
-
24
+
25
25
  it 'should generate valid JWTs' do
26
26
  asserter = Google::APIClient::JWTAsserter.new('client1', 'scope1 scope2', @key)
27
27
  jwt = asserter.to_jwt
28
- jwt.should_not == nil
29
-
28
+ jwt.should_not == nil
29
+
30
30
  claim = JWT.decode(jwt, @key.public_key, true)
31
31
  claim["iss"].should == 'client1'
32
32
  claim["scope"].should == 'scope1 scope2'
33
33
  end
34
-
34
+
35
35
  it 'should send valid access token request' do
36
36
  stubs = Faraday::Adapter::Test::Stubs.new do |stub|
37
37
  stub.post('/o/oauth2/token') do |env|
@@ -48,7 +48,7 @@ describe Google::APIClient::JWTAsserter do
48
48
  connection = Faraday.new(:url => 'https://accounts.google.com') do |builder|
49
49
  builder.adapter(:test, stubs)
50
50
  end
51
-
51
+
52
52
  asserter = Google::APIClient::JWTAsserter.new('client1', 'scope1 scope2', @key)
53
53
  auth = asserter.authorize(nil, { :connection => connection})
54
54
  auth.should_not == nil?
@@ -14,11 +14,11 @@
14
14
 
15
15
  require 'spec_helper'
16
16
 
17
- gem 'faraday', '~> 0.7.0'
17
+ gem 'faraday', '~> 0.8.1'
18
18
  require 'faraday'
19
19
  require 'faraday/utils'
20
20
 
21
- gem 'signet', '~> 0.3.0'
21
+ gem 'signet', '~> 0.4.0'
22
22
  require 'signet/oauth_1/client'
23
23
 
24
24
  require 'google/api_client'
@@ -26,26 +26,26 @@ require 'google/api_client/version'
26
26
 
27
27
  shared_examples_for 'configurable user agent' do
28
28
  it 'should allow the user agent to be modified' do
29
- @client.user_agent = 'Custom User Agent/1.2.3'
30
- @client.user_agent.should == 'Custom User Agent/1.2.3'
29
+ client.user_agent = 'Custom User Agent/1.2.3'
30
+ client.user_agent.should == 'Custom User Agent/1.2.3'
31
31
  end
32
32
 
33
33
  it 'should allow the user agent to be set to nil' do
34
- @client.user_agent = nil
35
- @client.user_agent.should == nil
34
+ client.user_agent = nil
35
+ client.user_agent.should == nil
36
36
  end
37
37
 
38
38
  it 'should not allow the user agent to be used with bogus values' do
39
39
  (lambda do
40
- @client.user_agent = 42
41
- @client.transmit(
40
+ client.user_agent = 42
41
+ client.transmit(
42
42
  ['GET', 'http://www.google.com/', [], []]
43
43
  )
44
44
  end).should raise_error(TypeError)
45
45
  end
46
46
 
47
47
  it 'should transmit a User-Agent header when sending requests' do
48
- @client.user_agent = 'Custom User Agent/1.2.3'
48
+ client.user_agent = 'Custom User Agent/1.2.3'
49
49
  request = Faraday::Request.new(:get) do |req|
50
50
  req.url('http://www.google.com/')
51
51
  end
@@ -53,48 +53,46 @@ shared_examples_for 'configurable user agent' do
53
53
  stub.get('/') do |env|
54
54
  headers = env[:request_headers]
55
55
  headers.should have_key('User-Agent')
56
- headers['User-Agent'].should == @client.user_agent
56
+ headers['User-Agent'].should == client.user_agent
57
57
  [200, {}, ['']]
58
58
  end
59
59
  end
60
60
  connection = Faraday.new(:url => 'https://www.google.com') do |builder|
61
61
  builder.adapter(:test, stubs)
62
62
  end
63
- @client.transmit(:request => request, :connection => connection)
63
+ client.transmit(:request => request, :connection => connection)
64
64
  stubs.verify_stubbed_calls
65
65
  end
66
66
  end
67
67
 
68
68
  describe Google::APIClient do
69
- before do
70
- @client = Google::APIClient.new
71
- end
69
+ let(:client) { Google::APIClient.new }
72
70
 
73
71
  it 'should make its version number available' do
74
72
  Google::APIClient::VERSION::STRING.should be_instance_of(String)
75
73
  end
76
74
 
77
75
  it 'should default to OAuth 2' do
78
- Signet::OAuth2::Client.should === @client.authorization
76
+ Signet::OAuth2::Client.should === client.authorization
79
77
  end
80
78
 
81
79
  it_should_behave_like 'configurable user agent'
82
80
 
83
81
  describe 'configured for OAuth 1' do
84
82
  before do
85
- @client.authorization = :oauth_1
83
+ client.authorization = :oauth_1
86
84
  end
87
85
 
88
86
  it 'should use the default OAuth1 client configuration' do
89
- @client.authorization.temporary_credential_uri.to_s.should ==
87
+ client.authorization.temporary_credential_uri.to_s.should ==
90
88
  'https://www.google.com/accounts/OAuthGetRequestToken'
91
- @client.authorization.authorization_uri.to_s.should include(
89
+ client.authorization.authorization_uri.to_s.should include(
92
90
  'https://www.google.com/accounts/OAuthAuthorizeToken'
93
91
  )
94
- @client.authorization.token_credential_uri.to_s.should ==
92
+ client.authorization.token_credential_uri.to_s.should ==
95
93
  'https://www.google.com/accounts/OAuthGetAccessToken'
96
- @client.authorization.client_credential_key.should == 'anonymous'
97
- @client.authorization.client_credential_secret.should == 'anonymous'
94
+ client.authorization.client_credential_key.should == 'anonymous'
95
+ client.authorization.client_credential_secret.should == 'anonymous'
98
96
  end
99
97
 
100
98
  it_should_behave_like 'configurable user agent'
@@ -102,7 +100,7 @@ describe Google::APIClient do
102
100
 
103
101
  describe 'configured for OAuth 2' do
104
102
  before do
105
- @client.authorization = :oauth_2
103
+ client.authorization = :oauth_2
106
104
  end
107
105
 
108
106
  # TODO
@@ -111,7 +109,7 @@ describe Google::APIClient do
111
109
 
112
110
  describe 'when executing requests' do
113
111
  before do
114
- @client.authorization = :oauth_2
112
+ client.authorization = :oauth_2
115
113
  @connection = Faraday.new(:url => 'https://www.googleapis.com') do |builder|
116
114
  stubs = Faraday::Adapter::Test::Stubs.new do |stub|
117
115
  stub.get('/test') do |env|
@@ -123,16 +121,16 @@ describe Google::APIClient do
123
121
  end
124
122
 
125
123
  it 'should use default authorization' do
126
- @client.authorization.access_token = "12345"
127
- @client.execute(:http_method => :get,
124
+ client.authorization.access_token = "12345"
125
+ client.execute(:http_method => :get,
128
126
  :uri => 'https://www.googleapis.com/test',
129
127
  :connection => @connection)
130
128
  end
131
129
 
132
130
  it 'should use request scoped authorization when provided' do
133
- @client.authorization.access_token = "abcdef"
131
+ client.authorization.access_token = "abcdef"
134
132
  new_auth = Signet::OAuth2::Client.new(:access_token => '12345')
135
- @client.execute(:http_method => :get,
133
+ client.execute(:http_method => :get,
136
134
  :uri => 'https://www.googleapis.com/test',
137
135
  :connection => @connection,
138
136
  :authorization => new_auth)
@@ -1,5 +1,7 @@
1
- spec_dir = File.expand_path("..", __FILE__)
2
- lib_dir = File.expand_path("../lib", spec_dir)
3
-
4
- $LOAD_PATH.unshift(lib_dir)
1
+ $LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
5
2
  $LOAD_PATH.uniq!
3
+
4
+ require 'rspec'
5
+
6
+ RSpec.configure do |config|
7
+ end
@@ -24,23 +24,20 @@ namespace :gem do
24
24
  s.rdoc_options.concat ['--main', 'README.md']
25
25
 
26
26
  # Dependencies used in the main library
27
- s.add_runtime_dependency('signet', '>= 0.3.4')
28
- s.add_runtime_dependency('addressable', '>= 2.2.3')
29
- s.add_runtime_dependency('autoparse', '>= 0.3.1')
30
- s.add_runtime_dependency('faraday', '~> 0.7.0')
31
- s.add_runtime_dependency('multi_json', '>= 1.3.0')
32
- s.add_runtime_dependency('extlib', '>= 0.9.15')
27
+ s.add_runtime_dependency('signet', '>= 0.4.1')
28
+ s.add_runtime_dependency('addressable', '>= 2.3.2')
33
29
  s.add_runtime_dependency('uuidtools', '>= 2.1.0')
30
+ s.add_runtime_dependency('autoparse', '>= 0.3.2')
31
+ s.add_runtime_dependency('faraday', '~> 0.8.1')
32
+ s.add_runtime_dependency('multi_json', '>= 1.0.0')
33
+ s.add_runtime_dependency('extlib', '>= 0.9.15')
34
34
 
35
35
  # Dependencies used in the CLI
36
- s.add_runtime_dependency('launchy', '>= 2.0.0')
36
+ s.add_runtime_dependency('launchy', '>= 2.1.1')
37
37
 
38
38
  # Dependencies used in the examples
39
- s.add_development_dependency('sinatra', '>= 1.2.0')
40
-
41
39
  s.add_development_dependency('rake', '>= 0.9.0')
42
- s.add_development_dependency('rspec', '~> 1.2.9')
43
- s.add_development_dependency('rcov', '>= 0.9.9')
40
+ s.add_development_dependency('rspec', '>= 2.11.0')
44
41
 
45
42
  s.require_path = 'lib'
46
43
 
@@ -1,16 +1,28 @@
1
- require 'spec/rake/verify_rcov'
2
1
  require 'rake/clean'
2
+ require 'rspec/core/rake_task'
3
3
 
4
4
  CLOBBER.include('coverage', 'specdoc')
5
5
 
6
6
  namespace :spec do
7
- Spec::Rake::SpecTask.new(:rcov) do |t|
8
- t.spec_files = FileList['spec/**/*_spec.rb']
9
- t.spec_opts = ['--require', 'rubygems', '--color', '--format', 'specdoc']
7
+ RSpec::Core::RakeTask.new(:all) do |t|
8
+ t.pattern = FileList['spec/**/*_spec.rb']
9
+ t.rspec_opts = ['--color', '--format', 'documentation']
10
+ t.rcov = false
11
+ end
12
+
13
+ desc 'Generate HTML Specdocs for all specs.'
14
+ RSpec::Core::RakeTask.new(:specdoc) do |t|
15
+ specdoc_path = File.expand_path('../../specdoc', __FILE__)
16
+
17
+ t.rspec_opts = %W( --format html --out #{File.join(specdoc_path, 'index.html')} )
18
+ t.fail_on_error = false
19
+ end
20
+
21
+ RSpec::Core::RakeTask.new(:rcov) do |t|
10
22
  if RCOV_ENABLED
11
23
  if `which rcov`.strip == ""
12
24
  STDERR.puts(
13
- "Please install rcov and ensure that its binary is in the PATH:"
25
+ "Please install rcov and ensure that its binary is in the PATH:"
14
26
  )
15
27
  STDERR.puts("sudo gem install rcov")
16
28
  exit(1)
@@ -19,51 +31,12 @@ namespace :spec do
19
31
  else
20
32
  t.rcov = false
21
33
  end
22
- t.rcov_opts = [
23
- '--exclude', 'lib\\/google\\/api_client\\/environment.rb',
24
- '--exclude', 'lib\\/compat',
25
- '--exclude', 'spec',
26
- '--exclude', '\\.rvm\\/gems',
27
- '--exclude', '1\\.8\\/gems',
28
- '--exclude', '1\\.9\\/gems',
29
- '--exclude', '\\.rvm',
30
- '--exclude', '\\/Library\\/Ruby',
31
- ]
32
- end
33
-
34
- Spec::Rake::SpecTask.new(:all) do |t|
35
- t.spec_files = FileList['spec/**/*_spec.rb']
36
- t.spec_opts = ['--require', 'rubygems', '--color', '--format', 'specdoc']
37
- t.rcov = false
38
- end
39
-
40
- Spec::Rake::SpecTask.new(:fast) do |t|
41
- t.spec_files = FileList['spec/**/*_spec.rb'].exclude(
42
- 'spec/**/*_slow_spec.rb'
34
+ t.rcov_opts = %w(
35
+ --exclude gems/
36
+ --exclude spec/
37
+ --exclude lib/google/api_client/environment.rb
38
+ --exclude lib/compat
43
39
  )
44
- t.spec_opts = ['--require', 'rubygems', '--color', '--format', 'specdoc']
45
- t.rcov = false
46
- end
47
-
48
- if RCOV_ENABLED
49
- RCov::VerifyTask.new(:verify) do |t|
50
- t.threshold = 65.0
51
- t.index_html = 'coverage/index.html'
52
- end
53
-
54
- task :verify => :rcov
55
- end
56
-
57
- desc 'Generate HTML Specdocs for all specs'
58
- Spec::Rake::SpecTask.new(:specdoc) do |t|
59
- specdoc_path = File.expand_path(
60
- File.join(File.dirname(__FILE__), '../specdoc/'))
61
- Dir.mkdir(specdoc_path) if !File.exist?(specdoc_path)
62
-
63
- output_file = File.join(specdoc_path, 'index.html')
64
- t.spec_files = FileList['spec/**/*_spec.rb']
65
- t.spec_opts = ['--format', "\"html:#{output_file}\"", '--diff']
66
- t.fail_on_error = false
67
40
  end
68
41
 
69
42
  namespace :rcov do