|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2024 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 { deleteApp, initializeApp, FirebaseApp } from '@firebase/app'; |
| 19 | +import { expect, use } from 'chai'; |
| 20 | +import * as sinon from 'sinon'; |
| 21 | +import sinonChai from 'sinon-chai'; |
| 22 | + |
| 23 | +import { DataConnect, executeQuery, getDataConnect, queryRef } from '../../src'; |
| 24 | +import { initializeFetch } from '../../src/network/fetch'; |
| 25 | + |
| 26 | +use(sinonChai); |
| 27 | +const json = { |
| 28 | + message: 'unauthorized' |
| 29 | +}; |
| 30 | +const fakeFetchImpl = sinon.stub().returns( |
| 31 | + Promise.resolve({ |
| 32 | + json: () => { |
| 33 | + return Promise.resolve(json); |
| 34 | + }, |
| 35 | + status: 401 |
| 36 | + } as Response) |
| 37 | +); |
| 38 | + |
| 39 | +describe('GMPID Tests', () => { |
| 40 | + let dc: DataConnect; |
| 41 | + let app: FirebaseApp; |
| 42 | + const APPID = 'MYAPPID'; |
| 43 | + beforeEach(() => { |
| 44 | + initializeFetch(fakeFetchImpl); |
| 45 | + app = initializeApp({ projectId: 'p', appId: APPID }, 'fdsasdf'); // TODO(mtewani): Replace with util function |
| 46 | + dc = getDataConnect(app, { connector: 'c', location: 'l', service: 's' }); |
| 47 | + }); |
| 48 | + afterEach(async () => { |
| 49 | + await dc._delete(); |
| 50 | + await deleteApp(app); |
| 51 | + }); |
| 52 | + it('should send a request with the corresponding gmpid if using the app id is specified', async () => { |
| 53 | + // @ts-ignore |
| 54 | + await executeQuery(queryRef(dc, '')).catch(() => {}); |
| 55 | + expect(fakeFetchImpl).to.be.calledWithMatch( |
| 56 | + 'https://firebasedataconnect.googleapis.com/v1alpha/projects/p/locations/l/services/s/connectors/c:executeQuery', |
| 57 | + { |
| 58 | + headers: { |
| 59 | + ['x-firebase-gmpid']: APPID |
| 60 | + } |
| 61 | + } |
| 62 | + ); |
| 63 | + }); |
| 64 | + it('should send a request with no gmpid if using the app id is not specified', async () => { |
| 65 | + const app2 = initializeApp({ projectId: 'p' }, 'def'); // TODO(mtewani): Replace with util function |
| 66 | + const dc2 = getDataConnect(app2, { |
| 67 | + connector: 'c', |
| 68 | + location: 'l', |
| 69 | + service: 's' |
| 70 | + }); |
| 71 | + // @ts-ignore |
| 72 | + await executeQuery(queryRef(dc2, '')).catch(() => {}); |
| 73 | + expect(fakeFetchImpl).to.be.calledWithMatch( |
| 74 | + 'https://firebasedataconnect.googleapis.com/v1alpha/projects/p/locations/l/services/s/connectors/c:executeQuery', |
| 75 | + { |
| 76 | + headers: { |
| 77 | + ['x-firebase-gmpid']: APPID |
| 78 | + } |
| 79 | + } |
| 80 | + ); |
| 81 | + await dc2._delete(); |
| 82 | + await deleteApp(app2); |
| 83 | + }); |
| 84 | +}); |
0 commit comments