Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Provide headers required for Firestore emulator. #2239

Merged
merged 1 commit into from
Aug 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions Firestore/src/Connection/Grpc.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ class Grpc implements ConnectionInterface
*/
private $resourcePrefixHeader;

/**
* @var bool
*/
private $isUsingEmulator = false;

/**
* @param array $config [optional]
*/
Expand Down Expand Up @@ -95,6 +100,7 @@ public function __construct(array $config = [])
//@codeCoverageIgnoreStart
$config += ['emulatorHost' => null];
if ((bool) $config['emulatorHost']) {
$this->isUsingEmulator = true;
$grpcConfig += $this->emulatorGapicConfig($config['emulatorHost']);
}
//@codeCoverageIgnoreEnd
Expand All @@ -115,7 +121,7 @@ public function batchGetDocuments(array $args)
return $this->send([$this->firestore, 'batchGetDocuments'], [
$this->pluck('database', $args),
$this->pluck('documents', $args),
$this->addResourcePrefixHeader($args)
$this->addRequestHeaders($args)
]);
}

Expand All @@ -135,7 +141,7 @@ public function beginTransaction(array $args)

return $this->send([$this->firestore, 'beginTransaction'], [
$this->pluck('database', $args),
$this->addResourcePrefixHeader($args)
$this->addRequestHeaders($args)
]);
}

Expand All @@ -152,7 +158,7 @@ public function commit(array $args)
return $this->send([$this->firestore, 'commit'], [
$this->pluck('database', $args),
$writes,
$this->addResourcePrefixHeader($args)
$this->addRequestHeaders($args)
]);
}

Expand All @@ -163,7 +169,7 @@ public function listCollectionIds(array $args)
{
return $this->send([$this->firestore, 'listCollectionIds'], [
$this->pluck('parent', $args),
$this->addResourcePrefixHeader($args)
$this->addRequestHeaders($args)
]);
}

Expand All @@ -181,7 +187,7 @@ public function listDocuments(array $args)
return $this->send([$this->firestore, 'listDocuments'], [
$this->pluck('parent', $args),
$this->pluck('collectionId', $args),
$this->addResourcePrefixHeader([
$this->addRequestHeaders([
'showMissing' => true
] + $args)
]);
Expand All @@ -195,7 +201,7 @@ public function rollback(array $args)
return $this->send([$this->firestore, 'rollback'], [
$this->pluck('database', $args),
$this->pluck('transaction', $args),
$this->addResourcePrefixHeader($args)
$this->addRequestHeaders($args)
]);
}

Expand All @@ -211,7 +217,7 @@ public function runQuery(array $args)

return $this->send([$this->firestore, 'runQuery'], [
$this->pluck('parent', $args),
$this->addResourcePrefixHeader($args)
$this->addRequestHeaders($args)
]);
}

Expand All @@ -223,17 +229,24 @@ private function documentMask(array $mask)
}

/**
* Add the `google-cloud-resource-prefix` header value to the request.
* Add required headers to the request.
*
* @param array $args
* @return array
*/
private function addResourcePrefixHeader(array $args)
private function addRequestHeaders(array $args)
{
$args['headers'] = [
'google-cloud-resource-prefix' => [$this->resourcePrefixHeader]
$args += [
'headers' => []
];

$args['headers']['google-cloud-resource-prefix'] = [$this->resourcePrefixHeader];

// Provide authentication header for requests when emulator is enabled.
if ($this->isUsingEmulator) {
$args['headers']['Authorization'] = ['Bearer owner'];
}

return $args;
}
}
49 changes: 44 additions & 5 deletions Firestore/tests/Unit/Connection/GrpcTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Google\Cloud\Core\GrpcRequestWrapper;
use Google\Cloud\Core\GrpcTrait;
use Google\Cloud\Core\Testing\GrpcTestTrait;
use Google\Cloud\Core\Testing\TestHelpers;
use Google\Cloud\Firestore\Connection\Grpc;
use Google\Cloud\Firestore\V1\Document;
use Google\Cloud\Firestore\V1\DocumentMask;
Expand Down Expand Up @@ -239,6 +240,43 @@ public function testRunQuery()
$this->sendAndAssert('runQuery', $args, $expected);
}

public function testCustomRequestHeaders()
{
$args = [
'parent' => sprintf('projects/%s/databases/%s/documents', self::PROJECT, self::DATABASE),
'headers' => [
'foo' => ['bar']
]
];

$headers = $this->header();
$headers['headers']['foo'] = ['bar'];
$expected = [$args['parent'], $headers];

$this->sendAndAssert('listCollectionIds', $args, $expected);
}

public function testProvidesAuthorizationHeaderWithEmulator()
{
$args = [
'parent' => sprintf('projects/%s/databases/%s/documents', self::PROJECT, self::DATABASE),
];

$headers = $this->header();
$headers['headers']['Authorization'] = ['Bearer owner'];
$expected = [$args['parent'], $headers];

$connection = TestHelpers::stub(Grpc::class, [
[
'projectId' => 'test',
'database' => '(default)'
]
], ['isUsingEmulator']);

$connection->___setProperty('isUsingEmulator', true);
$this->sendAndAssert('listCollectionIds', $args, $expected, $connection);
}

private function header()
{
return [
Expand All @@ -248,18 +286,19 @@ private function header()
];
}

private function sendAndAssert($method, array $args, array $expectedArgs)
private function sendAndAssert($method, array $args, array $expectedArgs, Grpc $connection = null)
{
$connection = $connection ?: new Grpc([
'projectId' => 'test',
'database' => '(default)'
]);

$this->requestWrapper->send(
Argument::type('callable'),
$expectedArgs,
Argument::type('array')
)->willReturn($this->successMessage);

$connection = new Grpc([
'projectId' => 'test',
'database' => '(default)'
]);
$connection->setRequestWrapper($this->requestWrapper->reveal());

$this->assertEquals($this->successMessage, $connection->$method($args));
Expand Down