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
21 CLIENT ||= Google::APIClient.new
24 # Reset client to not-quite-pristine state
29 it 'should raise an error if making an empty batch request' do
30 batch = Google::APIClient::BatchRequest.new
34 end).should raise_error(Google::APIClient::BatchError)
37 describe 'with the discovery API' do
39 CLIENT.authorization = nil
40 @discovery = CLIENT.discovered_api('discovery', 'v1')
43 describe 'with two valid requests' do
46 :api_method => @discovery.apis.get_rest,
54 :api_method => @discovery.apis.get_rest,
62 it 'should execute both when using a global callback' do
64 ids = ['first_call', 'second_call']
65 expected_ids = ids.clone
66 batch = Google::APIClient::BatchRequest.new do |result|
68 result.status.should == 200
69 expected_ids.should include(result.response.call_id)
70 expected_ids.delete(result.response.call_id)
73 batch.add(@call1, ids[0])
74 batch.add(@call2, ids[1])
77 block_called.should == 2
80 it 'should execute both when using individual callbacks' do
81 batch = Google::APIClient::BatchRequest.new
83 call1_returned, call2_returned = false, false
84 batch.add(@call1) do |result|
86 result.status.should == 200
88 batch.add(@call2) do |result|
90 result.status.should == 200
94 call1_returned.should == true
95 call2_returned.should == true
98 it 'should raise an error if using the same call ID more than once' do
99 batch = Google::APIClient::BatchRequest.new
102 batch.add(@call1, 'my_id')
103 batch.add(@call2, 'my_id')
104 end).should raise_error(Google::APIClient::BatchError)
108 describe 'with a valid request and an invalid one' do
111 :api_method => @discovery.apis.get_rest,
119 :api_method => @discovery.apis.get_rest,
127 it 'should execute both when using a global callback' do
129 ids = ['first_call', 'second_call']
130 expected_ids = ids.clone
131 batch = Google::APIClient::BatchRequest.new do |result|
133 expected_ids.should include(result.response.call_id)
134 expected_ids.delete(result.response.call_id)
135 if result.response.call_id == ids[0]
136 result.status.should == 200
138 result.status.should >= 400
139 result.status.should < 500
143 batch.add(@call1, ids[0])
144 batch.add(@call2, ids[1])
146 CLIENT.execute(batch)
147 block_called.should == 2
150 it 'should execute both when using individual callbacks' do
151 batch = Google::APIClient::BatchRequest.new
153 call1_returned, call2_returned = false, false
154 batch.add(@call1) do |result|
155 call1_returned = true
156 result.status.should == 200
158 batch.add(@call2) do |result|
159 call2_returned = true
160 result.status.should >= 400
161 result.status.should < 500
164 CLIENT.execute(batch)
165 call1_returned.should == true
166 call2_returned.should == true
171 describe 'with the calendar API' do
173 CLIENT.authorization = nil
174 @calendar = CLIENT.discovered_api('calendar', 'v3')
177 describe 'with two valid requests' do
180 'summary' => 'Appointment 1',
181 'location' => 'Somewhere',
183 'dateTime' => '2011-01-01T10:00:00.000-07:00'
186 'dateTime' => '2011-01-01T10:25:00.000-07:00'
190 'email' => 'myemail@mydomain.tld'
196 'summary' => 'Appointment 2',
197 'location' => 'Somewhere as well',
199 'dateTime' => '2011-01-02T10:00:00.000-07:00'
202 'dateTime' => '2011-01-02T10:25:00.000-07:00'
206 'email' => 'myemail@mydomain.tld'
212 :api_method => @calendar.events.insert,
213 :parameters => {'calendarId' => 'myemail@mydomain.tld'},
214 :body => MultiJson.dump(event1),
215 :headers => {'Content-Type' => 'application/json'}
219 :api_method => @calendar.events.insert,
220 :parameters => {'calendarId' => 'myemail@mydomain.tld'},
221 :body => MultiJson.dump(event2),
222 :headers => {'Content-Type' => 'application/json'}
226 it 'should convert to a correct HTTP request' do
227 batch = Google::APIClient::BatchRequest.new { |result| }
228 batch.add(@call1, '1').add(@call2, '2')
229 method, uri, headers, body = batch.to_http_request
230 boundary = Google::APIClient::BatchRequest::BATCH_BOUNDARY
231 method.to_s.downcase.should == 'post'
232 uri.to_s.should == 'https://www.googleapis.com/batch'
234 "Content-Type"=>"multipart/mixed; boundary=#{boundary}"
236 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)}--/
237 body.gsub("\r", "").should =~ expected_body