Skip to content

Commit

Permalink
Fixed and updated unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Whit Waldo <whit.waldo@innovian.net>
  • Loading branch information
WhitWaldo committed Oct 15, 2024
1 parent 616638f commit d386a5c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Dapr.AspNetCore/DaprServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
49 changes: 40 additions & 9 deletions test/Dapr.AspNetCore.Test/DaprServiceCollectionExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
{
Expand All @@ -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()
{
Expand All @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit d386a5c

Please sign in to comment.