diff --git a/src/Dapr.AspNetCore/DaprServiceCollectionExtensions.cs b/src/Dapr.AspNetCore/DaprServiceCollectionExtensions.cs index 5b22a46e0..c4dfff2ee 100644 --- a/src/Dapr.AspNetCore/DaprServiceCollectionExtensions.cs +++ b/src/Dapr.AspNetCore/DaprServiceCollectionExtensions.cs @@ -201,7 +201,7 @@ internal static string BuildEndpoint(string? endpoint, int? endpointPort) // 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) && endpointPort is null) + 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); diff --git a/test/Dapr.AspNetCore.Test/DaprServiceCollectionExtensionsTest.cs b/test/Dapr.AspNetCore.Test/DaprServiceCollectionExtensionsTest.cs index 8a61433b0..74e988a96 100644 --- a/test/Dapr.AspNetCore.Test/DaprServiceCollectionExtensionsTest.cs +++ b/test/Dapr.AspNetCore.Test/DaprServiceCollectionExtensionsTest.cs @@ -156,7 +156,7 @@ public void ShouldBuildHttpEndpointUsingPrefixedConfiguration() var configuration = configurationBuilder.Build(); var httpEndpoint = DaprServiceCollectionExtensions.GetHttpEndpoint(configuration); - Assert.Equal("https://dapr.io:2569/", httpEndpoint); + Assert.Equal("https://dapr.io:443/", httpEndpoint); } finally { @@ -184,7 +184,7 @@ public void ShouldBuildHttpEndpointUsingConfiguration() var configuration = configurationBuilder.Build(); var httpEndpoint = DaprServiceCollectionExtensions.GetHttpEndpoint(configuration); - Assert.Equal("https://dapr.io:2569/", httpEndpoint); + Assert.Equal("https://dapr.io:443/", httpEndpoint); } finally { @@ -194,6 +194,30 @@ public void ShouldBuildHttpEndpointUsingConfiguration() } } + [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() { @@ -204,14 +228,14 @@ public void ShouldBuildHttpEndpointUsingEnvVarValues() try { - Environment.SetEnvironmentVariable(endpointVarName, "https://dapr.io"); + 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("https://dapr.io:2569/", httpEndpoint); + Assert.Equal("http://dapr.io:80/", httpEndpoint); } finally { @@ -240,7 +264,7 @@ public void ShouldBuildHttpEndpointUsingMixedValues() var configuration = configurationBuilder.Build(); var httpEndpoint = DaprServiceCollectionExtensions.GetHttpEndpoint(configuration); - Assert.Equal("https://dapr.io:2569/", httpEndpoint); + Assert.Equal("https://dapr.io:443/", httpEndpoint); } finally { @@ -351,7 +375,7 @@ public void ShouldBuildGrpcEndpointAndPortUsingPrefixedConfiguration() var configuration = configurationBuilder.Build(); var httpEndpoint = DaprServiceCollectionExtensions.GetGrpcEndpoint(configuration); - Assert.Equal("https://grpc.dapr.io:2570/", httpEndpoint); + Assert.Equal("https://grpc.dapr.io:443/", httpEndpoint); } finally { @@ -460,7 +484,7 @@ public void ShouldBuildGrpcEndpointAndPortUsingEnvVarValues() var configuration = configurationBuilder.Build(); var httpEndpoint = DaprServiceCollectionExtensions.GetGrpcEndpoint(configuration); - Assert.Equal("https://grpc.dapr.io:4744/", httpEndpoint); + Assert.Equal("https://grpc.dapr.io:443/", httpEndpoint); } finally { @@ -628,17 +652,24 @@ public void ShouldBuildApiTokenWithEnvVarWhenConfigurationNotAvailable() } [Fact] - public void BuildEndpoint_WithOnlyEndpoint() + 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:3658/", output); + Assert.Equal("https://dapr.io:443/", output); } [Fact]