Skip to content

Commit

Permalink
Merge pull request #737 from bshaffer/fix-batch-for-post
Browse files Browse the repository at this point in the history
fixes batch requests for POST
  • Loading branch information
bshaffer committed Nov 9, 2015
2 parents 02d6c48 + 06d1b98 commit 4116a71
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 25 deletions.
36 changes: 24 additions & 12 deletions src/Google/Http/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,38 @@ public function execute()
{
$body = '';
$classes = array();
$batchHttpTemplate = <<<EOF
--%s
Content-Type: application/http
Content-Transfer-Encoding: binary
MIME-Version: 1.0
Content-ID: %s
%s%s
%s
EOF;

/** @var Google_Http_Request $req */
foreach ($this->requests as $key => $request) {
$request->addHeaders(
[
'Content-Type' => 'application/http',
'Content-Transfer-Encoding' => 'binary',
'MIME-Version' => '1.0',
'Content-ID' => $key,
]
);
$body .= "--{$this->boundary}";
$body .= Request::getHeadersAsString($request) . "\n\n";
$body .= sprintf(
$firstLine = sprintf(
'%s %s HTTP/%s',
$request->getMethod(),
$request->getResource(),
$request->getProtocolVersion()
);
$body .= "\n\n";

$content = (string) $request->getBody();

$body .= sprintf(
$batchHttpTemplate,
$this->boundary,
$key,
$firstLine,
Request::getHeadersAsString($request),
$content ? "\n".$content : ''
);

$classes['response-' . $key] = $request->getHeader('X-Php-Expected-Class');
}
Expand Down
49 changes: 36 additions & 13 deletions tests/Google/Http/BatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,18 @@

class Google_Http_BatchTest extends BaseTest
{
public $plus;

public function testBatchRequestWithAuth()
{
$this->checkToken();

$client = $this->getClient();
$batch = new Google_Http_Batch($client);
$this->plus = new Google_Service_Plus($client);
$plus = new Google_Service_Plus($client);

$client->setUseBatch(true);
$batch->add($this->plus->people->get('me'), 'key1');
$batch->add($this->plus->people->get('me'), 'key2');
$batch->add($this->plus->people->get('me'), 'key3');
$batch->add($plus->people->get('me'), 'key1');
$batch->add($plus->people->get('me'), 'key2');
$batch->add($plus->people->get('me'), 'key3');

$result = $batch->execute();
$this->assertTrue(isset($result['response-key1']));
Expand All @@ -45,12 +43,37 @@ public function testBatchRequest()
{
$client = $this->getClient();
$batch = new Google_Http_Batch($client);
$this->plus = new Google_Service_Plus($client);
$plus = new Google_Service_Plus($client);

$client->setUseBatch(true);
$batch->add($plus->people->get('+LarryPage'), 'key1');
$batch->add($plus->people->get('+LarryPage'), 'key2');
$batch->add($plus->people->get('+LarryPage'), 'key3');

$result = $batch->execute();
$this->assertTrue(isset($result['response-key1']));
$this->assertTrue(isset($result['response-key2']));
$this->assertTrue(isset($result['response-key3']));
}

public function testBatchRequestWithPostBody()
{
$this->checkToken();

$client = $this->getClient();
$batch = new Google_Http_Batch($client);
$shortener = new Google_Service_Urlshortener($client);
$url1 = new Google_Service_Urlshortener_Url;
$url2 = new Google_Service_Urlshortener_Url;
$url3 = new Google_Service_Urlshortener_Url;
$url1->setLongUrl('http://brentertainment.com');
$url2->setLongUrl('http://morehazards.com');
$url3->setLongUrl('http://github.com/bshaffer');

$client->setUseBatch(true);
$batch->add($this->plus->people->get('+LarryPage'), 'key1');
$batch->add($this->plus->people->get('+LarryPage'), 'key2');
$batch->add($this->plus->people->get('+LarryPage'), 'key3');
$batch->add($shortener->url->insert($url1), 'key1');
$batch->add($shortener->url->insert($url2), 'key2');
$batch->add($shortener->url->insert($url3), 'key3');

$result = $batch->execute();
$this->assertTrue(isset($result['response-key1']));
Expand All @@ -62,11 +85,11 @@ public function testInvalidBatchRequest()
{
$client = $this->getClient();
$batch = new Google_Http_Batch($client);
$this->plus = new Google_Service_Plus($client);
$plus = new Google_Service_Plus($client);

$client->setUseBatch(true);
$batch->add($this->plus->people->get('123456789987654321'), 'key1');
$batch->add($this->plus->people->get('+LarryPage'), 'key2');
$batch->add($plus->people->get('123456789987654321'), 'key1');
$batch->add($plus->people->get('+LarryPage'), 'key2');

$result = $batch->execute();
$this->assertTrue(isset($result['response-key2']));
Expand Down

0 comments on commit 4116a71

Please sign in to comment.