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(fdc): Use emulator credentials when connecting to the Data Connect emulator #2851

Closed
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
17 changes: 16 additions & 1 deletion src/data-connect/data-connect-api-client-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ import * as utils from '../utils/index';
import * as validator from '../utils/validator';
import { ConnectorConfig, ExecuteGraphqlResponse, GraphqlOptions } from './data-connect-api';

function useDataConnectEmulator(): boolean {
return !!process.env.DATA_CONNECT_EMULATOR_HOST;
}

export class DataConnectEmulatorHttpClient extends AuthorizedHttpClient {

protected getToken(): Promise<string> {
if (useDataConnectEmulator()) {
return Promise.resolve('owner');
}

return super.getToken();
}
}

// Data Connect backend constants
const DATA_CONNECT_HOST = 'https://firebasedataconnect.googleapis.com';
const DATA_CONNECT_API_URL_FORMAT =
Expand Down Expand Up @@ -52,7 +67,7 @@ export class DataConnectApiClient {
DATA_CONNECT_ERROR_CODE_MAPPING.INVALID_ARGUMENT,
'First argument passed to getDataConnect() must be a valid Firebase app instance.');
}
this.httpClient = new AuthorizedHttpClient(app as FirebaseApp);
this.httpClient = new DataConnectEmulatorHttpClient(app as FirebaseApp);
}

/**
Expand Down
21 changes: 13 additions & 8 deletions test/unit/data-connect/data-connect-api-client-internal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
} from '../../../src/utils/api-request';
import * as utils from '../utils';
import * as mocks from '../../resources/mocks';
import { DataConnectApiClient, FirebaseDataConnectError }
import { DataConnectApiClient, FirebaseDataConnectError, DataConnectEmulatorHttpClient }
from '../../../src/data-connect/data-connect-api-client-internal';
import { FirebaseApp } from '../../../src/app/firebase-app';
import { ConnectorConfig } from '../../../src/data-connect';
Expand Down Expand Up @@ -156,7 +156,7 @@ describe('DataConnectApiClient', () => {
sandbox
.stub(HttpClient.prototype, 'send')
.rejects(utils.errorFrom({}, 404));
const expected = new FirebaseDataConnectError('unknown-error', 'Unknown server error: {}');
const expected = new FirebaseDataConnectError('unknown-error', 'Error returned from server: + . Additionally, an internal error occurred while attempting to extract the errorcode from the error.');
return apiClient.executeGraphql('query', {})
.should.eventually.be.rejected.and.deep.include(expected);
});
Expand Down Expand Up @@ -208,20 +208,25 @@ describe('DataConnectApiClient', () => {
});
});

it('should use DATA_CONNECT_EMULATOR_HOST if set', () => {
it('should use DataConnectEmulatorHttpClient when emulator host is defined', () => {
process.env.DATA_CONNECT_EMULATOR_HOST = 'http://localhost:9000';
const stub = sandbox
.stub(HttpClient.prototype, 'send')
.resolves(utils.responseFrom(TEST_RESPONSE, 200));
const stub = sandbox.stub(DataConnectEmulatorHttpClient.prototype, 'getToken').resolves('owner');
const sendStub = sandbox.stub(HttpClient.prototype, 'send').resolves(utils.responseFrom(TEST_RESPONSE, 200))
return apiClient.executeGraphql('query', {})
.then(() => {
expect(stub).to.have.been.calledOnce.and.calledWith({
expect(DataConnectEmulatorHttpClient.prototype.getToken).to.have.been.called;
expect(sendStub).to.have.been.calledOnce.and.calledWith({
method: 'POST',
url: `http://localhost:9000/v1alpha/projects/test-project/locations/${connectorConfig.location}/services/${connectorConfig.serviceId}:executeGraphql`,
headers: EXPECTED_HEADERS,
headers: {
'Authorization': 'Bearer owner',
'X-Firebase-Client': `fire-admin-node/${getSdkVersion()}`,
'X-Goog-Api-Client': getMetricsHeader(),
},
data: { query: 'query' }
});
});
});

});
});
Loading