Skip to content

Commit 470dba7

Browse files
Amend #828 to introduce a new TryParse method
instead of changing the behavior of an existing one. Per discussion with @bording and @ig-sinicyn in #827.
1 parent 4944dda commit 470dba7

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

projects/RabbitMQ.Client/client/api/PublicationAddress.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,6 @@ public PublicationAddress(string exchangeType, string exchangeName, string routi
106106
/// </summary>
107107
public static PublicationAddress Parse(string uriLikeString)
108108
{
109-
// Callers such as IBasicProperties.ReplyToAddress
110-
// expect null result for invalid input.
111-
// The regex.Match() throws on null arguments so we perform explicit check here
112-
if (uriLikeString == null)
113-
{
114-
return null;
115-
}
116-
117109
Match match = PSEUDO_URI_PARSER.Match(uriLikeString);
118110
if (match.Success)
119111
{
@@ -124,6 +116,22 @@ public static PublicationAddress Parse(string uriLikeString)
124116
return null;
125117
}
126118

119+
public static PublicationAddress TryParse(string uriLikeString) {
120+
// Callers such as IBasicProperties.ReplyToAddress
121+
// expect null result for invalid input.
122+
// The regex.Match() throws on null arguments so we perform explicit check here
123+
if (uriLikeString == null)
124+
{
125+
return null;
126+
} else {
127+
try {
128+
return Parse(uriLikeString);
129+
} catch {
130+
return null;
131+
}
132+
}
133+
}
134+
127135
/// <summary>
128136
/// Reconstruct the "uri" from its constituents.
129137
/// </summary>

projects/RabbitMQ.Client/client/impl/BasicProperties.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public bool Persistent
115115
/// </summary>
116116
public PublicationAddress ReplyToAddress
117117
{
118-
get { return PublicationAddress.Parse(ReplyTo); }
118+
get { return PublicationAddress.TryParse(ReplyTo); }
119119
set { ReplyTo = value.ToString(); }
120120
}
121121

projects/Unit/TestPublicationAddress.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,23 @@ public void TestParseOk()
5757
}
5858

5959
[Test]
60-
public void TestParseFail()
60+
public void TestParseFailWithANE()
6161
{
62-
Assert.IsNull(PublicationAddress.Parse(null));
63-
Assert.IsNull(PublicationAddress.Parse("not a valid uri"));
62+
Assert.That(()=> PublicationAddress.Parse(null), Throws.ArgumentNullException);
63+
}
64+
65+
[Test]
66+
public void TestParseFailWithUnparseableInput()
67+
{
68+
Assert.IsNull(PublicationAddress.Parse("not a valid URI"));
69+
}
70+
71+
[Test]
72+
public void TestTryParseFail()
73+
{
74+
Assert.IsNull(PublicationAddress.TryParse(null));
75+
Assert.IsNull(PublicationAddress.TryParse("not a valid URI"));
76+
Assert.IsNull(PublicationAddress.TryParse("}}}}}}}}"));
6477
}
6578

6679
[Test]

0 commit comments

Comments
 (0)