Skip to content

Commit 872d016

Browse files
committed
Add separate createBatch method to propagate batch URLs properly on a per-service basis. Fix newline stripping in batching
1 parent c27df10 commit 872d016

File tree

4 files changed

+148
-7
lines changed

4 files changed

+148
-7
lines changed

src/Google/Http/Batch.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,15 @@ class Google_Http_Batch
3535

3636
private $expected_classes = array();
3737

38-
private $base_path;
38+
private $root_url;
3939

40-
public function __construct(Google_Client $client, $boundary = false)
40+
private $batch_path;
41+
42+
public function __construct(Google_Client $client, $boundary = false, $rootUrl = '', $batchPath = '')
4143
{
4244
$this->client = $client;
43-
$this->base_path = $this->client->getBasePath();
45+
$this->root_url = rtrim($rootUrl ? $rootUrl : $this->client->getBasePath(), '/');
46+
$this->batch_path = $batchPath ? $batchPath : 'batch';
4447
$this->expected_classes = array();
4548
$boundary = (false == $boundary) ? mt_rand() : $boundary;
4649
$this->boundary = str_replace('"', '', $boundary);
@@ -62,14 +65,13 @@ public function execute()
6265
/** @var Google_Http_Request $req */
6366
foreach ($this->requests as $key => $req) {
6467
$body .= "--{$this->boundary}\n";
65-
$body .= $req->toBatchString($key) . "\n";
68+
$body .= $req->toBatchString($key) . "\n\n";
6669
$this->expected_classes["response-" . $key] = $req->getExpectedClass();
6770
}
6871

69-
$body = rtrim($body);
70-
$body .= "\n--{$this->boundary}--";
72+
$body .= "--{$this->boundary}--";
7173

72-
$url = $this->base_path . '/batch';
74+
$url = $this->root_url . '/' . $this->batch_path;
7375
$httpRequest = new Google_Http_Request($url, 'POST');
7476
$httpRequest->setRequestHeaders(
7577
array('Content-Type' => 'multipart/mixed; boundary=' . $this->boundary)

src/Google/Service.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
class Google_Service
1919
{
20+
public $batchPath;
2021
public $rootUrl;
2122
public $version;
2223
public $servicePath;
@@ -37,4 +38,20 @@ public function getClient()
3738
{
3839
return $this->client;
3940
}
41+
42+
/**
43+
* Create a new HTTP Batch handler for this service
44+
*
45+
* @return Google_Http_Batch
46+
*/
47+
public function createBatch()
48+
{
49+
return new Google_Http_Batch(
50+
$this->client,
51+
false,
52+
$this->rootUrl,
53+
$this->batchPath
54+
);
55+
}
56+
4057
}

tests/general/Http/BatchTest.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
/*
3+
* Copyright 2011 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
class BatchTest extends PHPUnit_Framework_TestCase
19+
{
20+
21+
public function setUp()
22+
{
23+
$this->client = $this->getMockBuilder("Google_Client")
24+
->disableOriginalConstructor()
25+
->getMock();
26+
}
27+
28+
public function testBasicFunctionality()
29+
{
30+
$this->client->expects($this->once())
31+
->method("getBasePath")
32+
->will($this->returnValue("base_path"));
33+
$batch = new Google_Http_Batch($this->client);
34+
$this->assertAttributeEquals("base_path", "root_url", $batch);
35+
$this->assertAttributeEquals("batch", "batch_path", $batch);
36+
}
37+
38+
public function testExtractionOfRootUrlFromService()
39+
{
40+
$this->client->expects($this->never())
41+
->method("getBasePath");
42+
$service = new Google_Service($this->client);
43+
$service->rootUrl = "root_url_dummy";
44+
$service->batchPath = "batch_path_dummy";
45+
$batch = $service->createBatch();
46+
$this->assertInstanceOf("Google_Http_Batch", $batch);
47+
$this->assertAttributeEquals("root_url_dummy", "root_url", $batch);
48+
$this->assertAttributeEquals("batch_path_dummy", "batch_path", $batch);
49+
}
50+
51+
public function testExecuteCustomRootUrlBatchPath()
52+
{
53+
$io = $this->getMockBuilder('Google_IO_Abstract')
54+
->disableOriginalConstructor()
55+
->setMethods(['makeRequest', 'needsQuirk', 'executeRequest', 'setOptions', 'setTimeout', 'getTimeout'])
56+
->getMock();
57+
$req = null;
58+
$io->expects($this->once())
59+
->method("makeRequest")
60+
->will($this->returnCallback(function($request) use (&$req) {
61+
$req = $request;
62+
return $request;
63+
}));
64+
$this->client->expects($this->once())
65+
->method("getIo")
66+
->will($this->returnValue($io));
67+
$batch = new Google_Http_Batch($this->client, false, 'https://www.example.com/', 'bat');
68+
$this->assertNull($batch->execute());
69+
$this->assertInstanceOf("Google_Http_Request", $req);
70+
$this->assertEquals("https://www.example.com/bat", $req->getUrl());
71+
}
72+
73+
public function testExecuteBodySerialization()
74+
{
75+
$io = $this->getMockBuilder('Google_IO_Abstract')
76+
->disableOriginalConstructor()
77+
->setMethods(['makeRequest', 'needsQuirk', 'executeRequest', 'setOptions', 'setTimeout', 'getTimeout'])
78+
->getMock();
79+
$req = null;
80+
$io->expects($this->once())
81+
->method("makeRequest")
82+
->will($this->returnCallback(function($request) use (&$req) {
83+
$req = $request;
84+
return $request;
85+
}));
86+
$this->client->expects($this->once())
87+
->method("getIo")
88+
->will($this->returnValue($io));
89+
$batch = new Google_Http_Batch($this->client, "BOUNDARY_TEXT", 'https://www.example.com/', 'bat');
90+
$req1 = new Google_Http_Request("https://www.example.com/req1");
91+
$req2 = new Google_Http_Request("https://www.example.com/req2", 'POST', array(), 'POSTBODY');
92+
$batch->add($req1, '1');
93+
$batch->add($req2, '2');
94+
$this->assertNull($batch->execute());
95+
$this->assertInstanceOf("Google_Http_Request", $req);
96+
$format = <<<'EOF'
97+
--BOUNDARY_TEXT
98+
Content-Type: application/http
99+
Content-Transfer-Encoding: binary
100+
MIME-Version: 1.0
101+
Content-ID: 1
102+
103+
GET /req1? HTTP/1.1
104+
105+
106+
--BOUNDARY_TEXT
107+
Content-Type: application/http
108+
Content-Transfer-Encoding: binary
109+
MIME-Version: 1.0
110+
Content-ID: 2
111+
112+
POST /req2? HTTP/1.1
113+
114+
POSTBODY
115+
116+
--BOUNDARY_TEXT--
117+
EOF;
118+
$this->assertEquals($format, $req->getPostBody());
119+
}
120+
121+
}

tests/general/ServiceTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function isAssociativeArray($array)
3333

3434
class ServiceTest extends PHPUnit_Framework_TestCase
3535
{
36+
3637
public function testModel()
3738
{
3839
$model = new TestModel();

0 commit comments

Comments
 (0)