Additional fix for broken ||= constant assignment
[arvados.git] / spec / google / api_client / batch_spec.rb
1 # Copyright 2012 Google Inc.
2 #
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
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 require 'spec_helper'
16 require 'google/api_client'
17 require 'google/api_client/version'
18
19 describe Google::APIClient::BatchRequest do
20   CLIENT = Google::APIClient.new unless defined?(CLIENT)
21
22   after do
23     # Reset client to not-quite-pristine state
24     CLIENT.key = nil
25     CLIENT.user_ip = nil
26   end
27
28   it 'should raise an error if making an empty batch request' do
29     batch = Google::APIClient::BatchRequest.new
30
31     (lambda do
32       CLIENT.execute(batch)
33     end).should raise_error(Google::APIClient::BatchError)
34   end
35
36   describe 'with the discovery API' do
37     before do
38       CLIENT.authorization = nil
39       @discovery = CLIENT.discovered_api('discovery', 'v1')
40     end
41
42     describe 'with two valid requests' do
43       before do
44         @call1 = {
45           :api_method => @discovery.apis.get_rest,
46           :parameters => {
47             'api' => 'adsense',
48             'version' => 'v1'
49           }
50         }
51
52         @call2 = {
53           :api_method => @discovery.apis.get_rest,
54           :parameters => {
55             'api' => 'discovery',
56             'version' => 'v1'
57           }
58         }
59       end
60
61       it 'should execute both when using a global callback' do
62         block_called = 0
63         ids = ['first_call', 'second_call']
64         expected_ids = ids.clone
65         batch = Google::APIClient::BatchRequest.new do |result|
66           block_called += 1
67           result.status.should == 200
68           expected_ids.should include(result.response.call_id)
69           expected_ids.delete(result.response.call_id)
70         end
71
72         batch.add(@call1, ids[0])
73         batch.add(@call2, ids[1])
74
75         CLIENT.execute(batch)
76         block_called.should == 2
77       end
78
79       it 'should execute both when using individual callbacks' do
80         batch = Google::APIClient::BatchRequest.new
81
82         call1_returned, call2_returned = false, false
83         batch.add(@call1) do |result|
84           call1_returned = true
85           result.status.should == 200
86         end
87         batch.add(@call2) do |result|
88           call2_returned = true
89           result.status.should == 200
90         end
91
92         CLIENT.execute(batch)
93         call1_returned.should == true
94         call2_returned.should == true
95       end
96
97       it 'should raise an error if using the same call ID more than once' do
98         batch = Google::APIClient::BatchRequest.new
99
100         (lambda do
101           batch.add(@call1, 'my_id')
102           batch.add(@call2, 'my_id')
103         end).should raise_error(Google::APIClient::BatchError)
104       end
105     end
106
107     describe 'with a valid request and an invalid one' do
108       before do
109         @call1 = {
110           :api_method => @discovery.apis.get_rest,
111           :parameters => {
112             'api' => 'adsense',
113             'version' => 'v1'
114           }
115         }
116
117         @call2 = {
118           :api_method => @discovery.apis.get_rest,
119           :parameters => {
120             'api' => 0,
121             'version' => 1
122           }
123         }
124       end
125
126       it 'should execute both when using a global callback' do
127         block_called = 0
128         ids = ['first_call', 'second_call']
129         expected_ids = ids.clone
130         batch = Google::APIClient::BatchRequest.new do |result|
131           block_called += 1
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
136           else
137             result.status.should >= 400
138             result.status.should < 500
139           end
140         end
141
142         batch.add(@call1, ids[0])
143         batch.add(@call2, ids[1])
144
145         CLIENT.execute(batch)
146         block_called.should == 2
147       end
148
149       it 'should execute both when using individual callbacks' do
150         batch = Google::APIClient::BatchRequest.new
151
152         call1_returned, call2_returned = false, false
153         batch.add(@call1) do |result|
154           call1_returned = true
155           result.status.should == 200
156         end
157         batch.add(@call2) do |result|
158           call2_returned = true
159           result.status.should >= 400
160           result.status.should < 500
161         end
162
163         CLIENT.execute(batch)
164         call1_returned.should == true
165         call2_returned.should == true
166       end
167     end
168   end
169
170   describe 'with the calendar API' do
171     before do
172       CLIENT.authorization = nil
173       @calendar = CLIENT.discovered_api('calendar', 'v3')
174     end
175
176     describe 'with two valid requests' do
177       before do
178         event1 = {
179           'summary' => 'Appointment 1',
180           'location' => 'Somewhere',
181           'start' => {
182             'dateTime' => '2011-01-01T10:00:00.000-07:00'
183           },
184           'end' => {
185             'dateTime' => '2011-01-01T10:25:00.000-07:00'
186           },
187           'attendees' => [
188             {
189               'email' => 'myemail@mydomain.tld'
190             }
191           ]
192         }
193
194         event2 = {
195           'summary' => 'Appointment 2',
196           'location' => 'Somewhere as well',
197           'start' => {
198             'dateTime' => '2011-01-02T10:00:00.000-07:00'
199           },
200           'end' => {
201             'dateTime' => '2011-01-02T10:25:00.000-07:00'
202           },
203           'attendees' => [
204             {
205               'email' => 'myemail@mydomain.tld'
206             }
207           ]
208         }
209
210         @call1 = {
211           :api_method => @calendar.events.insert,
212           :parameters => {'calendarId' => 'myemail@mydomain.tld'},
213           :body => MultiJson.dump(event1),
214           :headers => {'Content-Type' => 'application/json'}
215         }
216
217         @call2 = {
218           :api_method => @calendar.events.insert,
219           :parameters => {'calendarId' => 'myemail@mydomain.tld'},
220           :body => MultiJson.dump(event2),
221           :headers => {'Content-Type' => 'application/json'}
222         }
223       end
224
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}"
233         # TODO - Fix headers
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
236       end
237     end
238     
239   end
240 end