From 4848f7fa1aeadd8f11abb3c8d0d081cd2950b1cf Mon Sep 17 00:00:00 2001 From: Jesse Sweetland Date: Fri, 8 Apr 2016 16:46:34 -0500 Subject: [PATCH] EndpointCollection.TryGetEndpointByAddress and ReadOnlyEndpointCollection.TryGetEndpointByAddress now ignore query strings in endpoint URIs to enable endpoint credentials to be found despite additional query string parameters that may be supplied in Origination, Destination, or ReplyTo headers. --- .../HttpPubSubTests.cs | 8 +- Source/Platibus.IntegrationTests/With.cs | 5 ++ Source/Platibus.IntegrationTests/app.config | 2 +- .../EndpointAddressEqualityComparerTests.cs | 73 +++++++++++++++++++ .../Platibus.UnitTests.csproj | 1 + .../EndpointAddressEqualityComparer.cs | 30 ++++++++ Source/Platibus/EndpointCollection.cs | 3 +- Source/Platibus/Platibus.csproj | 1 + Source/Platibus/ReadOnlyEndpointCollection.cs | 3 +- Source/Version.cs | 2 +- 10 files changed, 122 insertions(+), 6 deletions(-) create mode 100644 Source/Platibus.UnitTests/EndpointAddressEqualityComparerTests.cs create mode 100644 Source/Platibus/EndpointAddressEqualityComparer.cs diff --git a/Source/Platibus.IntegrationTests/HttpPubSubTests.cs b/Source/Platibus.IntegrationTests/HttpPubSubTests.cs index af2477ab..4dc01a67 100644 --- a/Source/Platibus.IntegrationTests/HttpPubSubTests.cs +++ b/Source/Platibus.IntegrationTests/HttpPubSubTests.cs @@ -20,10 +20,10 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -using System; -using System.Threading.Tasks; using NUnit.Framework; using Platibus.Http; +using System; +using System.Threading.Tasks; namespace Platibus.IntegrationTests { @@ -76,6 +76,10 @@ await With.HttpHostedBusInstances(async (platibus0, platibus1) => DateData = DateTime.UtcNow }; + + // Wait for subscription request for platibus.http2 to be processed + await Task.Delay(TimeSpan.FromSeconds(1)); + await platibus0.Publish(publication, "Topic0"); var publicationReceived = await TestPublicationHandler.WaitHandle.WaitOneAsync(TimeSpan.FromSeconds(3)); diff --git a/Source/Platibus.IntegrationTests/With.cs b/Source/Platibus.IntegrationTests/With.cs index 09034752..24ae02be 100644 --- a/Source/Platibus.IntegrationTests/With.cs +++ b/Source/Platibus.IntegrationTests/With.cs @@ -135,6 +135,11 @@ private static void Cleanup() { TryTo(() => Directory.Delete("platibus1", true)); } + + if (Directory.Exists("platibus2")) + { + TryTo(() => Directory.Delete("platibus2", true)); + } } private static void TryTo(Action action) diff --git a/Source/Platibus.IntegrationTests/app.config b/Source/Platibus.IntegrationTests/app.config index 06c85b73..341fa73d 100644 --- a/Source/Platibus.IntegrationTests/app.config +++ b/Source/Platibus.IntegrationTests/app.config @@ -113,7 +113,7 @@ THE SOFTWARE. - + diff --git a/Source/Platibus.UnitTests/EndpointAddressEqualityComparerTests.cs b/Source/Platibus.UnitTests/EndpointAddressEqualityComparerTests.cs new file mode 100644 index 00000000..91214dff --- /dev/null +++ b/Source/Platibus.UnitTests/EndpointAddressEqualityComparerTests.cs @@ -0,0 +1,73 @@ + +using System; +using NUnit.Framework; + +namespace Platibus.UnitTests +{ + public class EndpointAddressEqualityComparerTests + { + [Test] + public void IdenticalEndpointAddressesShouldBeEqual() + { + var address1 = new Uri("http://test.example.com:8080/platibus/"); + var address2 = new Uri("http://test.example.com:8080/platibus/"); + Assert.That(address1, Is.EqualTo(address2).Using(new EndpointAddressEqualityComparer())); + } + + [Test] + public void EndpointAddressesShouldBeEqualWithoutTerminatingPathSeparator() + { + var address1 = new Uri("http://test.example.com:8080/platibus"); + var address2 = new Uri("http://test.example.com:8080/platibus/"); + Assert.That(address1, Is.EqualTo(address2).Using(new EndpointAddressEqualityComparer())); + } + + [Test] + public void EndpointAddressesWithDifferentQueryStringsShouldBeEqual() + { + var address1 = new Uri("http://test.example.com:8080/platibus/"); + var address2 = new Uri("http://test.example.com:8080/platibus/?test=true"); + Assert.That(address1, Is.EqualTo(address2).Using(new EndpointAddressEqualityComparer())); + } + + [Test] + public void EndpointAddressesShouldBeEqualWithDifferentQueryStringsAndWithoutTerminatingPathSeparator() + { + var address1 = new Uri("http://test.example.com:8080/platibus"); + var address2 = new Uri("http://test.example.com:8080/platibus/?test=true"); + Assert.That(address1, Is.EqualTo(address2).Using(new EndpointAddressEqualityComparer())); + } + + [Test] + public void EndpointAddressesWithDifferentHostsShouldNotBeEqual() + { + var address1 = new Uri("http://test1.example.com:8080/platibus/"); + var address2 = new Uri("http://test2.example.com:8080/platibus/"); + Assert.That(address1, Is.Not.EqualTo(address2).Using(new EndpointAddressEqualityComparer())); + } + + [Test] + public void EndpointAddressesWithDifferentSchemesShouldNotBeEqual() + { + var address1 = new Uri("https://test.example.com:8080/platibus/"); + var address2 = new Uri("http://test.example.com:8080/platibus/"); + Assert.That(address1, Is.Not.EqualTo(address2).Using(new EndpointAddressEqualityComparer())); + } + + [Test] + public void EndpointAddressesWithDifferentPathsShouldNotBeEqual() + { + var address1 = new Uri("http://test.example.com:8080/platibus1/"); + var address2 = new Uri("http://test.example.com:8080/platibus2/"); + Assert.That(address1, Is.Not.EqualTo(address2).Using(new EndpointAddressEqualityComparer())); + } + + [Test] + public void EndpointAddressesWithDifferentAuthoritiesShouldNotBeEqual() + { + var address1 = new Uri("http://user:pass@test.example.com:8080/platibus1/"); + var address2 = new Uri("http://test.example.com:8080/platibus2/"); + Assert.That(address1, Is.Not.EqualTo(address2).Using(new EndpointAddressEqualityComparer())); + } + } +} diff --git a/Source/Platibus.UnitTests/Platibus.UnitTests.csproj b/Source/Platibus.UnitTests/Platibus.UnitTests.csproj index 598fc263..59b8ee4a 100644 --- a/Source/Platibus.UnitTests/Platibus.UnitTests.csproj +++ b/Source/Platibus.UnitTests/Platibus.UnitTests.csproj @@ -102,6 +102,7 @@ + diff --git a/Source/Platibus/EndpointAddressEqualityComparer.cs b/Source/Platibus/EndpointAddressEqualityComparer.cs new file mode 100644 index 00000000..ef8a0a57 --- /dev/null +++ b/Source/Platibus/EndpointAddressEqualityComparer.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; + +namespace Platibus +{ + /// + /// Determines the equality of two endpoints based on the left part of their respective address URIs + /// + public class EndpointAddressEqualityComparer : IEqualityComparer + { + public bool Equals(Uri x, Uri y) + { + if (ReferenceEquals(x, y)) return true; + if (ReferenceEquals(null, x)) return false; + if (ReferenceEquals(null, y)) return false; + return string.Equals( + x.GetLeftPart(UriPartial.Path).TrimEnd('/'), + y.GetLeftPart(UriPartial.Path).TrimEnd('/'), + StringComparison.OrdinalIgnoreCase); + } + + public int GetHashCode(Uri obj) + { + return obj == null ? 0 : obj + .GetLeftPart(UriPartial.Path).TrimEnd('/') + .ToLower() + .GetHashCode(); + } + } +} diff --git a/Source/Platibus/EndpointCollection.cs b/Source/Platibus/EndpointCollection.cs index 113a3493..9cb0326b 100644 --- a/Source/Platibus/EndpointCollection.cs +++ b/Source/Platibus/EndpointCollection.cs @@ -60,7 +60,8 @@ public IEndpoint this[EndpointName endpointName] /// otherwise public bool TryGetEndpointByAddress(Uri address, out IEndpoint endpoint) { - endpoint = _endpoints.Values.FirstOrDefault(e => Equals(address, e.Address)); + var comparer = new EndpointAddressEqualityComparer(); + endpoint = _endpoints.Values.FirstOrDefault(e => comparer.Equals(e.Address, address)); return endpoint != null; } diff --git a/Source/Platibus/Platibus.csproj b/Source/Platibus/Platibus.csproj index d53fc5f4..2bad521a 100644 --- a/Source/Platibus/Platibus.csproj +++ b/Source/Platibus/Platibus.csproj @@ -89,6 +89,7 @@ + diff --git a/Source/Platibus/ReadOnlyEndpointCollection.cs b/Source/Platibus/ReadOnlyEndpointCollection.cs index 14cb9540..e70f4952 100644 --- a/Source/Platibus/ReadOnlyEndpointCollection.cs +++ b/Source/Platibus/ReadOnlyEndpointCollection.cs @@ -63,7 +63,8 @@ public IEndpoint this[EndpointName endpointName] /// otherwise public bool TryGetEndpointByAddress(Uri address, out IEndpoint endpoint) { - endpoint = _endpoints.Values.FirstOrDefault(e => Equals(address, e.Address)); + var comparer = new EndpointAddressEqualityComparer(); + endpoint = _endpoints.Values.FirstOrDefault(e => comparer.Equals(e.Address, address)); return endpoint != null; } diff --git a/Source/Version.cs b/Source/Version.cs index 484ed43a..d9ed1183 100644 --- a/Source/Version.cs +++ b/Source/Version.cs @@ -22,4 +22,4 @@ using System.Reflection; -[assembly: AssemblyVersion("2.1.9.0")] \ No newline at end of file +[assembly: AssemblyVersion("2.1.10.0")] \ No newline at end of file