-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: BrokerAddress property in AzureServiceBus transport (#1576)
* fixed getting the broker address from service bus * fixed namespace extraction
- Loading branch information
Showing
5 changed files
with
182 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/DotNetCore.CAP.AzureServiceBus/Helpers/ServiceBusHelpers.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
using DotNetCore.CAP.Transport; | ||
|
||
namespace DotNetCore.CAP.AzureServiceBus.Helpers; | ||
|
||
public static class ServiceBusHelpers | ||
{ | ||
public static BrokerAddress GetBrokerAddress(string? connectionString, string? @namespace) | ||
{ | ||
var host = (@namespace, connectionString) switch | ||
{ | ||
_ when string.IsNullOrWhiteSpace(@namespace) && string.IsNullOrWhiteSpace(connectionString) | ||
=> throw new ArgumentException("Either connection string or namespace are required."), | ||
_ when string.IsNullOrWhiteSpace(connectionString) | ||
|| (!string.IsNullOrWhiteSpace(@namespace) && !string.IsNullOrWhiteSpace(connectionString)) | ||
=> @namespace!, | ||
_ when string.IsNullOrWhiteSpace(@namespace) | ||
=> TryGetEndpointFromConnectionString(connectionString, out var extractedValue) | ||
? extractedValue! | ||
: throw new InvalidOperationException("Unable to extract namespace from connection string.") | ||
}; | ||
|
||
return new BrokerAddress("AzureServiceBus", host); | ||
} | ||
|
||
|
||
private static bool TryGetEndpointFromConnectionString(string? connectionString, out string? @namespace) | ||
{ | ||
@namespace = string.Empty; | ||
|
||
if (string.IsNullOrWhiteSpace(connectionString)) | ||
return false; | ||
|
||
var keyValuePairs = connectionString.Split(';'); | ||
|
||
foreach (var kvp in keyValuePairs) | ||
{ | ||
if (!kvp.StartsWith("Endpoint", StringComparison.InvariantCultureIgnoreCase)) continue; | ||
|
||
var endpointParts = kvp.Split('='); | ||
|
||
if (endpointParts.Length != 2) continue; | ||
|
||
var uri = new Uri(endpointParts[1]); | ||
|
||
// Namespace is the host part without the .servicebus.windows.net | ||
@namespace = uri.ToString(); | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
test/DotNetCore.CAP.AzureServiceBus.Test/Helpers/ServiceBusHelperTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
using System; | ||
using DotNetCore.CAP.AzureServiceBus.Helpers; | ||
using Xunit; | ||
|
||
namespace DotNetCore.CAP.AzureServiceBus.Test.Helpers; | ||
|
||
public class ServiceBusHelpersTests | ||
{ | ||
[Fact] | ||
public void GetBrokerAddress_ShouldThrowArgumentException_WhenBothInputsAreNull() | ||
{ | ||
// Arrange | ||
string? connectionString = null; | ||
string? @namespace = null; | ||
|
||
// Act & Assert | ||
var ex = Assert.Throws<ArgumentException>(() => ServiceBusHelpers.GetBrokerAddress(connectionString, @namespace)); | ||
Assert.Equal("Either connection string or namespace are required.", ex.Message); | ||
} | ||
|
||
[Fact] | ||
public void GetBrokerAddress_ShouldReturnNamespace_WhenConnectionStringIsNull() | ||
{ | ||
// Arrange | ||
string? connectionString = null; | ||
string? @namespace = "sb://mynamespace.servicebus.windows.net/"; | ||
|
||
// Act | ||
var result = ServiceBusHelpers.GetBrokerAddress(connectionString, @namespace); | ||
|
||
// Assert | ||
Assert.Equal("AzureServiceBus", result.Name); | ||
Assert.Equal("sb://mynamespace.servicebus.windows.net/", result.Endpoint); | ||
} | ||
|
||
[Fact] | ||
public void GetBrokerAddress_ShouldReturnExtractedNamespace_WhenNamespaceIsNull() | ||
{ | ||
// Arrange | ||
string? connectionString = "Endpoint=sb://mynamespace.servicebus.windows.net/;SharedAccessKeyName=myPolicy;SharedAccessKey=myKey"; | ||
string? @namespace = null; | ||
|
||
// Act | ||
var result = ServiceBusHelpers.GetBrokerAddress(connectionString, @namespace); | ||
|
||
// Assert | ||
Assert.Equal("AzureServiceBus", result.Name); | ||
Assert.Equal("sb://mynamespace.servicebus.windows.net/", result.Endpoint); | ||
} | ||
|
||
[Fact] | ||
public void GetBrokerAddress_ShouldThrowInvalidOperationException_WhenNamespaceExtractionFails() | ||
{ | ||
// Arrange | ||
string? connectionString = "InvalidConnectionString"; | ||
string? @namespace = null; | ||
|
||
// Act & Assert | ||
var ex = Assert.Throws<InvalidOperationException>(() => ServiceBusHelpers.GetBrokerAddress(connectionString, @namespace)); | ||
Assert.Equal("Unable to extract namespace from connection string.", ex.Message); | ||
} | ||
|
||
[Fact] | ||
public void GetBrokerAddress_ShouldReturnNamespace_WhenBothNamespaceAndConnectionStringAreProvided() | ||
{ | ||
// Arrange | ||
string? connectionString = "Endpoint=sb://mynamespace.servicebus.windows.net/;SharedAccessKeyName=myPolicy;SharedAccessKey=myKey"; | ||
string? @namespace = "anothernamespace"; | ||
|
||
// Act | ||
var result = ServiceBusHelpers.GetBrokerAddress(connectionString, @namespace); | ||
|
||
// Assert | ||
Assert.Equal("AzureServiceBus", result.Name); | ||
Assert.Equal("anothernamespace", result.Endpoint); | ||
} | ||
|
||
[Fact] | ||
public void GetBrokerAddress_ShouldReturnExtractedNamespace_WhenConnectionStringIsValidAndNamespaceIsEmpty() | ||
{ | ||
// Arrange | ||
string? connectionString = "Endpoint=sb://mynamespace.servicebus.windows.net/;SharedAccessKeyName=myPolicy;SharedAccessKey=myKey"; | ||
string? @namespace = ""; | ||
|
||
// Act | ||
var result = ServiceBusHelpers.GetBrokerAddress(connectionString, @namespace); | ||
|
||
// Assert | ||
Assert.Equal("AzureServiceBus", result.Name); | ||
Assert.Equal("sb://mynamespace.servicebus.windows.net/", result.Endpoint); | ||
} | ||
|
||
[Fact] | ||
public void GetBrokerAddress_ShouldReturnNamespace_WhenConnectionStringIsEmpty() | ||
{ | ||
// Arrange | ||
string? connectionString = ""; | ||
string? @namespace = "sb://mynamespace.servicebus.windows.net/"; | ||
|
||
// Act | ||
var result = ServiceBusHelpers.GetBrokerAddress(connectionString, @namespace); | ||
|
||
// Assert | ||
Assert.Equal("AzureServiceBus", result.Name); | ||
Assert.Equal("sb://mynamespace.servicebus.windows.net/", result.Endpoint); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters