|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 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 | +import { Metadata } from '@grpc/grpc-js'; |
| 19 | +import { expect } from 'chai'; |
| 20 | + |
| 21 | +import { DatabaseId, DatabaseInfo } from '../../../src/core/database_info'; |
| 22 | +import { ResourcePath } from '../../../src/model/path'; |
| 23 | +import { GrpcConnection } from '../../../src/platform/node/grpc_connection'; |
| 24 | + |
| 25 | +export class TestGrpcConnection extends GrpcConnection { |
| 26 | + mockStub = { |
| 27 | + lastMetadata: null, |
| 28 | + mockRpc( |
| 29 | + req: unknown, |
| 30 | + metadata: Metadata, |
| 31 | + callback: (err: unknown, resp: unknown) => void |
| 32 | + ) { |
| 33 | + this.lastMetadata = metadata; |
| 34 | + callback(null, null); |
| 35 | + } |
| 36 | + } as { |
| 37 | + lastMetadata: null | Metadata; |
| 38 | + [index: string]: unknown; |
| 39 | + }; |
| 40 | + |
| 41 | + protected ensureActiveStub(): unknown { |
| 42 | + return this.mockStub; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +describe('GrpcConnection', () => { |
| 47 | + const testDatabaseInfo = new DatabaseInfo( |
| 48 | + new DatabaseId('testproject'), |
| 49 | + 'test-app-id', |
| 50 | + 'persistenceKey', |
| 51 | + 'example.com', |
| 52 | + /*ssl=*/ false, |
| 53 | + /*forceLongPolling=*/ false, |
| 54 | + /*autoDetectLongPolling=*/ false, |
| 55 | + /*longPollingOptions=*/ {}, |
| 56 | + /*useFetchStreams=*/ false, |
| 57 | + /*isUsingEmulator=*/ false, |
| 58 | + 'grpc-connection-test-api-key' |
| 59 | + ); |
| 60 | + const connection = new TestGrpcConnection( |
| 61 | + { google: { firestore: { v1: {} } } }, |
| 62 | + testDatabaseInfo |
| 63 | + ); |
| 64 | + |
| 65 | + it('Passes the API Key from DatabaseInfo to the grpc stub', async () => { |
| 66 | + const request = { |
| 67 | + database: 'projects/testproject/databases/(default)', |
| 68 | + writes: [] |
| 69 | + }; |
| 70 | + await connection.invokeRPC( |
| 71 | + 'mockRpc', |
| 72 | + ResourcePath.emptyPath(), |
| 73 | + request, |
| 74 | + null, |
| 75 | + null |
| 76 | + ); |
| 77 | + expect( |
| 78 | + connection.mockStub.lastMetadata?.get('x-goog-api-key') |
| 79 | + ).to.deep.equal(['grpc-connection-test-api-key']); |
| 80 | + }); |
| 81 | +}); |
0 commit comments