| 
									
										
										
										
											2015-04-17 00:28:38 +00:00
										 |  |  | # Copyright 2015 Google Inc. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  | # you may not use this file except in compliance with the License. | 
					
						
							|  |  |  | # You may obtain a copy of the License at | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | #      http://www.apache.org/licenses/LICENSE-2.0 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  | # distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  | # See the License for the specific language governing permissions and | 
					
						
							|  |  |  | # limitations under the License. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require 'spec_helper' | 
					
						
							|  |  |  | require 'google/apis/core/batch' | 
					
						
							|  |  |  | require 'google/apis/core/json_representation' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | RSpec.describe Google::Apis::Core::BatchCommand do | 
					
						
							|  |  |  |   include TestHelpers | 
					
						
							|  |  |  |   include_context 'HTTP client' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let(:command) do | 
					
						
							|  |  |  |     command = Google::Apis::Core::BatchCommand.new(:post, 'https://www.googleapis.com/batch') | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let(:get_command) { Google::Apis::Core::HttpCommand.new(:get, 'https://www.googleapis.com/zoo/animals/1') } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let(:post_with_string_command) do | 
					
						
							|  |  |  |     command = Google::Apis::Core::HttpCommand.new(:post, 'https://www.googleapis.com/zoo/animals/2') | 
					
						
							|  |  |  |     command.body = 'Hello world' | 
					
						
							| 
									
										
										
										
											2016-08-17 20:51:09 +00:00
										 |  |  |     command.header['Content-Type'] = 'text/plain' | 
					
						
							| 
									
										
										
										
											2015-04-17 00:28:38 +00:00
										 |  |  |     command | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let(:post_with_io_command) do | 
					
						
							|  |  |  |     command = Google::Apis::Core::HttpCommand.new(:post, 'https://www.googleapis.com/zoo/animals/3') | 
					
						
							|  |  |  |     command.body = StringIO.new('Goodbye!') | 
					
						
							| 
									
										
										
										
											2016-08-17 20:51:09 +00:00
										 |  |  |     command.header['Content-Type'] = 'text/plain' | 
					
						
							| 
									
										
										
										
											2015-04-17 00:28:38 +00:00
										 |  |  |     command | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   before(:example) do | 
					
						
							| 
									
										
										
										
											2015-10-19 22:36:24 +00:00
										 |  |  |     allow(SecureRandom).to receive(:uuid).and_return('ffe23d1b-e8f7-47f5-8c01-2a30cf8ecb8f') | 
					
						
							| 
									
										
										
										
											2016-08-17 20:51:09 +00:00
										 |  |  |     allow(Digest::SHA1).to receive(:hexdigest).and_return('123abc') | 
					
						
							| 
									
										
										
										
											2015-10-19 22:36:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-17 00:28:38 +00:00
										 |  |  |     response = <<EOF
 | 
					
						
							|  |  |  | --batch123 | 
					
						
							|  |  |  | Content-Type: application/http | 
					
						
							| 
									
										
										
										
											2015-10-19 22:36:24 +00:00
										 |  |  | Content-ID: <response-ffe23d1b-e8f7-47f5-8c01-2a30cf8ecb8f+0> | 
					
						
							| 
									
										
										
										
											2015-04-17 00:28:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | HTTP/1.1 200 OK | 
					
						
							|  |  |  | Content-Type: text/plain; charset=UTF-8
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Hello | 
					
						
							|  |  |  | --batch123 | 
					
						
							|  |  |  | Content-Type: application/http | 
					
						
							| 
									
										
										
										
											2015-10-19 22:36:24 +00:00
										 |  |  | Content-ID: <response-ffe23d1b-e8f7-47f5-8c01-2a30cf8ecb8f+2> | 
					
						
							| 
									
										
										
										
											2015-04-17 00:28:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-19 22:36:24 +00:00
										 |  |  | HTTP/1.1 500 Server Error | 
					
						
							| 
									
										
										
										
											2015-04-17 00:28:38 +00:00
										 |  |  | Content-Type: text/plain; charset=UTF-8
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-19 22:36:24 +00:00
										 |  |  | Error! | 
					
						
							| 
									
										
										
										
											2015-04-17 00:28:38 +00:00
										 |  |  | --batch123 | 
					
						
							|  |  |  | Content-Type: application/http | 
					
						
							| 
									
										
										
										
											2015-10-19 22:36:24 +00:00
										 |  |  | Content-ID: <response-ffe23d1b-e8f7-47f5-8c01-2a30cf8ecb8f+1> | 
					
						
							| 
									
										
										
										
											2015-04-17 00:28:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-19 22:36:24 +00:00
										 |  |  | HTTP/1.1 200 OK | 
					
						
							| 
									
										
										
										
											2015-04-17 00:28:38 +00:00
										 |  |  | Content-Type: text/plain; charset=UTF-8
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-19 22:36:24 +00:00
										 |  |  | world | 
					
						
							| 
									
										
										
										
											2015-04-17 00:28:38 +00:00
										 |  |  | --batch123-- | 
					
						
							|  |  |  | EOF | 
					
						
							|  |  |  |     stub_request(:post, 'https://www.googleapis.com/batch') | 
					
						
							|  |  |  |       .to_return(headers: { 'Content-Type' => 'multipart/mixed; boundary=batch123' }, body: response) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it 'should send content' do | 
					
						
							|  |  |  |     b = ->(_res, _err) {} | 
					
						
							|  |  |  |     command.add(get_command, &b) | 
					
						
							|  |  |  |     command.add(post_with_string_command, &b) | 
					
						
							|  |  |  |     command.add(post_with_io_command, &b) | 
					
						
							|  |  |  |     command.execute(client) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expected_body = <<EOF.gsub(/\n/, "\r\n")
 | 
					
						
							| 
									
										
										
										
											2016-08-17 20:51:09 +00:00
										 |  |  | --123abc | 
					
						
							| 
									
										
										
										
											2015-04-17 00:28:38 +00:00
										 |  |  | Content-Type: application/http | 
					
						
							| 
									
										
										
										
											2016-08-17 20:51:09 +00:00
										 |  |  | Content-Id: <ffe23d1b-e8f7-47f5-8c01-2a30cf8ecb8f+0> | 
					
						
							|  |  |  | Content-Length: 58
 | 
					
						
							| 
									
										
										
										
											2015-04-17 00:28:38 +00:00
										 |  |  | Content-Transfer-Encoding: binary | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GET /zoo/animals/1? HTTP/1.1
 | 
					
						
							|  |  |  | Host: www.googleapis.com | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-17 20:51:09 +00:00
										 |  |  | --123abc | 
					
						
							| 
									
										
										
										
											2015-04-17 00:28:38 +00:00
										 |  |  | Content-Type: application/http | 
					
						
							| 
									
										
										
										
											2016-08-17 20:51:09 +00:00
										 |  |  | Content-Id: <ffe23d1b-e8f7-47f5-8c01-2a30cf8ecb8f+1> | 
					
						
							|  |  |  | Content-Length: 96
 | 
					
						
							| 
									
										
										
										
											2015-04-17 00:28:38 +00:00
										 |  |  | Content-Transfer-Encoding: binary | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | POST /zoo/animals/2? HTTP/1.1
 | 
					
						
							|  |  |  | Content-Type: text/plain | 
					
						
							|  |  |  | Host: www.googleapis.com | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Hello world | 
					
						
							| 
									
										
										
										
											2016-08-17 20:51:09 +00:00
										 |  |  | --123abc | 
					
						
							| 
									
										
										
										
											2015-04-17 00:28:38 +00:00
										 |  |  | Content-Type: application/http | 
					
						
							| 
									
										
										
										
											2016-08-17 20:51:09 +00:00
										 |  |  | Content-Id: <ffe23d1b-e8f7-47f5-8c01-2a30cf8ecb8f+2> | 
					
						
							|  |  |  | Content-Length: 93
 | 
					
						
							| 
									
										
										
										
											2015-04-17 00:28:38 +00:00
										 |  |  | Content-Transfer-Encoding: binary | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | POST /zoo/animals/3? HTTP/1.1
 | 
					
						
							|  |  |  | Content-Type: text/plain | 
					
						
							|  |  |  | Host: www.googleapis.com | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Goodbye! | 
					
						
							| 
									
										
										
										
											2016-08-17 20:51:09 +00:00
										 |  |  | --123abc-- | 
					
						
							| 
									
										
										
										
											2015-04-17 00:28:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | EOF | 
					
						
							|  |  |  |     expect(a_request(:post, 'https://www.googleapis.com/batch').with(body: expected_body)).to have_been_made | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it 'should send decode responses' do | 
					
						
							|  |  |  |     expect do |b| | 
					
						
							| 
									
										
										
										
											2015-10-19 22:36:24 +00:00
										 |  |  |       command.add(get_command) do |res, err| | 
					
						
							|  |  |  |         b.to_proc.call(1, res, err) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       command.add(post_with_string_command) do |res, err| | 
					
						
							|  |  |  |         b.to_proc.call(2, res, err) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       command.add(post_with_io_command) do |res, err| | 
					
						
							|  |  |  |         b.to_proc.call(3, res, err) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2015-04-17 00:28:38 +00:00
										 |  |  |       command.execute(client) | 
					
						
							| 
									
										
										
										
											2015-10-19 22:36:24 +00:00
										 |  |  |     end.to yield_successive_args([1, 'Hello', nil], [3, nil, an_instance_of(Google::Apis::ServerError)], [2, 'world', nil],) | 
					
						
							| 
									
										
										
										
											2015-04-17 00:28:38 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it 'should raise error if batch is empty' do | 
					
						
							|  |  |  |     expect { command.execute(client) }.to raise_error(Google::Apis::BatchError) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |