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'
17 require 'google/api_client/version'
19 describe Google::APIClient::BatchRequest do
20 CLIENT = Google::APIClient.new(:application_name => 'API Client Tests') unless defined?(CLIENT)
23 # Reset client to not-quite-pristine state
28 it 'should raise an error if making an empty batch request' do
29 batch = Google::APIClient::BatchRequest.new
33 end).to raise_error(Google::APIClient::BatchError)
36 it 'should allow query parameters in batch requests' do
37 batch = Google::APIClient::BatchRequest.new
38 batch.add(:uri => 'https://example.com', :parameters => {
41 method, uri, headers, body = batch.to_http_request
42 expect(body.read).to include("/?a=12345")
45 describe 'with the discovery API' do
47 CLIENT.authorization = nil
48 @discovery = CLIENT.discovered_api('discovery', 'v1')
51 describe 'with two valid requests' do
54 :api_method => @discovery.apis.get_rest,
62 :api_method => @discovery.apis.get_rest,
70 it 'should execute both when using a global callback' do
72 ids = ['first_call', 'second_call']
73 expected_ids = ids.clone
74 batch = Google::APIClient::BatchRequest.new do |result|
76 expect(result.status).to eq(200)
77 expect(expected_ids).to include(result.response.call_id)
78 expected_ids.delete(result.response.call_id)
81 batch.add(@call1, ids[0])
82 batch.add(@call2, ids[1])
85 expect(block_called).to eq(2)
88 it 'should execute both when using individual callbacks' do
89 batch = Google::APIClient::BatchRequest.new
91 call1_returned, call2_returned = false, false
92 batch.add(@call1) do |result|
94 expect(result.status).to eq(200)
96 batch.add(@call2) do |result|
98 expect(result.status).to eq(200)
101 CLIENT.execute(batch)
102 expect(call1_returned).to be_truthy
103 expect(call2_returned).to be_truthy
106 it 'should raise an error if using the same call ID more than once' do
107 batch = Google::APIClient::BatchRequest.new
110 batch.add(@call1, 'my_id')
111 batch.add(@call2, 'my_id')
112 end).to raise_error(Google::APIClient::BatchError)
116 describe 'with a valid request and an invalid one' do
119 :api_method => @discovery.apis.get_rest,
127 :api_method => @discovery.apis.get_rest,
135 it 'should execute both when using a global callback' do
137 ids = ['first_call', 'second_call']
138 expected_ids = ids.clone
139 batch = Google::APIClient::BatchRequest.new do |result|
141 expect(expected_ids).to include(result.response.call_id)
142 expected_ids.delete(result.response.call_id)
143 if result.response.call_id == ids[0]
144 expect(result.status).to eq(200)
146 expect(result.status).to be >= 400
147 expect(result.status).to be < 500
151 batch.add(@call1, ids[0])
152 batch.add(@call2, ids[1])
154 CLIENT.execute(batch)
155 expect(block_called).to eq(2)
158 it 'should execute both when using individual callbacks' do
159 batch = Google::APIClient::BatchRequest.new
161 call1_returned, call2_returned = false, false
162 batch.add(@call1) do |result|
163 call1_returned = true
164 expect(result.status).to eq(200)
166 batch.add(@call2) do |result|
167 call2_returned = true
168 expect(result.status).to be >= 400
169 expect(result.status).to be < 500
172 CLIENT.execute(batch)
173 expect(call1_returned).to be_truthy
174 expect(call2_returned).to be_truthy
179 describe 'with the calendar API' do
181 CLIENT.authorization = nil
182 @calendar = CLIENT.discovered_api('calendar', 'v3')
185 describe 'with two valid requests' do
188 'summary' => 'Appointment 1',
189 'location' => 'Somewhere',
191 'dateTime' => '2011-01-01T10:00:00.000-07:00'
194 'dateTime' => '2011-01-01T10:25:00.000-07:00'
198 'email' => 'myemail@mydomain.tld'
204 'summary' => 'Appointment 2',
205 'location' => 'Somewhere as well',
207 'dateTime' => '2011-01-02T10:00:00.000-07:00'
210 'dateTime' => '2011-01-02T10:25:00.000-07:00'
214 'email' => 'myemail@mydomain.tld'
220 :api_method => @calendar.events.insert,
221 :parameters => {'calendarId' => 'myemail@mydomain.tld'},
222 :body => MultiJson.dump(event1),
223 :headers => {'Content-Type' => 'application/json'}
227 :api_method => @calendar.events.insert,
228 :parameters => {'calendarId' => 'myemail@mydomain.tld'},
229 :body => MultiJson.dump(event2),
230 :headers => {'Content-Type' => 'application/json'}
234 it 'should convert to a correct HTTP request' do
235 batch = Google::APIClient::BatchRequest.new { |result| }
236 batch.add(@call1, '1').add(@call2, '2')
237 request = batch.to_env(CLIENT.connection)
238 boundary = Google::APIClient::BatchRequest::BATCH_BOUNDARY
239 expect(request[:method].to_s.downcase).to eq('post')
240 expect(request[:url].to_s).to eq('https://www.googleapis.com/batch')
241 expect(request[:request_headers]['Content-Type']).to eq("multipart/mixed;boundary=#{boundary}")
242 body = request[:body].read
243 expect(body).to include(@call1[:body])
244 expect(body).to include(@call2[:body])