From 48e4de8d6b42f4377f4243a5f02bca077de580c4 Mon Sep 17 00:00:00 2001 From: Whit Waldo Date: Wed, 16 Oct 2024 01:52:51 -0500 Subject: [PATCH] Updated to put endpoint resolution mechanism in DaprDefaults within Dapr.Common - updating projects and unit tests Signed-off-by: Whit Waldo --- all.sln | 7 + .../ActorsServiceCollectionExtensions.cs | 110 +--- .../Dapr.Actors.AspNetCore.csproj | 1 + src/Dapr.Actors/Client/ActorProxyOptions.cs | 2 +- .../Runtime/ActorRuntimeOptions.cs | 22 +- .../DaprAuthenticationOptions.cs | 3 +- .../DaprServiceCollectionExtensions.cs | 145 +---- src/Dapr.Client/DaprClient.cs | 11 +- src/Dapr.Client/DaprClientBuilder.cs | 2 +- src/Dapr.Client/InvocationHandler.cs | 2 +- src/Dapr.Common/Dapr.Common.csproj | 1 + src/Dapr.Common/DaprDefaults.cs | 124 ++-- .../DaprServiceCollectionExtensionsTest.cs | 601 ------------------ test/Dapr.Common.Test/Dapr.Common.Test.csproj | 23 + test/Dapr.Common.Test/DaprDefaultTest.cs | 582 +++++++++++++++++ 15 files changed, 723 insertions(+), 913 deletions(-) create mode 100644 test/Dapr.Common.Test/Dapr.Common.Test.csproj create mode 100644 test/Dapr.Common.Test/DaprDefaultTest.cs diff --git a/all.sln b/all.sln index 1a5d78efb..85ed848a4 100644 --- a/all.sln +++ b/all.sln @@ -122,6 +122,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Protos", "src\Dapr.Pro EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Common", "src\Dapr.Common\Dapr.Common.csproj", "{B445B19C-A925-4873-8CB7-8317898B6970}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Common.Test", "test\Dapr.Common.Test\Dapr.Common.Test.csproj", "{CDB47863-BEBD-4841-A807-46D868962521}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -302,6 +304,10 @@ Global {B445B19C-A925-4873-8CB7-8317898B6970}.Debug|Any CPU.Build.0 = Debug|Any CPU {B445B19C-A925-4873-8CB7-8317898B6970}.Release|Any CPU.ActiveCfg = Release|Any CPU {B445B19C-A925-4873-8CB7-8317898B6970}.Release|Any CPU.Build.0 = Release|Any CPU + {CDB47863-BEBD-4841-A807-46D868962521}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CDB47863-BEBD-4841-A807-46D868962521}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -357,6 +363,7 @@ Global {C74FBA78-13E8-407F-A173-4555AEE41FF3} = {A7F41094-8648-446B-AECD-DCC2CC871F73} {DFBABB04-50E9-42F6-B470-310E1B545638} = {27C5D71D-0721-4221-9286-B94AB07B58CF} {B445B19C-A925-4873-8CB7-8317898B6970} = {27C5D71D-0721-4221-9286-B94AB07B58CF} + {CDB47863-BEBD-4841-A807-46D868962521} = {DD020B34-460F-455F-8D17-CF4A949F100B} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {65220BF2-EAE1-4CB2-AA58-EBE80768CB40} diff --git a/src/Dapr.Actors.AspNetCore/ActorsServiceCollectionExtensions.cs b/src/Dapr.Actors.AspNetCore/ActorsServiceCollectionExtensions.cs index a274f992a..6bba542fd 100644 --- a/src/Dapr.Actors.AspNetCore/ActorsServiceCollectionExtensions.cs +++ b/src/Dapr.Actors.AspNetCore/ActorsServiceCollectionExtensions.cs @@ -47,15 +47,14 @@ public static void AddActors(this IServiceCollection? services, Action>().Value; - //Replace the HttpEndpoint with an endpoint prioritizing IConfiguration var configuration = s.GetService(); - options.HttpEndpoint = options.HttpEndpoint != "http://127.0.0.1:3500" - ? GetHttpEndpoint(configuration) - : options.HttpEndpoint; - options.DaprApiToken = string.IsNullOrWhiteSpace(options.DaprApiToken) - ? GetApiToken(configuration) - : options.DaprApiToken; - + options.DaprApiToken = !string.IsNullOrWhiteSpace(options.DaprApiToken) + ? options.DaprApiToken + : DaprDefaults.GetDefaultDaprApiToken(configuration); + options.HttpEndpoint = !string.IsNullOrWhiteSpace(options.HttpEndpoint) + ? options.HttpEndpoint + : DaprDefaults.GetDefaultHttpEndpoint(); + var loggerFactory = s.GetRequiredService(); var activatorFactory = s.GetRequiredService(); var proxyFactory = s.GetRequiredService(); @@ -68,12 +67,12 @@ public static void AddActors(this IServiceCollection? services, Action(); - options.HttpEndpoint = options.HttpEndpoint != "http://127.0.0.1:3500" - ? GetHttpEndpoint(configuration) - : options.HttpEndpoint; - options.DaprApiToken = string.IsNullOrWhiteSpace(options.DaprApiToken) - ? GetApiToken(configuration) - : options.DaprApiToken; + options.DaprApiToken = !string.IsNullOrWhiteSpace(options.DaprApiToken) + ? options.DaprApiToken + : DaprDefaults.GetDefaultDaprApiToken(configuration); + options.HttpEndpoint = !string.IsNullOrWhiteSpace(options.HttpEndpoint) + ? options.HttpEndpoint + : DaprDefaults.GetDefaultHttpEndpoint(); var factory = new ActorProxyFactory() { @@ -93,88 +92,5 @@ public static void AddActors(this IServiceCollection? services, Action(configure); } } - - /// - /// Retrieves the Dapr API token using a failover approach starting with an optional - /// instance, then trying to pull from the well-known environment variable name and then opting for an empty string - /// as a default value. - /// - /// The Dapr API token. - private static string GetApiToken(IConfiguration? configuration) => GetResourceValue(configuration, DaprDefaults.DaprApiTokenName); - - /// - /// Builds the Dapr gRPC endpoint using the value from the IConfiguration, if available, then falling back - /// to the value in the environment variable(s) and finally otherwise using the default value (an empty string). - /// - /// - /// Marked as internal for testing purposes. - /// - /// An injected instance of the . - /// The built gRPC endpoint. - private static string GetHttpEndpoint(IConfiguration? configuration) - { - //Prioritize pulling from IConfiguration with a fallback from pulling from the environment variable directly - var httpEndpoint = GetResourceValue(configuration, DaprDefaults.DaprHttpEndpointName); - var httpPort = GetResourceValue(configuration, DaprDefaults.DaprHttpPortName); - int? parsedGrpcPort = string.IsNullOrWhiteSpace(httpPort) ? null : int.Parse(httpPort); - - var endpoint = BuildEndpoint(httpEndpoint, parsedGrpcPort); - return string.IsNullOrWhiteSpace(endpoint) ? $"http://localhost:{DaprDefaults.DefaultHttpPort}/" : endpoint; - } - - /// - /// Retrieves the specified value prioritizing pulling it from , falling back - /// to an environment variable, and using an empty string as a default. - /// - /// An instance of an . - /// The name of the value to retrieve. - /// The value of the resource. - private static string GetResourceValue(IConfiguration? configuration, string name) - { - //Attempt to retrieve first from the configuration - var configurationValue = configuration?.GetValue(name); - if (configurationValue is not null) - return configurationValue; - - //Fall back to the environment variable with the same name or default to an empty string - var envVar = Environment.GetEnvironmentVariable(name); - return envVar ?? string.Empty; - } - - /// - /// Builds the endpoint provided an optional endpoint and optional port. - /// - /// - /// Marked as internal for testing purposes. - /// - /// The endpoint. - /// The port - /// A constructed endpoint value. - internal static string BuildEndpoint(string? endpoint, int? endpointPort) - { - if (string.IsNullOrWhiteSpace(endpoint) && endpointPort is null) - return string.Empty; - - var endpointBuilder = new UriBuilder(); - if (!string.IsNullOrWhiteSpace(endpoint)) - { - //Extract the scheme, host and port from the endpoint - var uri = new Uri(endpoint); - endpointBuilder.Scheme = uri.Scheme; - endpointBuilder.Host = uri.Host; - endpointBuilder.Port = uri.Port; - - //Update the port if provided separately - if (endpointPort is not null) - endpointBuilder.Port = (int)endpointPort; - } - else if (string.IsNullOrWhiteSpace(endpoint) && endpointPort is not null) - { - endpointBuilder.Host = "localhost"; - endpointBuilder.Port = (int)endpointPort; - } - - return endpointBuilder.ToString(); - } } } diff --git a/src/Dapr.Actors.AspNetCore/Dapr.Actors.AspNetCore.csproj b/src/Dapr.Actors.AspNetCore/Dapr.Actors.AspNetCore.csproj index 82c5863db..ef57e76fe 100644 --- a/src/Dapr.Actors.AspNetCore/Dapr.Actors.AspNetCore.csproj +++ b/src/Dapr.Actors.AspNetCore/Dapr.Actors.AspNetCore.csproj @@ -11,5 +11,6 @@ + diff --git a/src/Dapr.Actors/Client/ActorProxyOptions.cs b/src/Dapr.Actors/Client/ActorProxyOptions.cs index 665a1dced..2afce852c 100644 --- a/src/Dapr.Actors/Client/ActorProxyOptions.cs +++ b/src/Dapr.Actors/Client/ActorProxyOptions.cs @@ -45,7 +45,7 @@ public JsonSerializerOptions JsonSerializerOptions /// /// The Dapr Api Token that is added to the header for all requests. /// - public string DaprApiToken { get; set; } = DaprDefaults.GetDefaultDaprApiToken(); + public string DaprApiToken { get; set; } = DaprDefaults.GetDefaultDaprApiToken(null); /// /// Gets or sets the HTTP endpoint URI used to communicate with the Dapr sidecar. diff --git a/src/Dapr.Actors/Runtime/ActorRuntimeOptions.cs b/src/Dapr.Actors/Runtime/ActorRuntimeOptions.cs index 62eaceea6..21c302018 100644 --- a/src/Dapr.Actors/Runtime/ActorRuntimeOptions.cs +++ b/src/Dapr.Actors/Runtime/ActorRuntimeOptions.cs @@ -11,6 +11,8 @@ // limitations under the License. // ------------------------------------------------------------------------ +#nullable enable + using System; using System.Text.Json; @@ -34,7 +36,7 @@ public sealed class ActorRuntimeOptions }; private bool useJsonSerialization = false; private JsonSerializerOptions jsonSerializerOptions = JsonSerializerDefaults.Web; - private string daprApiToken = DaprDefaults.GetDefaultDaprApiToken(); + private string daprApiToken = string.Empty; private int? remindersStoragePartitions = null; /// @@ -180,19 +182,14 @@ public JsonSerializerOptions JsonSerializerOptions set { - if (value is null) - { - throw new ArgumentNullException(nameof(JsonSerializerOptions), $"{nameof(ActorRuntimeOptions)}.{nameof(JsonSerializerOptions)} cannot be null"); - } - - this.jsonSerializerOptions = value; + this.jsonSerializerOptions = value ?? throw new ArgumentNullException(nameof(JsonSerializerOptions), $"{nameof(ActorRuntimeOptions)}.{nameof(JsonSerializerOptions)} cannot be null"); } } /// /// The to add to the headers in requests to Dapr runtime /// - public string DaprApiToken + public string? DaprApiToken { get { @@ -201,12 +198,7 @@ public string DaprApiToken set { - if (value is null) - { - throw new ArgumentNullException(nameof(DaprApiToken), $"{nameof(ActorRuntimeOptions)}.{nameof(DaprApiToken)} cannot be null"); - } - - this.daprApiToken = value; + this.daprApiToken = value ?? throw new ArgumentNullException(nameof(DaprApiToken), $"{nameof(ActorRuntimeOptions)}.{nameof(DaprApiToken)} cannot be null"); } } @@ -241,6 +233,6 @@ public int? RemindersStoragePartitions /// corresponding environment variables. /// /// - public string HttpEndpoint { get; set; } = DaprDefaults.GetDefaultHttpEndpoint(); + public string? HttpEndpoint { get; set; } } } diff --git a/src/Dapr.AspNetCore/DaprAuthenticationOptions.cs b/src/Dapr.AspNetCore/DaprAuthenticationOptions.cs index 4e073d06c..b12d4d14e 100644 --- a/src/Dapr.AspNetCore/DaprAuthenticationOptions.cs +++ b/src/Dapr.AspNetCore/DaprAuthenticationOptions.cs @@ -11,7 +11,6 @@ // limitations under the License. // ------------------------------------------------------------------------ -using System; using Microsoft.AspNetCore.Authentication; namespace Dapr.AspNetCore @@ -29,6 +28,6 @@ public class DaprAuthenticationOptions : AuthenticationSchemeOptions /// Gets or sets the App API token. /// By default, the token will be read from the APP_API_TOKEN environment variable. /// - public string Token { get; set; } = DaprDefaults.GetDefaultAppApiToken(); + public string Token { get; set; } = DaprDefaults.GetDefaultAppApiToken(null); } } diff --git a/src/Dapr.AspNetCore/DaprServiceCollectionExtensions.cs b/src/Dapr.AspNetCore/DaprServiceCollectionExtensions.cs index c4dfff2ee..26c557d80 100644 --- a/src/Dapr.AspNetCore/DaprServiceCollectionExtensions.cs +++ b/src/Dapr.AspNetCore/DaprServiceCollectionExtensions.cs @@ -42,18 +42,14 @@ public static void AddDaprClient(this IServiceCollection services, Action(); - //Set the HTTP endpoint, if provided - var httpEndpoint = GetHttpEndpoint(configuration); - if (!string.IsNullOrWhiteSpace(httpEndpoint)) - builder.UseHttpEndpoint(httpEndpoint); + //Set the HTTP endpoint, if provided, else use the default endpoint + builder.UseHttpEndpoint(DaprDefaults.GetDefaultHttpEndpoint(configuration)); //Set the gRPC endpoint, if provided - var grpcEndpoint = GetGrpcEndpoint(configuration); - if (!string.IsNullOrWhiteSpace(grpcEndpoint)) - builder.UseGrpcEndpoint(grpcEndpoint); - + builder.UseGrpcEndpoint(DaprDefaults.GetDefaultGrpcEndpoint(configuration)); + //Set the API token, if provided - var apiToken = GetApiToken(configuration); + var apiToken = DaprDefaults.GetDefaultDaprApiToken(configuration); if (!string.IsNullOrWhiteSpace(apiToken)) builder.UseDaprApiToken(apiToken); @@ -80,18 +76,14 @@ public static void AddDaprClient(this IServiceCollection services, var configuration = serviceProvider.GetService(); - //Set the HTTP endpoint, if provided - var httpEndpoint = GetHttpEndpoint(configuration); - if (!string.IsNullOrWhiteSpace(httpEndpoint)) - builder.UseHttpEndpoint(httpEndpoint); + //Set the HTTP endpoint, if provided, else use the default endpoint + builder.UseHttpEndpoint(DaprDefaults.GetDefaultHttpEndpoint(configuration)); //Set the gRPC endpoint, if provided - var grpcEndpoint = GetGrpcEndpoint(configuration); - if (!string.IsNullOrWhiteSpace(grpcEndpoint)) - builder.UseGrpcEndpoint(grpcEndpoint); - + builder.UseGrpcEndpoint(DaprDefaults.GetDefaultGrpcEndpoint(configuration)); + //Set the API token, if provided - var apiToken = GetApiToken(configuration); + var apiToken = DaprDefaults.GetDefaultDaprApiToken(configuration); if (!string.IsNullOrWhiteSpace(apiToken)) builder.UseDaprApiToken(apiToken); @@ -100,121 +92,4 @@ public static void AddDaprClient(this IServiceCollection services, return builder.Build(); }); } - - /// - /// Builds the Dapr HTTP endpoint using the value from the IConfiguration, if available, then falling back - /// to the value in the environment variable(s) and finally otherwise using the default value (an empty string). - /// - /// - /// Marked as internal for testing purposes. - /// - /// An injected instance of the . - /// The built HTTP endpoint. - internal static string GetHttpEndpoint(IConfiguration? configuration) - { - //Prioritize pulling from IConfiguration with a fallback of pulling from the environment variable directly - var httpEndpoint = GetResourceValue(configuration, DaprDefaults.DaprHttpEndpointName); - var httpPort = GetResourceValue(configuration, DaprDefaults.DaprHttpPortName); - int? parsedHttpPort = int.TryParse(httpPort, out var port) ? port : null; - - var endpoint = BuildEndpoint(httpEndpoint, parsedHttpPort); - return string.IsNullOrWhiteSpace(endpoint) - ? $"http://{DaprDefaults.DaprHostName}:{DaprDefaults.DefaultHttpPort}/" - : endpoint; - } - - /// - /// Builds the Dapr gRPC endpoint using the value from the IConfiguration, if available, then falling back - /// to the value in the environment variable(s) and finally otherwise using the default value (an empty string). - /// - /// - /// Marked as internal for testing purposes. - /// - /// An injected instance of the . - /// The built gRPC endpoint. - internal static string GetGrpcEndpoint(IConfiguration? configuration) - { - //Prioritize pulling from IConfiguration with a fallback from pulling from the environment variable directly - var grpcEndpoint = GetResourceValue(configuration, DaprDefaults.DaprGrpcEndpointName); - var grpcPort = GetResourceValue(configuration, DaprDefaults.DaprGrpcPortName); - int? parsedGrpcPort = int.TryParse(grpcPort, out var port) ? port : null; - - var endpoint = BuildEndpoint(grpcEndpoint, parsedGrpcPort); - return string.IsNullOrWhiteSpace(endpoint) - ? $"http://{DaprDefaults.DaprHostName}:{DaprDefaults.DefaultGrpcPort}/" - : endpoint; - } - - /// - /// Retrieves the Dapr API token first from the , if available, then falling back - /// to the value in the environment variable(s) directly, then finally otherwise using the default value (an - /// empty string). - /// - /// - /// Marked as internal for testing purposes. - /// - /// An injected instance of the . - /// The Dapr API token. - internal static string GetApiToken(IConfiguration? configuration) - { - //Prioritize pulling from IConfiguration with a fallback of pulling from the environment variable directly - return GetResourceValue(configuration, DaprDefaults.DaprApiTokenName); - } - - /// - /// Retrieves the specified value prioritizing pulling it from , falling back - /// to an environment variable, and using an empty string as a default. - /// - /// An instance of an . - /// The name of the value to retrieve. - /// The value of the resource. - private static string GetResourceValue(IConfiguration? configuration, string name) - { - //Attempt to retrieve first from the configuration - var configurationValue = configuration?.GetValue(name); - if (configurationValue is not null) - return configurationValue; - - //Fall back to the environment variable with the same name or default to an empty string - var envVar = Environment.GetEnvironmentVariable(name); - return envVar ?? string.Empty; - } - - /// - /// Builds the endpoint provided an optional endpoint and optional port. - /// - /// - /// Marked as internal for testing purposes. - /// - /// The endpoint. - /// The port - /// A constructed endpoint value. - internal static string BuildEndpoint(string? endpoint, int? endpointPort) - { - if (string.IsNullOrWhiteSpace(endpoint) && endpointPort is null) - return string.Empty; - - // Per the proposal at https://github.com/artursouza/proposals/blob/cd811136d0af0aade52ef297a84c3050f3243ae8/0008-S-sidecar-endpoint-tls.md#design this will - // favor whatever value is provided in the endpoint value (and if the port isn't provided, it will be inferred from the protocol). - - // The endpoint port will only be evaluated if the endpoint is not provided. While the proposal calls for a value of "127.0.0.1", it does accept its - // equivalent, e.g. "localhost", and because of the issue detailed at https://github.com/dapr/dotnet-sdk/issues/1032 - - var endpointBuilder = new UriBuilder(); - if (!string.IsNullOrWhiteSpace(endpoint)) //Ignore the endpoint port argument if the endpoint is provided - { - //Extract the scheme, host and port from the endpoint - var uri = new Uri(endpoint); - endpointBuilder.Scheme = uri.Scheme; - endpointBuilder.Host = uri.Host; - endpointBuilder.Port = uri.Port; - } - else if (string.IsNullOrWhiteSpace(endpoint) && endpointPort is not null) - { - endpointBuilder.Host = "localhost"; - endpointBuilder.Port = (int)endpointPort; - } - - return endpointBuilder.ToString(); - } } diff --git a/src/Dapr.Client/DaprClient.cs b/src/Dapr.Client/DaprClient.cs index 9f107578f..43c640a69 100644 --- a/src/Dapr.Client/DaprClient.cs +++ b/src/Dapr.Client/DaprClient.cs @@ -139,17 +139,14 @@ public static HttpClient CreateInvokeHttpClient(string appId = null, string dapr public static CallInvoker CreateInvocationInvoker(string appId, string daprEndpoint = null, string daprApiToken = null) { var channel = GrpcChannel.ForAddress(daprEndpoint ?? DaprDefaults.GetDefaultGrpcEndpoint()); - return channel.Intercept(new InvocationInterceptor(appId, daprApiToken ?? DaprDefaults.GetDefaultDaprApiToken())); + return channel.Intercept(new InvocationInterceptor(appId, daprApiToken ?? DaprDefaults.GetDefaultDaprApiToken(null))); } internal static KeyValuePair? GetDaprApiTokenHeader(string apiToken) { - if (string.IsNullOrWhiteSpace(apiToken)) - { - return null; - } - - return new KeyValuePair("dapr-api-token", apiToken); + return string.IsNullOrWhiteSpace(apiToken) + ? null + : new KeyValuePair("dapr-api-token", apiToken); } /// diff --git a/src/Dapr.Client/DaprClientBuilder.cs b/src/Dapr.Client/DaprClientBuilder.cs index 50a4979d1..68315c45b 100644 --- a/src/Dapr.Client/DaprClientBuilder.cs +++ b/src/Dapr.Client/DaprClientBuilder.cs @@ -40,7 +40,7 @@ public DaprClientBuilder() }; this.JsonSerializerOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web); - this.DaprApiToken = DaprDefaults.GetDefaultDaprApiToken(); + this.DaprApiToken = DaprDefaults.GetDefaultDaprApiToken(null); } // property exposed for testing purposes diff --git a/src/Dapr.Client/InvocationHandler.cs b/src/Dapr.Client/InvocationHandler.cs index 1b55436aa..36fd6b77f 100644 --- a/src/Dapr.Client/InvocationHandler.cs +++ b/src/Dapr.Client/InvocationHandler.cs @@ -51,7 +51,7 @@ public class InvocationHandler : DelegatingHandler public InvocationHandler() { this.parsedEndpoint = new Uri(DaprDefaults.GetDefaultHttpEndpoint(), UriKind.Absolute); - this.apiToken = DaprDefaults.GetDefaultDaprApiToken(); + this.apiToken = DaprDefaults.GetDefaultDaprApiToken(null); } /// diff --git a/src/Dapr.Common/Dapr.Common.csproj b/src/Dapr.Common/Dapr.Common.csproj index ea3e8ae84..e9af8681d 100644 --- a/src/Dapr.Common/Dapr.Common.csproj +++ b/src/Dapr.Common/Dapr.Common.csproj @@ -9,6 +9,7 @@ + diff --git a/src/Dapr.Common/DaprDefaults.cs b/src/Dapr.Common/DaprDefaults.cs index fd0da9e3b..85a4b18c8 100644 --- a/src/Dapr.Common/DaprDefaults.cs +++ b/src/Dapr.Common/DaprDefaults.cs @@ -11,6 +11,8 @@ // limitations under the License. // ------------------------------------------------------------------------ +using Microsoft.Extensions.Configuration; + namespace Dapr { internal static class DaprDefaults @@ -27,88 +29,104 @@ internal static class DaprDefaults public const string DaprGrpcEndpointName = "DAPR_GRPC_ENDPOINT"; public const string DaprGrpcPortName = "DAPR_GRPC_PORT"; - public const string DaprHostName = "localhost"; + public const string DefaultDaprScheme = "http"; + public const string DefaultDaprHost = "localhost"; public const int DefaultHttpPort = 3500; public const int DefaultGrpcPort = 50001; /// /// Get the value of environment variable DAPR_API_TOKEN /// + /// The optional to pull the value from. /// The value of environment variable DAPR_API_TOKEN - public static string GetDefaultDaprApiToken() - { - // Lazy-init is safe because this is just populating the default - // We don't plan to support the case where the user changes environment variables - // for a running process. - if (string.IsNullOrEmpty(daprApiToken)) - { - // Treat empty the same as null since it's an environment variable - var value = Environment.GetEnvironmentVariable(DaprApiTokenName); - daprApiToken = string.IsNullOrEmpty(value) ? string.Empty : value; - } - - return daprApiToken; - } + public static string GetDefaultDaprApiToken(IConfiguration? configuration) => + GetResourceValue(configuration, DaprApiTokenName) ?? string.Empty; /// /// Get the value of environment variable APP_API_TOKEN /// + /// The optional to pull the value from. /// The value of environment variable APP_API_TOKEN - public static string GetDefaultAppApiToken() - { - if (string.IsNullOrEmpty(appApiToken)) - { - var value = Environment.GetEnvironmentVariable(AppApiTokenName); - appApiToken = string.IsNullOrEmpty(value) ? string.Empty : value; - } - - return appApiToken; - } + public static string GetDefaultAppApiToken(IConfiguration? configuration) => + GetResourceValue(configuration, AppApiTokenName) ?? string.Empty; /// /// Get the value of HTTP endpoint based off environment variables /// + /// The optional to pull the value from. /// The value of HTTP endpoint based off environment variables - public static string GetDefaultHttpEndpoint() + public static string GetDefaultHttpEndpoint(IConfiguration? configuration = null) { - if (string.IsNullOrEmpty(httpEndpoint)) - { - var endpoint = Environment.GetEnvironmentVariable(DaprHttpEndpointName); - if (!string.IsNullOrEmpty(endpoint)) - { - httpEndpoint = endpoint; - return httpEndpoint; - } + //Prioritize pulling from the IConfiguration and fallback to the environment variable if not populated + var endpoint = GetResourceValue(configuration, DaprHttpEndpointName); + var port = GetResourceValue(configuration, DaprHttpPortName); + + //Use the default HTTP port if we're unable to retrieve/parse the provided port + int? parsedGrpcPort = string.IsNullOrWhiteSpace(port) ? DefaultHttpPort : int.Parse(port); - var port = Environment.GetEnvironmentVariable(DaprHttpPortName); - port = string.IsNullOrEmpty(port) ? $"{DefaultHttpPort}" : port; - httpEndpoint = $"http://127.0.0.1:{port}"; - } - - return httpEndpoint; + return BuildEndpoint(endpoint, parsedGrpcPort.Value); } /// /// Get the value of gRPC endpoint based off environment variables /// + /// The optional to pull the value from. /// The value of gRPC endpoint based off environment variables - public static string GetDefaultGrpcEndpoint() + public static string GetDefaultGrpcEndpoint(IConfiguration? configuration = null) { - if (string.IsNullOrEmpty(grpcEndpoint)) - { - var endpoint = Environment.GetEnvironmentVariable(DaprGrpcEndpointName); - if (!string.IsNullOrEmpty(endpoint)) - { - grpcEndpoint = endpoint; - return grpcEndpoint; - } + //Prioritize pulling from the IConfiguration and fallback to the environment variable if not populated + var endpoint = GetResourceValue(configuration, DaprGrpcEndpointName); + var port = GetResourceValue(configuration, DaprGrpcPortName); + + //Use the default gRPC port if we're unable to retrieve/parse the provided port + int? parsedGrpcPort = string.IsNullOrWhiteSpace(port) ? DefaultGrpcPort : int.Parse(port); - var port = Environment.GetEnvironmentVariable(DaprGrpcPortName); - port = string.IsNullOrEmpty(port) ? $"{DefaultGrpcPort}" : port; - grpcEndpoint = $"http://127.0.0.1:{port}"; + return BuildEndpoint(endpoint, parsedGrpcPort.Value); + } + + /// + /// Builds the Dapr endpoint. + /// + /// The endpoint value. + /// The endpoint port value, whether pulled from configuration/envvar or the default. + /// A constructed endpoint value. + private static string BuildEndpoint(string? endpoint, int endpointPort) + { + var endpointBuilder = new UriBuilder { Scheme = DefaultDaprScheme, Host = DefaultDaprHost }; //Port depends on endpoint + + if (!string.IsNullOrWhiteSpace(endpoint)) //If the endpoint is set, it doesn't matter if the port is + { + //Extract the scheme, host and port from the endpoint and replace defaults + var uri = new Uri(endpoint); + endpointBuilder.Scheme = uri.Scheme; + endpointBuilder.Host = uri.Host; + endpointBuilder.Port = uri.Port; } + else + { + //Should only set the port if the endpoint isn't populated + endpointBuilder.Port = endpointPort; + } + + return endpointBuilder.ToString(); + } + + /// + /// Retrieves the specified value prioritizing pulling it from , falling back + /// to an environment variable, and using an empty string as a default. + /// + /// An instance of an . + /// The name of the value to retrieve. + /// The value of the resource. + private static string? GetResourceValue(IConfiguration? configuration, string name) + { + //Attempt to retrieve first from the configuration + var configurationValue = configuration?[name]; + if (configurationValue is not null) + return configurationValue; - return grpcEndpoint; + //Fall back to the environment variable with the same name or default to an empty string + return Environment.GetEnvironmentVariable(name); } } } diff --git a/test/Dapr.AspNetCore.Test/DaprServiceCollectionExtensionsTest.cs b/test/Dapr.AspNetCore.Test/DaprServiceCollectionExtensionsTest.cs index 74e988a96..4a340e22a 100644 --- a/test/Dapr.AspNetCore.Test/DaprServiceCollectionExtensionsTest.cs +++ b/test/Dapr.AspNetCore.Test/DaprServiceCollectionExtensionsTest.cs @@ -16,7 +16,6 @@ using System; using System.Text.Json; using Dapr.Client; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Xunit; @@ -78,606 +77,6 @@ public void AddDaprClient_RegistersUsingDependencyFromIServiceProvider() Assert.False(client?.JsonSerializerOptions.PropertyNameCaseInsensitive); } - [Fact] - public void ShouldBuildHttpEndpointAndPortUsingPrefixedConfiguration() - { - const string endpointVarName = "test_DAPR_HTTP_ENDPOINT"; - const string portVarName = "test_DAPR_HTTP_PORT"; - var original_HttpEndpoint = Environment.GetEnvironmentVariable(endpointVarName); - var original_HttpPort = Environment.GetEnvironmentVariable(portVarName); - - try - { - const string prefix = "test_"; - - Environment.SetEnvironmentVariable(endpointVarName, "https://dapr.io"); - Environment.SetEnvironmentVariable(portVarName, null); //Will use 443 from the endpoint instead - - var configurationBuilder = new ConfigurationBuilder(); - configurationBuilder.AddEnvironmentVariables(prefix); - var configuration = configurationBuilder.Build(); - - var httpEndpoint = DaprServiceCollectionExtensions.GetHttpEndpoint(configuration); - Assert.Equal("https://dapr.io:443/", httpEndpoint); - } - finally - { - //Restore - Environment.SetEnvironmentVariable(endpointVarName, original_HttpEndpoint); - Environment.SetEnvironmentVariable(portVarName, original_HttpPort); - } - } - - [Fact] - public void ShouldBuildHttpEndpointAndPortUsingConfiguration() - { - const string endpointVarName = "DAPR_HTTP_ENDPOINT"; - const string portVarName = "DAPR_HTTP_PORT"; - var original_HttpEndpoint = Environment.GetEnvironmentVariable(endpointVarName); - var original_HttpPort = Environment.GetEnvironmentVariable(portVarName); - - try - { - Environment.SetEnvironmentVariable(endpointVarName, "https://dapr.io"); - Environment.SetEnvironmentVariable(portVarName, null); //Will use 443 from the endpoint instead - - var configurationBuilder = new ConfigurationBuilder(); - configurationBuilder.AddEnvironmentVariables(); - var configuration = configurationBuilder.Build(); - - var httpEndpoint = DaprServiceCollectionExtensions.GetHttpEndpoint(configuration); - Assert.Equal("https://dapr.io:443/", httpEndpoint); - } - finally - { - //Restore - Environment.SetEnvironmentVariable(endpointVarName, original_HttpEndpoint); - Environment.SetEnvironmentVariable(portVarName, original_HttpPort); - } - } - - [Fact] - public void ShouldBuildHttpEndpointUsingPrefixedConfiguration() - { - const string endpointVarName = "test_DAPR_HTTP_ENDPOINT"; - const string portVarName = "test_DAPR_HTTP_PORT"; - var original_HttpEndpoint = Environment.GetEnvironmentVariable(endpointVarName); - var original_HttpPort = Environment.GetEnvironmentVariable(portVarName); - - try - { - const string prefix = "test_"; - - Environment.SetEnvironmentVariable(endpointVarName, "https://dapr.io"); - Environment.SetEnvironmentVariable(portVarName, "2569"); - - var configurationBuilder = new ConfigurationBuilder(); - configurationBuilder.AddEnvironmentVariables(prefix); - var configuration = configurationBuilder.Build(); - - var httpEndpoint = DaprServiceCollectionExtensions.GetHttpEndpoint(configuration); - Assert.Equal("https://dapr.io:443/", httpEndpoint); - } - finally - { - //Restore - Environment.SetEnvironmentVariable(endpointVarName, original_HttpEndpoint); - Environment.SetEnvironmentVariable(portVarName, original_HttpPort); - } - } - - [Fact] - public void ShouldBuildHttpEndpointUsingConfiguration() - { - const string endpointVarName = "DAPR_HTTP_ENDPOINT"; - const string portVarName = "DAPR_HTTP_PORT"; - var original_HttpEndpoint = Environment.GetEnvironmentVariable(endpointVarName); - var original_HttpPort = Environment.GetEnvironmentVariable(portVarName); - - try - { - Environment.SetEnvironmentVariable(endpointVarName, "https://dapr.io"); - Environment.SetEnvironmentVariable(portVarName, "2569"); - - var configurationBuilder = new ConfigurationBuilder(); - configurationBuilder.AddEnvironmentVariables(); - var configuration = configurationBuilder.Build(); - - var httpEndpoint = DaprServiceCollectionExtensions.GetHttpEndpoint(configuration); - Assert.Equal("https://dapr.io:443/", httpEndpoint); - } - finally - { - //Restore - Environment.SetEnvironmentVariable(endpointVarName, original_HttpEndpoint); - Environment.SetEnvironmentVariable(portVarName, original_HttpPort); - } - } - - [Fact] - public void ShouldBuildHttpEndpointUsingOnlyPortConfiguration() - { - const string portVarName = "DAPR_HTTP_PORT"; - var original_HttpPort = Environment.GetEnvironmentVariable(portVarName); - - try - { - Environment.SetEnvironmentVariable(portVarName, "2569"); - - var configurationBuilder = new ConfigurationBuilder(); - configurationBuilder.AddEnvironmentVariables(); - var configuration = configurationBuilder.Build(); - - var httpEndpoint = DaprServiceCollectionExtensions.GetHttpEndpoint(configuration); - Assert.Equal("http://localhost:2569/", httpEndpoint); - } - finally - { - //Restore - Environment.SetEnvironmentVariable(portVarName, original_HttpPort); - } - } - - [Fact] - public void ShouldBuildHttpEndpointUsingEnvVarValues() - { - const string endpointVarName = "DAPR_HTTP_ENDPOINT"; - const string portVarName = "DAPR_HTTP_PORT"; - var original_HttpEndpoint = Environment.GetEnvironmentVariable(endpointVarName); - var original_HttpPort = Environment.GetEnvironmentVariable(portVarName); - - try - { - Environment.SetEnvironmentVariable(endpointVarName, "http://dapr.io"); - Environment.SetEnvironmentVariable(portVarName, "2569"); - - var configurationBuilder = new ConfigurationBuilder(); - var configuration = configurationBuilder.Build(); - - var httpEndpoint = DaprServiceCollectionExtensions.GetHttpEndpoint(configuration); - Assert.Equal("http://dapr.io:80/", httpEndpoint); - } - finally - { - //Restore - Environment.SetEnvironmentVariable(endpointVarName, original_HttpEndpoint); - Environment.SetEnvironmentVariable(portVarName, original_HttpPort); - } - } - - [Fact] - public void ShouldBuildHttpEndpointUsingMixedValues() - { - const string endpointVarName = "test_DAPR_HTTP_ENDPOINT"; - const string portVarName = "DAPR_HTTP_PORT"; - var original_HttpEndpoint = Environment.GetEnvironmentVariable(endpointVarName); - var original_HttpPort = Environment.GetEnvironmentVariable(portVarName); - - try - { - Environment.SetEnvironmentVariable(endpointVarName, "https://dapr.io"); - Environment.SetEnvironmentVariable(portVarName, "2569"); - - var configurationBuilder = new ConfigurationBuilder(); - configurationBuilder.AddEnvironmentVariables(); - configurationBuilder.AddEnvironmentVariables("test_"); - var configuration = configurationBuilder.Build(); - - var httpEndpoint = DaprServiceCollectionExtensions.GetHttpEndpoint(configuration); - Assert.Equal("https://dapr.io:443/", httpEndpoint); - } - finally - { - //Restore - Environment.SetEnvironmentVariable(endpointVarName, original_HttpEndpoint); - Environment.SetEnvironmentVariable(portVarName, original_HttpPort); - } - } - - [Fact] - public void ShouldDefaultToEmptyHttpEndpoint() - { - const string endpointVarName = "DAPR_HTTP_ENDPOINT"; - const string portVarName = "DAPR_HTTP_PORT"; - var original_HttpEndpoint = Environment.GetEnvironmentVariable(endpointVarName); - var original_HttpPort = Environment.GetEnvironmentVariable(portVarName); - - try - { - Environment.SetEnvironmentVariable(endpointVarName, null); - Environment.SetEnvironmentVariable(portVarName, null); - - var configurationBuilder = new ConfigurationBuilder(); - var configuration = configurationBuilder.Build(); - - var httpEndpoint = DaprServiceCollectionExtensions.GetHttpEndpoint(configuration); - Assert.Equal("http://localhost:3500/", httpEndpoint); - } - finally - { - //Restore - Environment.SetEnvironmentVariable(endpointVarName, original_HttpEndpoint); - Environment.SetEnvironmentVariable(portVarName, original_HttpPort); - } - } - - [Fact] - public void ShouldDefaultToLocalhostWithPort() - { - const string endpointVarName = "DAPR_HTTP_ENDPOINT"; - const string portVarName = "DAPR_HTTP_PORT"; - var original_HttpEndpoint = Environment.GetEnvironmentVariable(endpointVarName); - var original_HttpPort = Environment.GetEnvironmentVariable(portVarName); - - try - { - Environment.SetEnvironmentVariable(endpointVarName, null); - Environment.SetEnvironmentVariable(portVarName, "7256"); - - var configurationBuilder = new ConfigurationBuilder(); - var configuration = configurationBuilder.Build(); - - var httpEndpoint = DaprServiceCollectionExtensions.GetHttpEndpoint(configuration); - Assert.Equal("http://localhost:7256/", httpEndpoint); - } - finally - { - //Restore - Environment.SetEnvironmentVariable(endpointVarName, original_HttpEndpoint); - Environment.SetEnvironmentVariable(portVarName, original_HttpPort); - } - } - - [Fact] - public void ShouldDefaultToLocalhostWithDefaultPort() - { - const string endpointVarName = "DAPR_HTTP_ENDPOINT"; - const string portVarName = "DAPR_HTTP_PORT"; - var original_HttpEndpoint = Environment.GetEnvironmentVariable(endpointVarName); - var original_HttpPort = Environment.GetEnvironmentVariable(portVarName); - - try - { - Environment.SetEnvironmentVariable(endpointVarName, null); - Environment.SetEnvironmentVariable(portVarName, null); - - var configurationBuilder = new ConfigurationBuilder(); - var configuration = configurationBuilder.Build(); - - var httpEndpoint = DaprServiceCollectionExtensions.GetHttpEndpoint(configuration); - Assert.Equal("http://localhost:3500/", httpEndpoint); - } - finally - { - //Restore - Environment.SetEnvironmentVariable(endpointVarName, original_HttpEndpoint); - Environment.SetEnvironmentVariable(portVarName, original_HttpPort); - } - } - - [Fact] - public void ShouldBuildGrpcEndpointAndPortUsingPrefixedConfiguration() - { - const string endpointVarName = "test_DAPR_GRPC_ENDPOINT"; - const string portVarName = "test_DAPR_GRPC_PORT"; - var original_GrpcEndpoint = Environment.GetEnvironmentVariable(endpointVarName); - var original_GrpcPort = Environment.GetEnvironmentVariable(portVarName); - - try - { - const string prefix = "test_"; - - Environment.SetEnvironmentVariable(endpointVarName, "https://grpc.dapr.io"); - Environment.SetEnvironmentVariable(portVarName, "2570"); - - var configurationBuilder = new ConfigurationBuilder(); - configurationBuilder.AddEnvironmentVariables(prefix); - var configuration = configurationBuilder.Build(); - - var httpEndpoint = DaprServiceCollectionExtensions.GetGrpcEndpoint(configuration); - Assert.Equal("https://grpc.dapr.io:443/", httpEndpoint); - } - finally - { - //Restore - Environment.SetEnvironmentVariable(endpointVarName, original_GrpcEndpoint); - Environment.SetEnvironmentVariable(portVarName, original_GrpcPort); - } - } - - [Fact] - public void ShouldBuildGrpcEndpointAndPortUsingConfiguration() - { - const string endpointVarName = "DAPR_GRPC_ENDPOINT"; - const string portVarName = "DAPR_GRPC_PORT"; - var original_GrpcEndpoint = Environment.GetEnvironmentVariable(endpointVarName); - var original_GrpcPort = Environment.GetEnvironmentVariable(portVarName); - - try - { - Environment.SetEnvironmentVariable(endpointVarName, "https://grpc.dapr.io"); - Environment.SetEnvironmentVariable(portVarName, null); //Will use 443 from the endpoint value instead - - var configurationBuilder = new ConfigurationBuilder(); - configurationBuilder.AddEnvironmentVariables(); - var configuration = configurationBuilder.Build(); - - var httpEndpoint = DaprServiceCollectionExtensions.GetGrpcEndpoint(configuration); - Assert.Equal("https://grpc.dapr.io:443/", httpEndpoint); - } - finally - { - //Restore - Environment.SetEnvironmentVariable(endpointVarName, original_GrpcEndpoint); - Environment.SetEnvironmentVariable(portVarName, original_GrpcPort); - } - } - - [Fact] - public void ShouldBuildGrpcEndpointUsingPrefixedConfiguration() - { - const string endpointVarName = "test_DAPR_GRPC_ENDPOINT"; - var original_GrpcEndpoint = Environment.GetEnvironmentVariable(endpointVarName); - - try - { - const string prefix = "test_"; - - Environment.SetEnvironmentVariable(endpointVarName, "https://grpc.dapr.io"); - - var configurationBuilder = new ConfigurationBuilder(); - configurationBuilder.AddEnvironmentVariables(prefix); - var configuration = configurationBuilder.Build(); - - var httpEndpoint = DaprServiceCollectionExtensions.GetGrpcEndpoint(configuration); - Assert.Equal("https://grpc.dapr.io:443/", httpEndpoint); - } - finally - { - //Restore - Environment.SetEnvironmentVariable(endpointVarName, original_GrpcEndpoint); - } - } - - [Fact] - public void ShouldBuildGrpcEndpointUsingConfiguration() - { - const string endpointVarName = "DAPR_GRPC_ENDPOINT"; - const string portVarName = "DAPR_GRPC_PORT"; - var original_GrpcEndpoint = Environment.GetEnvironmentVariable(endpointVarName); - var original_GrpcPort = Environment.GetEnvironmentVariable(portVarName); - - try - { - Environment.SetEnvironmentVariable(endpointVarName, "https://grpc.dapr.io"); - Environment.SetEnvironmentVariable(portVarName, null); //Will use 443 from the endpoint value instead - - var configurationBuilder = new ConfigurationBuilder(); - configurationBuilder.AddEnvironmentVariables(); - var configuration = configurationBuilder.Build(); - - var httpEndpoint = DaprServiceCollectionExtensions.GetGrpcEndpoint(configuration); - Assert.Equal("https://grpc.dapr.io:443/", httpEndpoint); - } - finally - { - //Restore - Environment.SetEnvironmentVariable(endpointVarName, original_GrpcEndpoint); - Environment.SetEnvironmentVariable(portVarName, original_GrpcPort); - } - } - - [Fact] - public void ShouldBuildGrpcEndpointAndPortUsingEnvVarValues() - { - const string endpointVarName = "DAPR_GRPC_ENDPOINT"; - const string portVarName = "DAPR_GRPC_PORT"; - var original_GrpcEndpoint = Environment.GetEnvironmentVariable(endpointVarName); - var original_GrpcPort = Environment.GetEnvironmentVariable(portVarName); - - try - { - Environment.SetEnvironmentVariable(endpointVarName, "https://grpc.dapr.io"); - Environment.SetEnvironmentVariable(portVarName, "4744"); //Will use 443 from the endpoint value instead - - var configurationBuilder = new ConfigurationBuilder(); - var configuration = configurationBuilder.Build(); - - var httpEndpoint = DaprServiceCollectionExtensions.GetGrpcEndpoint(configuration); - Assert.Equal("https://grpc.dapr.io:443/", httpEndpoint); - } - finally - { - //Restore - Environment.SetEnvironmentVariable(endpointVarName, original_GrpcEndpoint); - Environment.SetEnvironmentVariable(portVarName, original_GrpcPort); - } - } - - [Fact] - public void ShouldBuildGrpcEndpointUsingEnvVarValues() - { - const string endpointVarName = "DAPR_GRPC_ENDPOINT"; - const string portVarName = "DAPR_GRPC_PORT"; - var original_GrpcEndpoint = Environment.GetEnvironmentVariable(endpointVarName); - var original_GrpcPort = Environment.GetEnvironmentVariable(portVarName); - - try - { - Environment.SetEnvironmentVariable(endpointVarName, "https://grpc.dapr.io"); - Environment.SetEnvironmentVariable(portVarName, null); //Will use 443 from the endpoint value instead - - var configurationBuilder = new ConfigurationBuilder(); - var configuration = configurationBuilder.Build(); - - var httpEndpoint = DaprServiceCollectionExtensions.GetGrpcEndpoint(configuration); - Assert.Equal("https://grpc.dapr.io:443/", httpEndpoint); - } - finally - { - //Restore - Environment.SetEnvironmentVariable(endpointVarName, original_GrpcEndpoint); - Environment.SetEnvironmentVariable(portVarName, original_GrpcPort); - } - } - - [Fact] - public void ShouldBuildGrpcEndpointDefaultToLocalhostWithPort() - { - const string endpointVarName = "DAPR_GRPC_ENDPOINT"; - const string portVarName = "DAPR_GRPC_PORT"; - var original_grpcEndpoint = Environment.GetEnvironmentVariable(endpointVarName); - var original_grpcPort = Environment.GetEnvironmentVariable(portVarName); - - try - { - Environment.SetEnvironmentVariable(endpointVarName, null); - Environment.SetEnvironmentVariable(portVarName, "7256"); - - var configurationBuilder = new ConfigurationBuilder(); - var configuration = configurationBuilder.Build(); - - var httpEndpoint = DaprServiceCollectionExtensions.GetGrpcEndpoint(configuration); - Assert.Equal("http://localhost:7256/", httpEndpoint); - } - finally - { - //Restore - Environment.SetEnvironmentVariable(endpointVarName, original_grpcEndpoint); - Environment.SetEnvironmentVariable(portVarName, original_grpcPort); - } - } - - [Fact] - public void ShouldBuildGrpcEndpointDefaultToLocalhostWithDefaultPort() - { - const string endpointVarName = "DAPR_GRPC_ENDPOINT"; - const string portVarName = "DAPR_GRPC_PORT"; - var original_grpcEndpoint = Environment.GetEnvironmentVariable(endpointVarName); - var original_grpcPort = Environment.GetEnvironmentVariable(portVarName); - - try - { - Environment.SetEnvironmentVariable(endpointVarName, null); - Environment.SetEnvironmentVariable(portVarName, null); - - var configurationBuilder = new ConfigurationBuilder(); - var configuration = configurationBuilder.Build(); - - var httpEndpoint = DaprServiceCollectionExtensions.GetGrpcEndpoint(configuration); - Assert.Equal("http://localhost:50001/", httpEndpoint); - } - finally - { - //Restore - Environment.SetEnvironmentVariable(endpointVarName, original_grpcEndpoint); - Environment.SetEnvironmentVariable(portVarName, original_grpcPort); - } - } - - [Fact] - public void ShouldBuildApiTokenUsingConfiguration() - { - const string envVarName = "DAPR_API_TOKEN"; - var original_ApiToken = Environment.GetEnvironmentVariable(envVarName); - - try - { - const string apiToken = "abc123"; - Environment.SetEnvironmentVariable(envVarName, apiToken); - - var configurationBuilder = new ConfigurationBuilder(); - configurationBuilder.AddEnvironmentVariables(); - var configuration = configurationBuilder.Build(); - - var testApiToken = DaprServiceCollectionExtensions.GetApiToken(configuration); - Assert.Equal(apiToken, testApiToken); - } - finally - { - //Restore - Environment.SetEnvironmentVariable(envVarName, original_ApiToken); - } - } - - [Fact] - public void ShouldBuildApiTokenUsingPrefixedConfiguration() - { - const string envVarName = "test_DAPR_API_TOKEN"; - var original_ApiToken = Environment.GetEnvironmentVariable(envVarName); - - try - { - const string prefix = "test_"; - - const string apiToken = "abc123"; - Environment.SetEnvironmentVariable(envVarName, apiToken); - - var configurationBuilder = new ConfigurationBuilder(); - configurationBuilder.AddEnvironmentVariables(prefix); - var configuration = configurationBuilder.Build(); - - var testApiToken = DaprServiceCollectionExtensions.GetApiToken(configuration); - Assert.Equal(apiToken, testApiToken); - } - finally - { - //Restore - Environment.SetEnvironmentVariable(envVarName, original_ApiToken); - } - } - - [Fact] - public void ShouldBuildApiTokenWithEnvVarWhenConfigurationNotAvailable() - { - const string envVarName = "DAPR_API_TOKEN"; - var original_ApiToken = Environment.GetEnvironmentVariable(envVarName); - const string apiToken = "abc123"; - Environment.SetEnvironmentVariable(envVarName, apiToken); - - try - { - var configurationBuilder = new ConfigurationBuilder(); - configurationBuilder.AddEnvironmentVariables(); - var configuration = configurationBuilder.Build(); - - var testApiToken = DaprServiceCollectionExtensions.GetApiToken(configuration); - Assert.Equal(apiToken, testApiToken); - } - finally - { - //Restore - Environment.SetEnvironmentVariable(envVarName, original_ApiToken); - } - } - - [Fact] - public void BuildEndpoint_WithOnlyHttpsEndpoint() - { - var output = DaprServiceCollectionExtensions.BuildEndpoint("https://dapr.io", null); - Assert.Equal("https://dapr.io:443/", output); - } - - [Fact] - public void BuildEndpoint_WithOnlyHttpEndpoint() - { - var output = DaprServiceCollectionExtensions.BuildEndpoint("http://dapr.io", null); - Assert.Equal("http://dapr.io:80/", output); - } - - [Fact] - public void BuildEndpoint_WithEndpointAndPort() - { - var output = DaprServiceCollectionExtensions.BuildEndpoint("https://dapr.io", 3658); - Assert.Equal("https://dapr.io:443/", output); - } - - [Fact] - public void BuildEndpoint_WithOnlyPort() - { - var output = DaprServiceCollectionExtensions.BuildEndpoint(null, 3658); - Assert.Equal("http://localhost:3658/", output); - } #if NET8_0_OR_GREATER [Fact] diff --git a/test/Dapr.Common.Test/Dapr.Common.Test.csproj b/test/Dapr.Common.Test/Dapr.Common.Test.csproj new file mode 100644 index 000000000..6e34c3a7a --- /dev/null +++ b/test/Dapr.Common.Test/Dapr.Common.Test.csproj @@ -0,0 +1,23 @@ + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/test/Dapr.Common.Test/DaprDefaultTest.cs b/test/Dapr.Common.Test/DaprDefaultTest.cs new file mode 100644 index 000000000..ef4d0da3c --- /dev/null +++ b/test/Dapr.Common.Test/DaprDefaultTest.cs @@ -0,0 +1,582 @@ +using System; +using Microsoft.Extensions.Configuration; +using Xunit; + +namespace Dapr.Common.Test; + +public class DaprDefaultTest +{ + [Fact] + public void ShouldBuildHttpEndpointAndPortUsingPrefixedConfiguration() + { + const string endpointVarName = "test_DAPR_HTTP_ENDPOINT"; + const string portVarName = "test_DAPR_HTTP_PORT"; + var original_HttpEndpoint = Environment.GetEnvironmentVariable(endpointVarName); + var original_HttpPort = Environment.GetEnvironmentVariable(portVarName); + + try + { + const string prefix = "test_"; + + Environment.SetEnvironmentVariable(endpointVarName, "https://dapr.io"); + Environment.SetEnvironmentVariable(portVarName, null); //Will use 443 from the endpoint instead + + var configurationBuilder = new ConfigurationBuilder(); + configurationBuilder.AddEnvironmentVariables(prefix); + var configuration = configurationBuilder.Build(); + + var httpEndpoint = DaprDefaults.GetDefaultHttpEndpoint(configuration); + Assert.Equal("https://dapr.io:443/", httpEndpoint); + } + finally + { + //Restore + Environment.SetEnvironmentVariable(endpointVarName, original_HttpEndpoint); + Environment.SetEnvironmentVariable(portVarName, original_HttpPort); + } + } + + [Fact] + public void ShouldBuildHttpEndpointAndPortUsingConfiguration() + { + const string endpointVarName = "DAPR_HTTP_ENDPOINT"; + const string portVarName = "DAPR_HTTP_PORT"; + var original_HttpEndpoint = Environment.GetEnvironmentVariable(endpointVarName); + var original_HttpPort = Environment.GetEnvironmentVariable(portVarName); + + try + { + Environment.SetEnvironmentVariable(endpointVarName, "https://dapr.io"); + Environment.SetEnvironmentVariable(portVarName, null); //Will use 443 from the endpoint instead + + var configurationBuilder = new ConfigurationBuilder(); + configurationBuilder.AddEnvironmentVariables(); + var configuration = configurationBuilder.Build(); + + var httpEndpoint = DaprDefaults.GetDefaultHttpEndpoint(configuration); + Assert.Equal("https://dapr.io:443/", httpEndpoint); + } + finally + { + //Restore + Environment.SetEnvironmentVariable(endpointVarName, original_HttpEndpoint); + Environment.SetEnvironmentVariable(portVarName, original_HttpPort); + } + } + + [Fact] + public void ShouldBuildHttpEndpointUsingPrefixedConfiguration() + { + const string endpointVarName = "test_DAPR_HTTP_ENDPOINT"; + const string portVarName = "test_DAPR_HTTP_PORT"; + var original_HttpEndpoint = Environment.GetEnvironmentVariable(endpointVarName); + var original_HttpPort = Environment.GetEnvironmentVariable(portVarName); + + try + { + const string prefix = "test_"; + + Environment.SetEnvironmentVariable(endpointVarName, "https://dapr.io"); + Environment.SetEnvironmentVariable(portVarName, "2569"); + + var configurationBuilder = new ConfigurationBuilder(); + configurationBuilder.AddEnvironmentVariables(prefix); + var configuration = configurationBuilder.Build(); + + var httpEndpoint = DaprDefaults.GetDefaultHttpEndpoint(configuration); + Assert.Equal("https://dapr.io:443/", httpEndpoint); + } + finally + { + //Restore + Environment.SetEnvironmentVariable(endpointVarName, original_HttpEndpoint); + Environment.SetEnvironmentVariable(portVarName, original_HttpPort); + } + } + + [Fact] + public void ShouldBuildHttpEndpointUsingConfiguration() + { + const string endpointVarName = "DAPR_HTTP_ENDPOINT"; + const string portVarName = "DAPR_HTTP_PORT"; + var original_HttpEndpoint = Environment.GetEnvironmentVariable(endpointVarName); + var original_HttpPort = Environment.GetEnvironmentVariable(portVarName); + + try + { + Environment.SetEnvironmentVariable(endpointVarName, "https://dapr.io"); + Environment.SetEnvironmentVariable(portVarName, "2569"); + + var configurationBuilder = new ConfigurationBuilder(); + configurationBuilder.AddEnvironmentVariables(); + var configuration = configurationBuilder.Build(); + + var httpEndpoint = DaprDefaults.GetDefaultHttpEndpoint(configuration); + Assert.Equal("https://dapr.io:443/", httpEndpoint); + } + finally + { + //Restore + Environment.SetEnvironmentVariable(endpointVarName, original_HttpEndpoint); + Environment.SetEnvironmentVariable(portVarName, original_HttpPort); + } + } + + [Fact] + public void ShouldBuildHttpEndpointUsingOnlyPortConfiguration() + { + const string portVarName = "DAPR_HTTP_PORT"; + var original_HttpPort = Environment.GetEnvironmentVariable(portVarName); + + try + { + Environment.SetEnvironmentVariable(portVarName, "2569"); + + var configurationBuilder = new ConfigurationBuilder(); + configurationBuilder.AddEnvironmentVariables(); + var configuration = configurationBuilder.Build(); + + var httpEndpoint = DaprDefaults.GetDefaultHttpEndpoint(configuration); + Assert.Equal("http://localhost:2569/", httpEndpoint); + } + finally + { + //Restore + Environment.SetEnvironmentVariable(portVarName, original_HttpPort); + } + } + + [Fact] + public void ShouldBuildHttpEndpointUsingEnvVarValues() + { + const string endpointVarName = "DAPR_HTTP_ENDPOINT"; + const string portVarName = "DAPR_HTTP_PORT"; + var original_HttpEndpoint = Environment.GetEnvironmentVariable(endpointVarName); + var original_HttpPort = Environment.GetEnvironmentVariable(portVarName); + + try + { + Environment.SetEnvironmentVariable(endpointVarName, "http://dapr.io"); + Environment.SetEnvironmentVariable(portVarName, "2569"); + + var configurationBuilder = new ConfigurationBuilder(); + var configuration = configurationBuilder.Build(); + + var httpEndpoint = DaprDefaults.GetDefaultHttpEndpoint(configuration); + Assert.Equal("http://dapr.io:80/", httpEndpoint); + } + finally + { + //Restore + Environment.SetEnvironmentVariable(endpointVarName, original_HttpEndpoint); + Environment.SetEnvironmentVariable(portVarName, original_HttpPort); + } + } + + [Fact] + public void ShouldBuildHttpEndpointUsingMixedValues() + { + const string endpointVarName = "test_DAPR_HTTP_ENDPOINT"; + const string portVarName = "DAPR_HTTP_PORT"; + var original_HttpEndpoint = Environment.GetEnvironmentVariable(endpointVarName); + var original_HttpPort = Environment.GetEnvironmentVariable(portVarName); + + try + { + Environment.SetEnvironmentVariable(endpointVarName, "https://dapr.io"); + Environment.SetEnvironmentVariable(portVarName, "2569"); + + var configurationBuilder = new ConfigurationBuilder(); + configurationBuilder.AddEnvironmentVariables(); + configurationBuilder.AddEnvironmentVariables("test_"); + var configuration = configurationBuilder.Build(); + + var httpEndpoint = DaprDefaults.GetDefaultHttpEndpoint(configuration); + Assert.Equal("https://dapr.io:443/", httpEndpoint); + } + finally + { + //Restore + Environment.SetEnvironmentVariable(endpointVarName, original_HttpEndpoint); + Environment.SetEnvironmentVariable(portVarName, original_HttpPort); + } + } + + [Fact] + public void ShouldDefaultToEmptyHttpEndpoint() + { + const string endpointVarName = "DAPR_HTTP_ENDPOINT"; + const string portVarName = "DAPR_HTTP_PORT"; + var original_HttpEndpoint = Environment.GetEnvironmentVariable(endpointVarName); + var original_HttpPort = Environment.GetEnvironmentVariable(portVarName); + + try + { + Environment.SetEnvironmentVariable(endpointVarName, null); + Environment.SetEnvironmentVariable(portVarName, null); + + var configurationBuilder = new ConfigurationBuilder(); + var configuration = configurationBuilder.Build(); + + var httpEndpoint = DaprDefaults.GetDefaultHttpEndpoint(configuration); + Assert.Equal("http://localhost:3500/", httpEndpoint); + } + finally + { + //Restore + Environment.SetEnvironmentVariable(endpointVarName, original_HttpEndpoint); + Environment.SetEnvironmentVariable(portVarName, original_HttpPort); + } + } + + [Fact] + public void ShouldDefaultToLocalhostWithPort() + { + const string endpointVarName = "DAPR_HTTP_ENDPOINT"; + const string portVarName = "DAPR_HTTP_PORT"; + var original_HttpEndpoint = Environment.GetEnvironmentVariable(endpointVarName); + var original_HttpPort = Environment.GetEnvironmentVariable(portVarName); + + try + { + Environment.SetEnvironmentVariable(endpointVarName, null); + Environment.SetEnvironmentVariable(portVarName, "7256"); + + var configurationBuilder = new ConfigurationBuilder(); + var configuration = configurationBuilder.Build(); + + var httpEndpoint = DaprDefaults.GetDefaultHttpEndpoint(configuration); + Assert.Equal("http://localhost:7256/", httpEndpoint); + } + finally + { + //Restore + Environment.SetEnvironmentVariable(endpointVarName, original_HttpEndpoint); + Environment.SetEnvironmentVariable(portVarName, original_HttpPort); + } + } + + [Fact] + public void ShouldDefaultToLocalhostWithDefaultPort() + { + const string endpointVarName = "DAPR_HTTP_ENDPOINT"; + const string portVarName = "DAPR_HTTP_PORT"; + var original_HttpEndpoint = Environment.GetEnvironmentVariable(endpointVarName); + var original_HttpPort = Environment.GetEnvironmentVariable(portVarName); + + try + { + Environment.SetEnvironmentVariable(endpointVarName, null); + Environment.SetEnvironmentVariable(portVarName, null); + + var configurationBuilder = new ConfigurationBuilder(); + var configuration = configurationBuilder.Build(); + + var httpEndpoint = DaprDefaults.GetDefaultHttpEndpoint(configuration); + Assert.Equal("http://localhost:3500/", httpEndpoint); + } + finally + { + //Restore + Environment.SetEnvironmentVariable(endpointVarName, original_HttpEndpoint); + Environment.SetEnvironmentVariable(portVarName, original_HttpPort); + } + } + + [Fact] + public void ShouldBuildGrpcEndpointAndPortUsingPrefixedConfiguration() + { + const string endpointVarName = "test_DAPR_GRPC_ENDPOINT"; + const string portVarName = "test_DAPR_GRPC_PORT"; + var original_GrpcEndpoint = Environment.GetEnvironmentVariable(endpointVarName); + var original_GrpcPort = Environment.GetEnvironmentVariable(portVarName); + + try + { + const string prefix = "test_"; + + Environment.SetEnvironmentVariable(endpointVarName, "https://grpc.dapr.io"); + Environment.SetEnvironmentVariable(portVarName, "2570"); + + var configurationBuilder = new ConfigurationBuilder(); + configurationBuilder.AddEnvironmentVariables(prefix); + var configuration = configurationBuilder.Build(); + + var grpcEndpoint = DaprDefaults.GetDefaultGrpcEndpoint(configuration); + Assert.Equal("https://grpc.dapr.io:443/", grpcEndpoint); + } + finally + { + //Restore + Environment.SetEnvironmentVariable(endpointVarName, original_GrpcEndpoint); + Environment.SetEnvironmentVariable(portVarName, original_GrpcPort); + } + } + + [Fact] + public void ShouldBuildGrpcEndpointAndPortUsingConfiguration() + { + const string endpointVarName = DaprDefaults.DaprGrpcEndpointName; + const string portVarName = DaprDefaults.DaprGrpcPortName; + var original_GrpcEndpoint = Environment.GetEnvironmentVariable(endpointVarName); + var original_GrpcPort = Environment.GetEnvironmentVariable(portVarName); + + try + { + Environment.SetEnvironmentVariable(endpointVarName, "https://grpc.dapr.io", EnvironmentVariableTarget.Process); + Environment.SetEnvironmentVariable(portVarName, null, EnvironmentVariableTarget.Process); //Will use 443 from the endpoint value instead + + var configurationBuilder = new ConfigurationBuilder(); + configurationBuilder.AddEnvironmentVariables(); + var configuration = configurationBuilder.Build(); + + var grpcEndpoint = DaprDefaults.GetDefaultGrpcEndpoint(configuration); + Assert.Equal("https://grpc.dapr.io:443/", grpcEndpoint); + } + finally + { + //Restore + Environment.SetEnvironmentVariable(endpointVarName, original_GrpcEndpoint); + Environment.SetEnvironmentVariable(portVarName, original_GrpcPort); + } + } + + [Fact] + public void ShouldBuildGrpcEndpointUsingPrefixedConfiguration() + { + const string endpointVarName = "test_DAPR_GRPC_ENDPOINT"; + var original_GrpcEndpoint = Environment.GetEnvironmentVariable(endpointVarName); + + try + { + const string prefix = "test_"; + + Environment.SetEnvironmentVariable(endpointVarName, "https://grpc.dapr.io"); + + var configurationBuilder = new ConfigurationBuilder(); + configurationBuilder.AddEnvironmentVariables(prefix); + var configuration = configurationBuilder.Build(); + + var grpcEndpoint = DaprDefaults.GetDefaultGrpcEndpoint(configuration); + Assert.Equal("https://grpc.dapr.io:443/", grpcEndpoint); + } + finally + { + //Restore + Environment.SetEnvironmentVariable(endpointVarName, original_GrpcEndpoint); + } + } + + [Fact] + public void ShouldBuildGrpcEndpointUsingConfiguration() + { + const string endpointVarName = "DAPR_GRPC_ENDPOINT"; + const string portVarName = "DAPR_GRPC_PORT"; + var original_GrpcEndpoint = Environment.GetEnvironmentVariable(endpointVarName); + var original_GrpcPort = Environment.GetEnvironmentVariable(portVarName); + + try + { + Environment.SetEnvironmentVariable(endpointVarName, "https://grpc.dapr.io"); + Environment.SetEnvironmentVariable(portVarName, null); //Will use 443 from the endpoint value instead + + var configurationBuilder = new ConfigurationBuilder(); + configurationBuilder.AddEnvironmentVariables(); + var configuration = configurationBuilder.Build(); + + var grpcEndpoint = DaprDefaults.GetDefaultGrpcEndpoint(configuration); + Assert.Equal("https://grpc.dapr.io:443/", grpcEndpoint); + } + finally + { + //Restore + Environment.SetEnvironmentVariable(endpointVarName, original_GrpcEndpoint); + Environment.SetEnvironmentVariable(portVarName, original_GrpcPort); + } + } + + [Fact] + public void ShouldBuildGrpcEndpointAndPortUsingEnvVarValues() + { + const string endpointVarName = "DAPR_GRPC_ENDPOINT"; + const string portVarName = "DAPR_GRPC_PORT"; + var original_GrpcEndpoint = Environment.GetEnvironmentVariable(endpointVarName); + var original_GrpcPort = Environment.GetEnvironmentVariable(portVarName); + + try + { + Environment.SetEnvironmentVariable(endpointVarName, "https://grpc.dapr.io"); + Environment.SetEnvironmentVariable(portVarName, "4744"); //Will use 443 from the endpoint value instead + + var configurationBuilder = new ConfigurationBuilder(); + var configuration = configurationBuilder.Build(); + + var grpcEndpoint = DaprDefaults.GetDefaultGrpcEndpoint(configuration); + Assert.Equal("https://grpc.dapr.io:443/", grpcEndpoint); + } + finally + { + //Restore + Environment.SetEnvironmentVariable(endpointVarName, original_GrpcEndpoint); + Environment.SetEnvironmentVariable(portVarName, original_GrpcPort); + } + } + + [Fact] + public void ShouldBuildGrpcEndpointUsingEnvVarValues() + { + const string endpointVarName = "DAPR_GRPC_ENDPOINT"; + const string portVarName = "DAPR_GRPC_PORT"; + var original_GrpcEndpoint = Environment.GetEnvironmentVariable(endpointVarName); + var original_GrpcPort = Environment.GetEnvironmentVariable(portVarName); + + try + { + Environment.SetEnvironmentVariable(endpointVarName, "https://grpc.dapr.io"); + Environment.SetEnvironmentVariable(portVarName, null); //Will use 443 from the endpoint value instead + + var configurationBuilder = new ConfigurationBuilder(); + var configuration = configurationBuilder.Build(); + + var grpcEndpoint = DaprDefaults.GetDefaultGrpcEndpoint(configuration); + Assert.Equal("https://grpc.dapr.io:443/", grpcEndpoint); + } + finally + { + //Restore + Environment.SetEnvironmentVariable(endpointVarName, original_GrpcEndpoint); + Environment.SetEnvironmentVariable(portVarName, original_GrpcPort); + } + } + + [Fact] + public void ShouldBuildGrpcEndpointDefaultToLocalhostWithPort() + { + const string endpointVarName = DaprDefaults.DaprGrpcEndpointName; + const string portVarName = DaprDefaults.DaprGrpcPortName; + var original_grpcEndpoint = Environment.GetEnvironmentVariable(endpointVarName); + var original_grpcPort = Environment.GetEnvironmentVariable(portVarName); + + try + { + Environment.SetEnvironmentVariable(endpointVarName, null); + Environment.SetEnvironmentVariable(portVarName, "7256"); + + var configurationBuilder = new ConfigurationBuilder(); + var configuration = configurationBuilder.Build(); + + var grpcEndpoint = DaprDefaults.GetDefaultGrpcEndpoint(configuration); + Assert.Equal($"{DaprDefaults.DefaultDaprScheme}://{DaprDefaults.DefaultDaprHost}:7256/", grpcEndpoint); + } + finally + { + //Restore + Environment.SetEnvironmentVariable(endpointVarName, original_grpcEndpoint); + Environment.SetEnvironmentVariable(portVarName, original_grpcPort); + } + } + + [Fact] + public void ShouldBuildGrpcEndpointDefaultToLocalhostWithDefaultPort() + { + const string endpointVarName = DaprDefaults.DaprGrpcEndpointName; + const string portVarName = DaprDefaults.DaprGrpcPortName; + var original_grpcEndpoint = Environment.GetEnvironmentVariable(endpointVarName); + var original_grpcPort = Environment.GetEnvironmentVariable(portVarName); + + try + { + Environment.SetEnvironmentVariable(endpointVarName, null); + Environment.SetEnvironmentVariable(portVarName, null); + + var configurationBuilder = new ConfigurationBuilder(); + var configuration = configurationBuilder.Build(); + + var grpcEndpoint = DaprDefaults.GetDefaultGrpcEndpoint(configuration); + Assert.Equal($"{DaprDefaults.DefaultDaprScheme}://{DaprDefaults.DefaultDaprHost}:{DaprDefaults.DefaultGrpcPort}/", grpcEndpoint); + } + finally + { + //Restore + Environment.SetEnvironmentVariable(endpointVarName, original_grpcEndpoint); + Environment.SetEnvironmentVariable(portVarName, original_grpcPort); + } + } + + [Fact] + public void ShouldBuildApiTokenUsingConfiguration() + { + const string envVarName = DaprDefaults.DaprApiTokenName; + var original_ApiToken = Environment.GetEnvironmentVariable(envVarName); + + try + { + const string apiToken = "abc123"; + Environment.SetEnvironmentVariable(envVarName, apiToken); + + var configurationBuilder = new ConfigurationBuilder(); + configurationBuilder.AddEnvironmentVariables(); + var configuration = configurationBuilder.Build(); + + var testApiToken = DaprDefaults.GetDefaultDaprApiToken(configuration); + Assert.Equal(apiToken, testApiToken); + } + finally + { + //Restore + Environment.SetEnvironmentVariable(envVarName, original_ApiToken); + } + } + + [Fact] + public void ShouldBuildApiTokenUsingPrefixedConfiguration() + { + + const string envVarName = $"test_{DaprDefaults.DaprApiTokenName}"; + var original_ApiToken = Environment.GetEnvironmentVariable(envVarName); + + try + { + const string prefix = "test_"; + + const string apiToken = "abc123"; + Environment.SetEnvironmentVariable(envVarName, apiToken); + + var configurationBuilder = new ConfigurationBuilder(); + configurationBuilder.AddEnvironmentVariables(prefix); + var configuration = configurationBuilder.Build(); + + var testApiToken = DaprDefaults.GetDefaultDaprApiToken(configuration); + Assert.Equal(apiToken, testApiToken); + } + finally + { + //Restore + Environment.SetEnvironmentVariable(envVarName, original_ApiToken); + } + } + + [Fact] + public void ShouldBuildApiTokenWithEnvVarWhenConfigurationNotAvailable() + { + const string envVarName = DaprDefaults.DaprApiTokenName; + var original_ApiToken = Environment.GetEnvironmentVariable(envVarName); + const string apiToken = "abc123"; + Environment.SetEnvironmentVariable(envVarName, apiToken); + + try + { + var configurationBuilder = new ConfigurationBuilder(); + configurationBuilder.AddEnvironmentVariables(); + var configuration = configurationBuilder.Build(); + + var testApiToken = DaprDefaults.GetDefaultDaprApiToken(configuration); + Assert.Equal(apiToken, testApiToken); + } + finally + { + //Restore + Environment.SetEnvironmentVariable(envVarName, original_ApiToken); + } + } +}