Skip to content

Commit

Permalink
EndpointCollection.TryGetEndpointByAddress and ReadOnlyEndpointCollec…
Browse files Browse the repository at this point in the history
…tion.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.
  • Loading branch information
sweetlandj committed Apr 8, 2016
1 parent 2419b54 commit 4848f7f
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 6 deletions.
8 changes: 6 additions & 2 deletions Source/Platibus.IntegrationTests/HttpPubSubTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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));
Expand Down
5 changes: 5 additions & 0 deletions Source/Platibus.IntegrationTests/With.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Source/Platibus.IntegrationTests/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ THE SOFTWARE.

<platibus.http2 baseUri="http://localhost:52180/platibus2" replyTimeout="00:00:30">
<queueing provider="SQLite" path="platibus2\queues" />
<subscriptionTracking provider="Filesystem" path="platibus1\subscriptions" />
<subscriptionTracking provider="Filesystem" path="platibus2\subscriptions" />
<endpoints>
<add name="platibus0" address="http://localhost:52180/platibus0" />
</endpoints>
Expand Down
73 changes: 73 additions & 0 deletions Source/Platibus.UnitTests/EndpointAddressEqualityComparerTests.cs
Original file line number Diff line number Diff line change
@@ -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:[email protected]:8080/platibus1/");
var address2 = new Uri("http://test.example.com:8080/platibus2/");
Assert.That(address1, Is.Not.EqualTo(address2).Using(new EndpointAddressEqualityComparer()));
}
}
}
1 change: 1 addition & 0 deletions Source/Platibus.UnitTests/Platibus.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
<Compile Include="ClaimsPrincipalEqualityComparer.cs" />
<Compile Include="DataContractMessageNamingServiceTests.cs" />
<Compile Include="DefaultMessageNamingServiceTests.cs" />
<Compile Include="EndpointAddressEqualityComparerTests.cs" />
<Compile Include="PlatibusConfigurationExtensionTests.cs" />
<Compile Include="RabbitMQMessageQueueingServiceTests.cs" />
<Compile Include="SQLiteMessageQueueingServiceTests.cs" />
Expand Down
30 changes: 30 additions & 0 deletions Source/Platibus/EndpointAddressEqualityComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;

namespace Platibus
{
/// <summary>
/// Determines the equality of two endpoints based on the left part of their respective address URIs
/// </summary>
public class EndpointAddressEqualityComparer : IEqualityComparer<Uri>
{
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();
}
}
}
3 changes: 2 additions & 1 deletion Source/Platibus/EndpointCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public IEndpoint this[EndpointName endpointName]
/// otherwise</returns>
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;
}

Expand Down
1 change: 1 addition & 0 deletions Source/Platibus/Platibus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<Compile Include="Config\LoopbackConfigurationSection.cs" />
<Compile Include="Config\QueueNameFactory.cs" />
<Compile Include="DataContractMessageNamingService.cs" />
<Compile Include="EndpointAddressEqualityComparer.cs" />
<Compile Include="EndpointCollection.cs" />
<Compile Include="Http\AuthenticationSchemeElement.cs" />
<Compile Include="Http\AuthenticationSchemesElementCollection.cs" />
Expand Down
3 changes: 2 additions & 1 deletion Source/Platibus/ReadOnlyEndpointCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public IEndpoint this[EndpointName endpointName]
/// otherwise</returns>
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;
}

Expand Down
2 changes: 1 addition & 1 deletion Source/Version.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@

using System.Reflection;

[assembly: AssemblyVersion("2.1.9.0")]
[assembly: AssemblyVersion("2.1.10.0")]

0 comments on commit 4848f7f

Please sign in to comment.