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.
17 require 'google/api_client'
18 require 'google/api_client/version'
20 describe Google::APIClient::BatchRequest do
22 @client = Google::APIClient.new
25 it 'should raise an error if making an empty batch request' do
26 batch = Google::APIClient::BatchRequest.new
29 @client.execute(batch)
30 end).should raise_error(Google::APIClient::BatchError)
33 describe 'with the discovery API' do
35 @client.authorization = nil
36 @discovery = @client.discovered_api('discovery', 'v1')
39 describe 'with two valid requests' do
42 :api_method => @discovery.apis.get_rest,
50 :api_method => @discovery.apis.get_rest,
58 it 'should execute both when using a global callback' do
60 ids = ['first_call', 'second_call']
61 expected_ids = ids.clone
62 batch = Google::APIClient::BatchRequest.new do |result|
64 result.status.should == 200
65 expected_ids.should include(result.response.call_id)
66 expected_ids.delete(result.response.call_id)
69 batch.add(@call1, ids[0])
70 batch.add(@call2, ids[1])
72 @client.execute(batch)
73 block_called.should == 2
76 it 'should execute both when using individual callbacks' do
77 batch = Google::APIClient::BatchRequest.new
79 call1_returned, call2_returned = false, false
80 batch.add(@call1) do |result|
82 result.status.should == 200
84 batch.add(@call2) do |result|
86 result.status.should == 200
89 @client.execute(batch)
90 call1_returned.should == true
91 call2_returned.should == true
94 it 'should raise an error if using the same call ID more than once' do
95 batch = Google::APIClient::BatchRequest.new
98 batch.add(@call1, 'my_id')
99 batch.add(@call2, 'my_id')
100 end).should raise_error(Google::APIClient::BatchError)
104 describe 'with a valid request and an invalid one' do
107 :api_method => @discovery.apis.get_rest,
115 :api_method => @discovery.apis.get_rest,
123 it 'should execute both when using a global callback' do
125 ids = ['first_call', 'second_call']
126 expected_ids = ids.clone
127 batch = Google::APIClient::BatchRequest.new do |result|
129 expected_ids.should include(result.response.call_id)
130 expected_ids.delete(result.response.call_id)
131 if result.response.call_id == ids[0]
132 result.status.should == 200
134 result.status.should >= 400
135 result.status.should < 500
139 batch.add(@call1, ids[0])
140 batch.add(@call2, ids[1])
142 @client.execute(batch)
143 block_called.should == 2
146 it 'should execute both when using individual callbacks' do
147 batch = Google::APIClient::BatchRequest.new
149 call1_returned, call2_returned = false, false
150 batch.add(@call1) do |result|
151 call1_returned = true
152 result.status.should == 200
154 batch.add(@call2) do |result|
155 call2_returned = true
156 result.status.should >= 400
157 result.status.should < 500
160 @client.execute(batch)
161 call1_returned.should == true
162 call2_returned.should == true
167 describe 'with the calendar API' do
169 @client.authorization = nil
170 @calendar = @client.discovered_api('calendar', 'v3')
173 describe 'with two valid requests' do
176 'summary' => 'Appointment 1',
177 'location' => 'Somewhere',
179 'dateTime' => '2011-01-01T10:00:00.000-07:00'
182 'dateTime' => '2011-01-01T10:25:00.000-07:00'
186 'email' => 'myemail@mydomain.tld'
192 'summary' => 'Appointment 2',
193 'location' => 'Somewhere as well',
195 'dateTime' => '2011-01-02T10:00:00.000-07:00'
198 'dateTime' => '2011-01-02T10:25:00.000-07:00'
202 'email' => 'myemail@mydomain.tld'
208 :api_method => @calendar.events.insert,
209 :parameters => {'calendarId' => 'myemail@mydomain.tld'},
210 :body => MultiJson.dump(event1),
211 :headers => {'Content-Type' => 'application/json'}
215 :api_method => @calendar.events.insert,
216 :parameters => {'calendarId' => 'myemail@mydomain.tld'},
217 :body => MultiJson.dump(event2),
218 :headers => {'Content-Type' => 'application/json'}
222 it 'should convert to a correct HTTP request' do
223 batch = Google::APIClient::BatchRequest.new { |result| }
224 batch.add(@call1, '1').add(@call2, '2')
225 method, uri, headers, body = batch.to_http_request
226 boundary = Google::APIClient::BatchRequest::BATCH_BOUNDARY
227 method.to_s.downcase.should == 'post'
228 uri.to_s.should == 'https://www.googleapis.com/batch'
230 "Content-Type"=>"multipart/mixed; boundary=#{boundary}"
232 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)}--/
233 body.gsub("\r", "").should =~ expected_body