Resolve merge conflict

This commit is contained in:
Steven Bazyl committed 2012-06-06 17:32:29 -07:00
commit a9d46c0359
12 files changed
+792 -70

No files matched your search

+237
View File
@@ -0,0 +1,237 @@
# Copyright 2012 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'spec_helper'
require 'google/api_client'
require 'google/api_client/version'
describe Google::APIClient::BatchRequest do
before do
@client = Google::APIClient.new
end
it 'should raise an error if making an empty batch request' do
batch = Google::APIClient::BatchRequest.new
(lambda do
@client.execute(batch)
end).should raise_error(Google::APIClient::BatchError)
end
describe 'with the discovery API' do
before do
@client.authorization = nil
@discovery = @client.discovered_api('discovery', 'v1')
end
describe 'with two valid requests' do
before do
@call1 = {
:api_method => @discovery.apis.get_rest,
:parameters => {
'api' => 'adsense',
'version' => 'v1'
}
}
@call2 = {
:api_method => @discovery.apis.get_rest,
:parameters => {
'api' => 'discovery',
'version' => 'v1'
}
}
end
it 'should execute both when using a global callback' do
block_called = 0
ids = ['first_call', 'second_call']
expected_ids = ids.clone
batch = Google::APIClient::BatchRequest.new do |result|
block_called += 1
result.status.should == 200
expected_ids.should include(result.response.call_id)
expected_ids.delete(result.response.call_id)
end
batch.add(@call1, ids[0])
batch.add(@call2, ids[1])
@client.execute(batch)
block_called.should == 2
end
it 'should execute both when using individual callbacks' do
batch = Google::APIClient::BatchRequest.new
call1_returned, call2_returned = false, false
batch.add(@call1) do |result|
call1_returned = true
result.status.should == 200
end
batch.add(@call2) do |result|
call2_returned = true
result.status.should == 200
end
@client.execute(batch)
call1_returned.should == true
call2_returned.should == true
end
it 'should raise an error if using the same call ID more than once' do
batch = Google::APIClient::BatchRequest.new
(lambda do
batch.add(@call1, 'my_id')
batch.add(@call2, 'my_id')
end).should raise_error(Google::APIClient::BatchError)
end
end
describe 'with a valid request and an invalid one' do
before do
@call1 = {
:api_method => @discovery.apis.get_rest,
:parameters => {
'api' => 'adsense',
'version' => 'v1'
}
}
@call2 = {
:api_method => @discovery.apis.get_rest,
:parameters => {
'api' => 0,
'version' => 1
}
}
end
it 'should execute both when using a global callback' do
block_called = 0
ids = ['first_call', 'second_call']
expected_ids = ids.clone
batch = Google::APIClient::BatchRequest.new do |result|
block_called += 1
expected_ids.should include(result.response.call_id)
expected_ids.delete(result.response.call_id)
if result.response.call_id == ids[0]
result.status.should == 200
else
result.status.should >= 400
result.status.should < 500
end
end
batch.add(@call1, ids[0])
batch.add(@call2, ids[1])
@client.execute(batch)
block_called.should == 2
end
it 'should execute both when using individual callbacks' do
batch = Google::APIClient::BatchRequest.new
call1_returned, call2_returned = false, false
batch.add(@call1) do |result|
call1_returned = true
result.status.should == 200
end
batch.add(@call2) do |result|
call2_returned = true
result.status.should >= 400
result.status.should < 500
end
@client.execute(batch)
call1_returned.should == true
call2_returned.should == true
end
end
end
describe 'with the calendar API' do
before do
@client.authorization = nil
@calendar = @client.discovered_api('calendar', 'v3')
end
describe 'with two valid requests' do
before do
event1 = {
'summary' => 'Appointment 1',
'location' => 'Somewhere',
'start' => {
'dateTime' => '2011-01-01T10:00:00.000-07:00'
},
'end' => {
'dateTime' => '2011-01-01T10:25:00.000-07:00'
},
'attendees' => [
{
'email' => 'myemail@mydomain.tld'
}
]
}
event2 = {
'summary' => 'Appointment 2',
'location' => 'Somewhere as well',
'start' => {
'dateTime' => '2011-01-02T10:00:00.000-07:00'
},
'end' => {
'dateTime' => '2011-01-02T10:25:00.000-07:00'
},
'attendees' => [
{
'email' => 'myemail@mydomain.tld'
}
]
}
@call1 = {
:api_method => @calendar.events.insert,
:parameters => {'calendarId' => 'myemail@mydomain.tld'},
:body => JSON.dump(event1),
:headers => {'Content-Type' => 'application/json'}
}
@call2 = {
:api_method => @calendar.events.insert,
:parameters => {'calendarId' => 'myemail@mydomain.tld'},
:body => JSON.dump(event2),
:headers => {'Content-Type' => 'application/json'}
}
end
it 'should convert to a correct HTTP request' do
batch = Google::APIClient::BatchRequest.new { |result| }
batch.add(@call1, '1').add(@call2, '2')
method, uri, headers, body = batch.to_http_request
boundary = Google::APIClient::BatchRequest::BATCH_BOUNDARY
method.to_s.downcase.should == 'post'
uri.to_s.should == 'https://www.googleapis.com/batch'
headers.should == {
"Content-Type"=>"multipart/mixed; boundary=#{boundary}"
}
expected_body = /--#{Regexp.escape(boundary)}\nContent-Type: +application\/http\nContent-ID: +<[\w-]+\+1>\n\nPOST +https:\/\/www.googleapis.com\/calendar\/v3\/calendars\/myemail@mydomain.tld\/events +HTTP\/1.1\nContent-Type: +application\/json\n\n#{Regexp.escape(@call1[:body])}\n\n--#{boundary}\nContent-Type: +application\/http\nContent-ID: +<[\w-]+\+2>\n\nPOST +https:\/\/www.googleapis.com\/calendar\/v3\/calendars\/myemail@mydomain.tld\/events HTTP\/1.1\nContent-Type: +application\/json\n\n#{Regexp.escape(@call2[:body])}\n\n--#{Regexp.escape(boundary)}--/
body.gsub("\r", "").should =~ expected_body
end
end
end
end
+40
View File
@@ -142,6 +142,12 @@ describe Google::APIClient do
).name.should == 'delete'
end
it 'should define the origin API in discovered methods' do
@client.discovered_method(
'prediction.training.insert', 'prediction', 'v1.2'
).api.name.should == 'prediction'
end
it 'should not find methods that are not in the discovery document' do
@client.discovered_method(
'prediction.bogus', 'prediction', 'v1.2'
@@ -165,6 +171,10 @@ describe Google::APIClient do
@client.preferred_version(:prediction).version.should_not == 'v1'
end
it 'should return a batch path' do
@client.discovered_api('prediction', 'v1.2').batch_path.should_not be_nil
end
it 'should generate valid requests' do
request = @client.generate_request(
:api_method => @prediction.training.insert,
@@ -324,6 +334,12 @@ describe Google::APIClient do
).name.should == 'list'
end
it 'should define the origin API in discovered methods' do
@client.discovered_method(
'plus.activities.list', 'plus'
).api.name.should == 'plus'
end
it 'should not find methods that are not in the discovery document' do
@client.discovered_method('plus.bogus', 'plus').should == nil
end
@@ -381,12 +397,22 @@ describe Google::APIClient do
@client.discovered_api('latitude').version.should == 'v1'
end
it 'should return a batch path' do
@client.discovered_api('latitude').batch_path.should_not be_nil
end
it 'should find methods that are in the discovery document' do
@client.discovered_method(
'latitude.currentLocation.get', 'latitude'
).name.should == 'get'
end
it 'should define the origin API in discovered methods' do
@client.discovered_method(
'latitude.currentLocation.get', 'latitude'
).api.name.should == 'latitude'
end
it 'should not find methods that are not in the discovery document' do
@client.discovered_method('latitude.bogus', 'latitude').should == nil
end
@@ -440,10 +466,20 @@ describe Google::APIClient do
).name.should == 'get'
end
it 'should define the origin API in discovered methods' do
@client.discovered_method(
'moderator.profiles.get', 'moderator'
).api.name.should == 'moderator'
end
it 'should not find methods that are not in the discovery document' do
@client.discovered_method('moderator.bogus', 'moderator').should == nil
end
it 'should return a batch path' do
@client.discovered_api('moderator').batch_path.should_not be_nil
end
it 'should generate requests against the correct URIs' do
request = @client.generate_request(
:api_method => 'moderator.profiles.get',
@@ -490,6 +526,10 @@ describe Google::APIClient do
@client.discovered_api('adsense').version.should == 'v1'
end
it 'should return a batch path' do
@client.discovered_api('adsense').batch_path.should_not be_nil
end
it 'should find methods that are in the discovery document' do
@client.discovered_method(
'adsense.reports.generate', 'adsense'
+92 -24
View File
@@ -49,34 +49,102 @@ describe Google::APIClient::Result do
'server' => 'GSE',
'connection' => 'close'
})
@response.stub(:body).and_return(
<<-END_OF_STRING
{
"kind": "plus#activityFeed",
"etag": "FOO",
"nextPageToken": "NEXT+PAGE+TOKEN",
"selfLink": "https://www.googleapis.com/plus/v1/people/foo/activities/public?",
"nextLink": "https://www.googleapis.com/plus/v1/people/foo/activities/public?maxResults=20&pageToken=NEXT%2BPAGE%2BTOKEN",
"title": "Plus Public Activity Feed for ",
"updated": "2012-04-23T00:00:00.000Z",
"id": "tag:google.com,2010:/plus/people/foo/activities/public",
"items": []
}
END_OF_STRING
)
@result = Google::APIClient::Result.new(@reference, @request, @response)
end
it 'should return the correct next page token' do
@result.next_page_token.should == 'NEXT+PAGE+TOKEN'
describe 'with a next page token' do
before do
@response.stub(:body).and_return(
<<-END_OF_STRING
{
"kind": "plus#activityFeed",
"etag": "FOO",
"nextPageToken": "NEXT+PAGE+TOKEN",
"selfLink": "https://www.googleapis.com/plus/v1/people/foo/activities/public?",
"nextLink": "https://www.googleapis.com/plus/v1/people/foo/activities/public?maxResults=20&pageToken=NEXT%2BPAGE%2BTOKEN",
"title": "Plus Public Activity Feed for ",
"updated": "2012-04-23T00:00:00.000Z",
"id": "tag:google.com,2010:/plus/people/foo/activities/public",
"items": []
}
END_OF_STRING
)
@result = Google::APIClient::Result.new(@reference, @request, @response)
end
it 'should return the correct next page token' do
@result.next_page_token.should == 'NEXT+PAGE+TOKEN'
end
it 'should escape the next page token when calling next_page' do
reference = @result.next_page
reference.parameters.should include('pageToken')
reference.parameters['pageToken'].should == 'NEXT+PAGE+TOKEN'
path = reference.to_request.path.to_s
path.should include 'pageToken=NEXT%2BPAGE%2BTOKEN'
end
it 'should return content type correctly' do
@result.media_type.should == 'application/json'
end
it 'should return the result data correctly' do
@result.data?.should be_true
@result.data.class.to_s.should ==
'Google::APIClient::Schema::Plus::V1::ActivityFeed'
@result.data.kind.should == 'plus#activityFeed'
@result.data.etag.should == 'FOO'
@result.data.nextPageToken.should == 'NEXT+PAGE+TOKEN'
@result.data.selfLink.should ==
'https://www.googleapis.com/plus/v1/people/foo/activities/public?'
@result.data.nextLink.should ==
'https://www.googleapis.com/plus/v1/people/foo/activities/public?' +
'maxResults=20&pageToken=NEXT%2BPAGE%2BTOKEN'
@result.data.title.should == 'Plus Public Activity Feed for '
@result.data.id.should ==
'tag:google.com,2010:/plus/people/foo/activities/public'
@result.data.items.should be_empty
end
end
it 'should escape the next page token when calling next_page' do
reference = @result.next_page
reference.parameters.should include('pageToken')
reference.parameters['pageToken'].should == 'NEXT+PAGE+TOKEN'
path = reference.to_request.path.to_s
path.should include 'pageToken=NEXT%2BPAGE%2BTOKEN'
describe 'without a next page token' do
before do
@response.stub(:body).and_return(
<<-END_OF_STRING
{
"kind": "plus#activityFeed",
"etag": "FOO",
"selfLink": "https://www.googleapis.com/plus/v1/people/foo/activities/public?",
"title": "Plus Public Activity Feed for ",
"updated": "2012-04-23T00:00:00.000Z",
"id": "tag:google.com,2010:/plus/people/foo/activities/public",
"items": []
}
END_OF_STRING
)
@result = Google::APIClient::Result.new(@reference, @request, @response)
end
it 'should not return a next page token' do
@result.next_page_token.should == nil
end
it 'should return content type correctly' do
@result.media_type.should == 'application/json'
end
it 'should return the result data correctly' do
@result.data?.should be_true
@result.data.class.to_s.should ==
'Google::APIClient::Schema::Plus::V1::ActivityFeed'
@result.data.kind.should == 'plus#activityFeed'
@result.data.etag.should == 'FOO'
@result.data.selfLink.should ==
'https://www.googleapis.com/plus/v1/people/foo/activities/public?'
@result.data.title.should == 'Plus Public Activity Feed for '
@result.data.id.should ==
'tag:google.com,2010:/plus/people/foo/activities/public'
@result.data.items.should be_empty
end
end
end
end