Skip to content
Open
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
13 changes: 7 additions & 6 deletions src/implementation/Client/GRPCClient/GRPCClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,19 @@ export default class GRPCClient implements IClient {
}

private generateEndpoint(options: Partial<DaprClientOptions>): GrpcEndpoint {
const host = options?.daprHost ?? Settings.getDefaultHost();
const port = options?.daprPort ?? Settings.getDefaultGrpcPort();
let uri = `${host}:${port}`;

// Check for explicit endpoint first (highest priority)
if (!(options?.daprHost || options?.daprPort)) {
// If neither host nor port are specified, check the endpoint environment variable.
const endpoint = Settings.getDefaultGrpcEndpoint();
if (endpoint != "") {
uri = endpoint;
return new GrpcEndpoint(endpoint);
}
}

// Use provided options or environment variable defaults
const host = options?.daprHost ?? Settings.getDefaultHost();
const port = options?.daprPort ?? Settings.getDefaultGrpcPort();
const uri = `${host}:${port}`;

return new GrpcEndpoint(uri);
}

Expand Down
13 changes: 7 additions & 6 deletions src/implementation/Client/HTTPClient/HTTPClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,19 @@ export default class HTTPClient implements IClient {
}

private generateEndpoint(options: Partial<DaprClientOptions>): HttpEndpoint {
const host = options?.daprHost ?? Settings.getDefaultHost();
const port = options?.daprPort ?? Settings.getDefaultHttpPort();
let uri = `${host}:${port}`;

// Check for explicit endpoint first (highest priority)
if (!(options?.daprHost || options?.daprPort)) {
// If neither host nor port are specified, check the endpoint environment variable.
const endpoint = Settings.getDefaultHttpEndpoint();
if (endpoint != "") {
uri = endpoint;
return new HttpEndpoint(endpoint);
}
}

// Use provided options or environment variable defaults
const host = options?.daprHost ?? Settings.getDefaultHost();
const port = options?.daprPort ?? Settings.getDefaultHttpPort();
const uri = `${host}:${port}`;

return new HttpEndpoint(uri);
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/Settings.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class Settings {
}

static getDefaultHost(): string {
return Settings.defaultHost;
return process.env.DAPR_RUNTIME_HOST ?? Settings.defaultHost;
}

static getDefaultHttpPort(): string {
Expand Down
79 changes: 79 additions & 0 deletions test/unit/utils/Settings.env.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Copyright 2022 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/* eslint-disable @typescript-eslint/no-var-requires */
// No top-level imports - we use jest.isolateModules() to import fresh modules

describe("Settings - Environment Variable Support", () => {
// Store original env vars to restore later
const originalEnv = { ...process.env };

afterEach(() => {
// Restore original env after each test
process.env = { ...originalEnv };
});

describe("getDefaultHost", () => {
it("respects DAPR_RUNTIME_HOST environment variable", () => {
jest.isolateModules(() => {
process.env.DAPR_RUNTIME_HOST = "192.168.1.100";
const { Settings } = require("../../../src/utils/Settings.util");
expect(Settings.getDefaultHost()).toEqual("192.168.1.100");
});
});

it("returns default when DAPR_RUNTIME_HOST is not set", () => {
jest.isolateModules(() => {
delete process.env.DAPR_RUNTIME_HOST;
const { Settings } = require("../../../src/utils/Settings.util");
expect(Settings.getDefaultHost()).toEqual("127.0.0.1");
});
});
});

describe("getDefaultHttpPort", () => {
it("respects DAPR_HTTP_PORT environment variable", () => {
jest.isolateModules(() => {
process.env.DAPR_HTTP_PORT = "3510";
const { Settings } = require("../../../src/utils/Settings.util");
expect(Settings.getDefaultHttpPort()).toEqual("3510");
});
});

it("returns default when DAPR_HTTP_PORT is not set", () => {
jest.isolateModules(() => {
delete process.env.DAPR_HTTP_PORT;
const { Settings } = require("../../../src/utils/Settings.util");
expect(Settings.getDefaultHttpPort()).toEqual("3500");
});
});
});

describe("getDefaultGrpcPort", () => {
it("respects DAPR_GRPC_PORT environment variable", () => {
jest.isolateModules(() => {
process.env.DAPR_GRPC_PORT = "50010";
const { Settings } = require("../../../src/utils/Settings.util");
expect(Settings.getDefaultGrpcPort()).toEqual("50010");
});
});

it("returns default when DAPR_GRPC_PORT is not set", () => {
jest.isolateModules(() => {
delete process.env.DAPR_GRPC_PORT;
const { Settings } = require("../../../src/utils/Settings.util");
expect(Settings.getDefaultGrpcPort()).toEqual("50001");
});
});
});
});
1 change: 1 addition & 0 deletions test/unit/utils/Settings.util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe("Settings", () => {
expect(Settings.getDefaultPort(CommunicationProtocolEnum.HTTP)).toEqual(Settings.getDefaultHttpPort());
});
});

describe("getDefaultAppPort returns the correct default when", () => {
it("communication protocol is GRPC", () => {
expect(Settings.getDefaultAppPort(CommunicationProtocolEnum.GRPC)).toEqual(Settings.getDefaultGrpcAppPort());
Expand Down