2012-05-16 19:05:24 +00:00
|
|
|
# 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'
|
|
|
|
|
|
|
|
describe Google::APIClient::BatchRequest do
|
2012-12-30 19:27:45 +00:00
|
|
|
CLIENT = Google::APIClient.new(:application_name => 'API Client Tests') unless defined?(CLIENT)
|
2012-07-31 20:56:13 +00:00
|
|
|
|
|
|
|
after do
|
|
|
|
# Reset client to not-quite-pristine state
|
|
|
|
CLIENT.key = nil
|
|
|
|
CLIENT.user_ip = nil
|
|
|
|
end
|
2012-05-16 19:05:24 +00:00
|
|
|
|
|
|
|
it 'should raise an error if making an empty batch request' do
|
|
|
|
batch = Google::APIClient::BatchRequest.new
|
|
|
|
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(lambda do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.execute(batch)
|
2014-08-14 15:54:16 +00:00
|
|
|
end).to raise_error(Google::APIClient::BatchError)
|
2012-05-16 19:05:24 +00:00
|
|
|
end
|
|
|
|
|
2013-02-15 21:13:58 +00:00
|
|
|
it 'should allow query parameters in batch requests' do
|
|
|
|
batch = Google::APIClient::BatchRequest.new
|
|
|
|
batch.add(:uri => 'https://example.com', :parameters => {
|
|
|
|
'a' => '12345'
|
|
|
|
})
|
|
|
|
method, uri, headers, body = batch.to_http_request
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(body.read).to include("/?a=12345")
|
2013-02-15 21:13:58 +00:00
|
|
|
end
|
|
|
|
|
2012-05-16 19:05:24 +00:00
|
|
|
describe 'with the discovery API' do
|
|
|
|
before do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.authorization = nil
|
|
|
|
@discovery = CLIENT.discovered_api('discovery', 'v1')
|
2012-05-16 19:05:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'with two valid requests' do
|
|
|
|
before do
|
|
|
|
@call1 = {
|
|
|
|
:api_method => @discovery.apis.get_rest,
|
|
|
|
:parameters => {
|
2013-10-04 13:46:14 +00:00
|
|
|
'api' => 'plus',
|
2012-05-16 19:05:24 +00:00
|
|
|
'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
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(result.status).to eq(200)
|
|
|
|
expect(expected_ids).to include(result.response.call_id)
|
2012-05-16 19:05:24 +00:00
|
|
|
expected_ids.delete(result.response.call_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
batch.add(@call1, ids[0])
|
|
|
|
batch.add(@call2, ids[1])
|
|
|
|
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.execute(batch)
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(block_called).to eq(2)
|
2012-05-16 19:05:24 +00:00
|
|
|
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
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(result.status).to eq(200)
|
2012-05-16 19:05:24 +00:00
|
|
|
end
|
|
|
|
batch.add(@call2) do |result|
|
|
|
|
call2_returned = true
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(result.status).to eq(200)
|
2012-05-16 19:05:24 +00:00
|
|
|
end
|
|
|
|
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.execute(batch)
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(call1_returned).to be_truthy
|
|
|
|
expect(call2_returned).to be_truthy
|
2012-05-16 19:05:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should raise an error if using the same call ID more than once' do
|
|
|
|
batch = Google::APIClient::BatchRequest.new
|
|
|
|
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(lambda do
|
2012-05-16 19:05:24 +00:00
|
|
|
batch.add(@call1, 'my_id')
|
|
|
|
batch.add(@call2, 'my_id')
|
2014-08-14 15:54:16 +00:00
|
|
|
end).to raise_error(Google::APIClient::BatchError)
|
2012-05-16 19:05:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'with a valid request and an invalid one' do
|
|
|
|
before do
|
|
|
|
@call1 = {
|
|
|
|
:api_method => @discovery.apis.get_rest,
|
|
|
|
:parameters => {
|
2013-10-04 13:46:14 +00:00
|
|
|
'api' => 'plus',
|
2012-05-16 19:05:24 +00:00
|
|
|
'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
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(expected_ids).to include(result.response.call_id)
|
2012-05-16 19:05:24 +00:00
|
|
|
expected_ids.delete(result.response.call_id)
|
|
|
|
if result.response.call_id == ids[0]
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(result.status).to eq(200)
|
2012-05-16 19:05:24 +00:00
|
|
|
else
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(result.status).to be >= 400
|
|
|
|
expect(result.status).to be < 500
|
2012-05-16 19:05:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
batch.add(@call1, ids[0])
|
|
|
|
batch.add(@call2, ids[1])
|
|
|
|
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.execute(batch)
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(block_called).to eq(2)
|
2012-05-16 19:05:24 +00:00
|
|
|
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
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(result.status).to eq(200)
|
2012-05-16 19:05:24 +00:00
|
|
|
end
|
|
|
|
batch.add(@call2) do |result|
|
|
|
|
call2_returned = true
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(result.status).to be >= 400
|
|
|
|
expect(result.status).to be < 500
|
2012-05-16 19:05:24 +00:00
|
|
|
end
|
|
|
|
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.execute(batch)
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(call1_returned).to be_truthy
|
|
|
|
expect(call2_returned).to be_truthy
|
2012-05-16 19:05:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'with the calendar API' do
|
|
|
|
before do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.authorization = nil
|
|
|
|
@calendar = CLIENT.discovered_api('calendar', 'v3')
|
2012-05-16 19:05:24 +00:00
|
|
|
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'},
|
2012-07-21 10:50:48 +00:00
|
|
|
:body => MultiJson.dump(event1),
|
2012-05-16 19:05:24 +00:00
|
|
|
:headers => {'Content-Type' => 'application/json'}
|
|
|
|
}
|
|
|
|
|
|
|
|
@call2 = {
|
|
|
|
:api_method => @calendar.events.insert,
|
|
|
|
:parameters => {'calendarId' => 'myemail@mydomain.tld'},
|
2012-07-21 10:50:48 +00:00
|
|
|
:body => MultiJson.dump(event2),
|
2012-05-16 19:05:24 +00:00
|
|
|
: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')
|
2013-05-13 21:03:49 +00:00
|
|
|
request = batch.to_env(CLIENT.connection)
|
2012-05-16 19:05:24 +00:00
|
|
|
boundary = Google::APIClient::BatchRequest::BATCH_BOUNDARY
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(request[:method].to_s.downcase).to eq('post')
|
|
|
|
expect(request[:url].to_s).to eq('https://www.googleapis.com/batch')
|
|
|
|
expect(request[:request_headers]['Content-Type']).to eq("multipart/mixed;boundary=#{boundary}")
|
|
|
|
body = request[:body].read
|
|
|
|
expect(body).to include(@call1[:body])
|
|
|
|
expect(body).to include(@call2[:body])
|
2012-05-16 19:05:24 +00:00
|
|
|
end
|
|
|
|
end
|
2013-10-04 13:46:14 +00:00
|
|
|
|
2012-05-16 19:05:24 +00:00
|
|
|
end
|
|
|
|
end
|