1 # Copyright 2012 Google Inc.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 require 'google/api_client'
18 RSpec.describe Google::APIClient::BatchRequest do
19 CLIENT = Google::APIClient.new(:application_name => 'API Client Tests') unless defined?(CLIENT)
22 # Reset client to not-quite-pristine state
27 it 'should raise an error if making an empty batch request' do
28 batch = Google::APIClient::BatchRequest.new
32 end).to raise_error(Google::APIClient::BatchError)
35 it 'should allow query parameters in batch requests' do
36 batch = Google::APIClient::BatchRequest.new
37 batch.add(:uri => 'https://example.com', :parameters => {
40 method, uri, headers, body = batch.to_http_request
41 expect(body.read).to include("/?a=12345")
44 describe 'with the discovery API' do
46 CLIENT.authorization = nil
47 @discovery = CLIENT.discovered_api('discovery', 'v1')
50 describe 'with two valid requests' do
53 :api_method => @discovery.apis.get_rest,
61 :api_method => @discovery.apis.get_rest,
69 it 'should execute both when using a global callback' do
71 ids = ['first_call', 'second_call']
72 expected_ids = ids.clone
73 batch = Google::APIClient::BatchRequest.new do |result|
75 expect(result.status).to eq(200)
76 expect(expected_ids).to include(result.response.call_id)
77 expected_ids.delete(result.response.call_id)
80 batch.add(@call1, ids[0])
81 batch.add(@call2, ids[1])
84 expect(block_called).to eq(2)
87 it 'should execute both when using individual callbacks' do
88 batch = Google::APIClient::BatchRequest.new
90 call1_returned, call2_returned = false, false
91 batch.add(@call1) do |result|
93 expect(result.status).to eq(200)
95 batch.add(@call2) do |result|
97 expect(result.status).to eq(200)
100 CLIENT.execute(batch)
101 expect(call1_returned).to be_truthy
102 expect(call2_returned).to be_truthy
105 it 'should raise an error if using the same call ID more than once' do
106 batch = Google::APIClient::BatchRequest.new
109 batch.add(@call1, 'my_id')
110 batch.add(@call2, 'my_id')
111 end).to raise_error(Google::APIClient::BatchError)
115 describe 'with a valid request and an invalid one' do
118 :api_method => @discovery.apis.get_rest,
126 :api_method => @discovery.apis.get_rest,
134 it 'should execute both when using a global callback' do
136 ids = ['first_call', 'second_call']
137 expected_ids = ids.clone
138 batch = Google::APIClient::BatchRequest.new do |result|
140 expect(expected_ids).to include(result.response.call_id)
141 expected_ids.delete(result.response.call_id)
142 if result.response.call_id == ids[0]
143 expect(result.status).to eq(200)
145 expect(result.status).to be >= 400
146 expect(result.status).to be < 500
150 batch.add(@call1, ids[0])
151 batch.add(@call2, ids[1])
153 CLIENT.execute(batch)
154 expect(block_called).to eq(2)
157 it 'should execute both when using individual callbacks' do
158 batch = Google::APIClient::BatchRequest.new
160 call1_returned, call2_returned = false, false
161 batch.add(@call1) do |result|
162 call1_returned = true
163 expect(result.status).to eq(200)
165 batch.add(@call2) do |result|
166 call2_returned = true
167 expect(result.status).to be >= 400
168 expect(result.status).to be < 500
171 CLIENT.execute(batch)
172 expect(call1_returned).to be_truthy
173 expect(call2_returned).to be_truthy
178 describe 'with the calendar API' do
180 CLIENT.authorization = nil
181 @calendar = CLIENT.discovered_api('calendar', 'v3')
184 describe 'with two valid requests' do
187 'summary' => 'Appointment 1',
188 'location' => 'Somewhere',
190 'dateTime' => '2011-01-01T10:00:00.000-07:00'
193 'dateTime' => '2011-01-01T10:25:00.000-07:00'
197 'email' => 'myemail@mydomain.tld'
203 'summary' => 'Appointment 2',
204 'location' => 'Somewhere as well',
206 'dateTime' => '2011-01-02T10:00:00.000-07:00'
209 'dateTime' => '2011-01-02T10:25:00.000-07:00'
213 'email' => 'myemail@mydomain.tld'
219 :api_method => @calendar.events.insert,
220 :parameters => {'calendarId' => 'myemail@mydomain.tld'},
221 :body => MultiJson.dump(event1),
222 :headers => {'Content-Type' => 'application/json'}
226 :api_method => @calendar.events.insert,
227 :parameters => {'calendarId' => 'myemail@mydomain.tld'},
228 :body => MultiJson.dump(event2),
229 :headers => {'Content-Type' => 'application/json'}
233 it 'should convert to a correct HTTP request' do
234 batch = Google::APIClient::BatchRequest.new { |result| }
235 batch.add(@call1, '1').add(@call2, '2')
236 request = batch.to_env(CLIENT.connection)
237 boundary = Google::APIClient::BatchRequest::BATCH_BOUNDARY
238 expect(request[:method].to_s.downcase).to eq('post')
239 expect(request[:url].to_s).to eq('https://www.googleapis.com/batch')
240 expect(request[:request_headers]['Content-Type']).to eq("multipart/mixed;boundary=#{boundary}")
241 body = request[:body].read
242 expect(body).to include(@call1[:body])
243 expect(body).to include(@call2[:body])