Skip to content

Commit fd78680

Browse files
authored
fix(Firestore): use x-goog-request-params header (#8267)
1 parent 782a630 commit fd78680

File tree

6 files changed

+111
-22
lines changed

6 files changed

+111
-22
lines changed

.github/workflows/emulator-system-tests-bigtable.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
steps:
2222
- uses: actions/checkout@v5
2323

24-
- run: ./.github/emulator/start-emulator.sh bigtable 419.0.0-emulators
24+
- run: ./.github/emulator/start-emulator.sh bigtable 522.0.0-emulators
2525

2626
- name: Setup PHP
2727
uses: shivammathur/setup-php@v2

.github/workflows/emulator-system-tests-datastore.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
steps:
2222
- uses: actions/checkout@v5
2323

24-
- run: ./.github/emulator/start-emulator.sh datastore 419.0.0-emulators
24+
- run: ./.github/emulator/start-emulator.sh datastore 522.0.0-emulators
2525

2626
- name: Setup PHP
2727
uses: shivammathur/setup-php@v2

.github/workflows/emulator-system-tests-firestore.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
steps:
2222
- uses: actions/checkout@v5
2323

24-
- run: ./.github/emulator/start-emulator.sh firestore 453.0.0-emulators
24+
- run: ./.github/emulator/start-emulator.sh firestore 522.0.0-emulators
2525

2626
- name: Setup PHP
2727
uses: shivammathur/setup-php@v2

Firestore/src/Connection/Grpc.php

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,6 @@ class Grpc implements ConnectionInterface
5151
*/
5252
private $firestore;
5353

54-
/**
55-
* @var string
56-
*/
57-
private $resourcePrefixHeader;
58-
5954
/**
6055
* @var string
6156
*/
@@ -124,15 +119,8 @@ public function __construct(array $config = [])
124119
$projectId = $this->pluck('projectId', $config);
125120
$databaseId = $this->pluck('database', $config);
126121

127-
$this->resourcePrefixHeader = FirestoreClient::databaseRootName(
128-
$projectId,
129-
$databaseId
130-
);
131-
$this->databaseRoutingHeader = sprintf(
132-
'project_id=%s&database_id=%s',
133-
$projectId,
134-
$databaseId
135-
);
122+
$database = FirestoreClient::databaseRootName($projectId, $databaseId);
123+
$this->databaseRoutingHeader = sprintf('database=%s', urlencode($database));
136124
}
137125

138126
/**
@@ -315,8 +303,9 @@ private function addRequestHeaders(array $args)
315303
'headers' => []
316304
];
317305

318-
$args['headers']['google-cloud-resource-prefix'] = [$this->resourcePrefixHeader];
319-
$args['headers']['x-goog-request-params'] = [$this->databaseRoutingHeader];
306+
$args['headers']['x-goog-request-params'] = [
307+
$this->databaseRoutingHeader,
308+
];
320309

321310
// Provide authentication header for requests when emulator is enabled.
322311
if ($this->isUsingEmulator) {
@@ -354,7 +343,6 @@ public function __debugInfo()
354343
return [
355344
'serializer' => get_class($this->serializer),
356345
'firestore' => get_class($this->firestore),
357-
'resourcePrefixHeader' => $this->resourcePrefixHeader,
358346
'databaseRoutingHeader' => $this->databaseRoutingHeader,
359347
'isUsingEmulator' => $this->isUsingEmulator
360348
];
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
/**
3+
* Copyright 2017 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+
namespace Google\Cloud\Firestore\Tests\System;
19+
20+
use Google\ApiCore\ApiException;
21+
use Google\ApiCore\BidiStream;
22+
use Google\Cloud\Firestore\V1\Client\FirestoreClient;
23+
use Google\Cloud\Firestore\V1\ListenRequest;
24+
use Google\Cloud\Firestore\V1\ListenResponse;
25+
26+
/**
27+
* @group firestore
28+
* @group firestore-listen
29+
*/
30+
class ListenTest extends FirestoreTestCase
31+
{
32+
const DATABASE = 'projects/%s/databases/(default)';
33+
34+
private $query;
35+
36+
public function setUp(): void
37+
{
38+
$this->query = self::$client->collection(uniqid(self::COLLECTION_NAME));
39+
self::$localDeletionQueue->add($this->query);
40+
if (!$this->projectId = getenv('GOOGLE_CLOUD_FIRESTORE_PROJECT')) {
41+
$this->markTestSkipped('please set the GOOGLE_CLOUD_FIRESTORE_PROJECT env var');
42+
}
43+
}
44+
45+
public function testListen()
46+
{
47+
$database = sprintf(self::DATABASE, $this->projectId);
48+
49+
// Create a client.
50+
$firestoreClient = new FirestoreClient();
51+
52+
// Prepare the request message.
53+
$request = (new ListenRequest())
54+
->setDatabase($database);
55+
56+
// Call the API and handle any network failures.
57+
/** @var BidiStream $stream */
58+
$stream = $firestoreClient->listen([
59+
'headers' => [
60+
'x-goog-request-params' => [
61+
'database=' . $database
62+
]
63+
]
64+
]);
65+
$stream->writeAll([$request,]);
66+
67+
/** @var ListenResponse $element */
68+
foreach ($stream->closeWriteAndReadAll() as $element) {
69+
// TODO: Assert something
70+
}
71+
$this->assertTrue(true);
72+
}
73+
74+
public function testListenThrowsExceptionWithoutDatabaseHeader()
75+
{
76+
$this->expectException(ApiException::class);
77+
$this->expectExceptionMessage(
78+
'Missing required http header (\'google-cloud-resource-prefix\' or \'x-goog-request-params\')'
79+
. ' or query param \'database\'.'
80+
);
81+
$database = sprintf(self::DATABASE, $this->projectId);
82+
83+
// Create a client.
84+
$firestoreClient = new FirestoreClient();
85+
86+
// Prepare the request message.
87+
$request = (new ListenRequest())
88+
->setDatabase($database);
89+
90+
// Call the API and handle any network failures.
91+
/** @var BidiStream $stream */
92+
$stream = $firestoreClient->listen();
93+
$stream->writeAll([$request,]);
94+
95+
/** @var ListenResponse $element */
96+
foreach ($stream->closeWriteAndReadAll() as $element) {
97+
// TODO: Assert something
98+
}
99+
}
100+
}

Firestore/tests/Unit/Connection/GrpcTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,9 @@ private function header()
404404
{
405405
return [
406406
"headers" => [
407-
"google-cloud-resource-prefix" => ["projects/test/databases/(default)"],
408-
"x-goog-request-params" => ["project_id=test&database_id=(default)"]
407+
"x-goog-request-params" => [
408+
'database=projects%2Ftest%2Fdatabases%2F%28default%29'
409+
]
409410
]
410411
];
411412
}

0 commit comments

Comments
 (0)