Skip to content

Commit

Permalink
Amend #828 to introduce a new TryParse method
Browse files Browse the repository at this point in the history
instead of changing the behavior of an existing one.

Per discussion with @bording and @ig-sinicyn in #827.
  • Loading branch information
michaelklishin committed May 11, 2020
1 parent 4944dda commit 470dba7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
24 changes: 16 additions & 8 deletions projects/RabbitMQ.Client/client/api/PublicationAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,6 @@ public PublicationAddress(string exchangeType, string exchangeName, string routi
/// </summary>
public static PublicationAddress Parse(string uriLikeString)
{
// Callers such as IBasicProperties.ReplyToAddress
// expect null result for invalid input.
// The regex.Match() throws on null arguments so we perform explicit check here
if (uriLikeString == null)
{
return null;
}

Match match = PSEUDO_URI_PARSER.Match(uriLikeString);
if (match.Success)
{
Expand All @@ -124,6 +116,22 @@ public static PublicationAddress Parse(string uriLikeString)
return null;
}

public static PublicationAddress TryParse(string uriLikeString) {
// Callers such as IBasicProperties.ReplyToAddress
// expect null result for invalid input.
// The regex.Match() throws on null arguments so we perform explicit check here
if (uriLikeString == null)
{
return null;
} else {
try {
return Parse(uriLikeString);
} catch {
return null;
}
}
}

/// <summary>
/// Reconstruct the "uri" from its constituents.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion projects/RabbitMQ.Client/client/impl/BasicProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public bool Persistent
/// </summary>
public PublicationAddress ReplyToAddress
{
get { return PublicationAddress.Parse(ReplyTo); }
get { return PublicationAddress.TryParse(ReplyTo); }
set { ReplyTo = value.ToString(); }
}

Expand Down
19 changes: 16 additions & 3 deletions projects/Unit/TestPublicationAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,23 @@ public void TestParseOk()
}

[Test]
public void TestParseFail()
public void TestParseFailWithANE()
{
Assert.IsNull(PublicationAddress.Parse(null));
Assert.IsNull(PublicationAddress.Parse("not a valid uri"));
Assert.That(()=> PublicationAddress.Parse(null), Throws.ArgumentNullException);
}

[Test]
public void TestParseFailWithUnparseableInput()
{
Assert.IsNull(PublicationAddress.Parse("not a valid URI"));
}

[Test]
public void TestTryParseFail()
{
Assert.IsNull(PublicationAddress.TryParse(null));
Assert.IsNull(PublicationAddress.TryParse("not a valid URI"));
Assert.IsNull(PublicationAddress.TryParse("}}}}}}}}"));
}

[Test]
Expand Down

0 comments on commit 470dba7

Please sign in to comment.