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).should raise_error(Google::APIClient::BatchError)
36 describe 'with the discovery API' do
38 CLIENT.authorization = nil
39 @discovery = CLIENT.discovered_api('discovery', 'v1')
42 describe 'with two valid requests' do
45 :api_method => @discovery.apis.get_rest,
53 :api_method => @discovery.apis.get_rest,
61 it 'should execute both when using a global callback' do
63 ids = ['first_call', 'second_call']
64 expected_ids = ids.clone
65 batch = Google::APIClient::BatchRequest.new do |result|
67 result.status.should == 200
68 expected_ids.should include(result.response.call_id)
69 expected_ids.delete(result.response.call_id)
72 batch.add(@call1, ids[0])
73 batch.add(@call2, ids[1])
76 block_called.should == 2
79 it 'should execute both when using individual callbacks' do
80 batch = Google::APIClient::BatchRequest.new
82 call1_returned, call2_returned = false, false
83 batch.add(@call1) do |result|
85 result.status.should == 200
87 batch.add(@call2) do |result|
89 result.status.should == 200
93 call1_returned.should == true
94 call2_returned.should == true
97 it 'should raise an error if using the same call ID more than once' do
98 batch = Google::APIClient::BatchRequest.new
101 batch.add(@call1, 'my_id')
102 batch.add(@call2, 'my_id')
103 end).should raise_error(Google::APIClient::BatchError)
107 describe 'with a valid request and an invalid one' do
110 :api_method => @discovery.apis.get_rest,
118 :api_method => @discovery.apis.get_rest,
126 it 'should execute both when using a global callback' do
128 ids = ['first_call', 'second_call']
129 expected_ids = ids.clone
130 batch = Google::APIClient::BatchRequest.new do |result|
132 expected_ids.should include(result.response.call_id)
133 expected_ids.delete(result.response.call_id)
134 if result.response.call_id == ids[0]
135 result.status.should == 200
137 result.status.should >= 400
138 result.status.should < 500
142 batch.add(@call1, ids[0])
143 batch.add(@call2, ids[1])
145 CLIENT.execute(batch)
146 block_called.should == 2
149 it 'should execute both when using individual callbacks' do
150 batch = Google::APIClient::BatchRequest.new
152 call1_returned, call2_returned = false, false
153 batch.add(@call1) do |result|
154 call1_returned = true
155 result.status.should == 200
157 batch.add(@call2) do |result|
158 call2_returned = true
159 result.status.should >= 400
160 result.status.should < 500
163 CLIENT.execute(batch)
164 call1_returned.should == true
165 call2_returned.should == true
170 describe 'with the calendar API' do
172 CLIENT.authorization = nil
173 @calendar = CLIENT.discovered_api('calendar', 'v3')
176 describe 'with two valid requests' do
179 'summary' => 'Appointment 1',
180 'location' => 'Somewhere',
182 'dateTime' => '2011-01-01T10:00:00.000-07:00'
185 'dateTime' => '2011-01-01T10:25:00.000-07:00'
189 'email' => 'myemail@mydomain.tld'
195 'summary' => 'Appointment 2',
196 'location' => 'Somewhere as well',
198 'dateTime' => '2011-01-02T10:00:00.000-07:00'
201 'dateTime' => '2011-01-02T10:25:00.000-07:00'
205 'email' => 'myemail@mydomain.tld'
211 :api_method => @calendar.events.insert,
212 :parameters => {'calendarId' => 'myemail@mydomain.tld'},
213 :body => MultiJson.dump(event1),
214 :headers => {'Content-Type' => 'application/json'}
218 :api_method => @calendar.events.insert,
219 :parameters => {'calendarId' => 'myemail@mydomain.tld'},
220 :body => MultiJson.dump(event2),
221 :headers => {'Content-Type' => 'application/json'}
225 it 'should convert to a correct HTTP request' do
226 batch = Google::APIClient::BatchRequest.new { |result| }
227 batch.add(@call1, '1').add(@call2, '2')
228 request = batch.to_env(Faraday.default_connection)
229 boundary = Google::APIClient::BatchRequest::BATCH_BOUNDARY
230 request[:method].to_s.downcase.should == 'post'
231 request[:url].to_s.should == 'https://www.googleapis.com/batch'
232 request[:request_headers]['Content-Type'].should == "multipart/mixed;boundary=#{boundary}"
234 #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)}--/
235 #request[:body].read.gsub("\r", "").should =~ expected_body