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 let(:client) { Google::APIClient.new }
23 it 'should raise an error if making an empty batch request' do
24 batch = Google::APIClient::BatchRequest.new
28 end).should raise_error(Google::APIClient::BatchError)
31 describe 'with the discovery API' do
33 client.authorization = nil
34 @discovery = client.discovered_api('discovery', 'v1')
37 describe 'with two valid requests' do
40 :api_method => @discovery.apis.get_rest,
48 :api_method => @discovery.apis.get_rest,
56 it 'should execute both when using a global callback' do
58 ids = ['first_call', 'second_call']
59 expected_ids = ids.clone
60 batch = Google::APIClient::BatchRequest.new do |result|
62 result.status.should == 200
63 expected_ids.should include(result.response.call_id)
64 expected_ids.delete(result.response.call_id)
67 batch.add(@call1, ids[0])
68 batch.add(@call2, ids[1])
71 block_called.should == 2
74 it 'should execute both when using individual callbacks' do
75 batch = Google::APIClient::BatchRequest.new
77 call1_returned, call2_returned = false, false
78 batch.add(@call1) do |result|
80 result.status.should == 200
82 batch.add(@call2) do |result|
84 result.status.should == 200
88 call1_returned.should == true
89 call2_returned.should == true
92 it 'should raise an error if using the same call ID more than once' do
93 batch = Google::APIClient::BatchRequest.new
96 batch.add(@call1, 'my_id')
97 batch.add(@call2, 'my_id')
98 end).should raise_error(Google::APIClient::BatchError)
102 describe 'with a valid request and an invalid one' do
105 :api_method => @discovery.apis.get_rest,
113 :api_method => @discovery.apis.get_rest,
121 it 'should execute both when using a global callback' do
123 ids = ['first_call', 'second_call']
124 expected_ids = ids.clone
125 batch = Google::APIClient::BatchRequest.new do |result|
127 expected_ids.should include(result.response.call_id)
128 expected_ids.delete(result.response.call_id)
129 if result.response.call_id == ids[0]
130 result.status.should == 200
132 result.status.should >= 400
133 result.status.should < 500
137 batch.add(@call1, ids[0])
138 batch.add(@call2, ids[1])
140 client.execute(batch)
141 block_called.should == 2
144 it 'should execute both when using individual callbacks' do
145 batch = Google::APIClient::BatchRequest.new
147 call1_returned, call2_returned = false, false
148 batch.add(@call1) do |result|
149 call1_returned = true
150 result.status.should == 200
152 batch.add(@call2) do |result|
153 call2_returned = true
154 result.status.should >= 400
155 result.status.should < 500
158 client.execute(batch)
159 call1_returned.should == true
160 call2_returned.should == true
165 describe 'with the calendar API' do
167 client.authorization = nil
168 @calendar = client.discovered_api('calendar', 'v3')
171 describe 'with two valid requests' do
174 'summary' => 'Appointment 1',
175 'location' => 'Somewhere',
177 'dateTime' => '2011-01-01T10:00:00.000-07:00'
180 'dateTime' => '2011-01-01T10:25:00.000-07:00'
184 'email' => 'myemail@mydomain.tld'
190 'summary' => 'Appointment 2',
191 'location' => 'Somewhere as well',
193 'dateTime' => '2011-01-02T10:00:00.000-07:00'
196 'dateTime' => '2011-01-02T10:25:00.000-07:00'
200 'email' => 'myemail@mydomain.tld'
206 :api_method => @calendar.events.insert,
207 :parameters => {'calendarId' => 'myemail@mydomain.tld'},
208 :body => MultiJson.dump(event1),
209 :headers => {'Content-Type' => 'application/json'}
213 :api_method => @calendar.events.insert,
214 :parameters => {'calendarId' => 'myemail@mydomain.tld'},
215 :body => MultiJson.dump(event2),
216 :headers => {'Content-Type' => 'application/json'}
220 it 'should convert to a correct HTTP request' do
221 batch = Google::APIClient::BatchRequest.new { |result| }
222 batch.add(@call1, '1').add(@call2, '2')
223 method, uri, headers, body = batch.to_http_request
224 boundary = Google::APIClient::BatchRequest::BATCH_BOUNDARY
225 method.to_s.downcase.should == 'post'
226 uri.to_s.should == 'https://www.googleapis.com/batch'
228 "Content-Type"=>"multipart/mixed; boundary=#{boundary}"
230 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)}--/
231 body.gsub("\r", "").should =~ expected_body