From 470dba7304a45f92b5bfb5ff2e8e6bb1a7345000 Mon Sep 17 00:00:00 2001 From: Michael Klishin Date: Mon, 11 May 2020 19:34:20 +0300 Subject: [PATCH] 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. --- .../client/api/PublicationAddress.cs | 24 ++++++++++++------- .../client/impl/BasicProperties.cs | 2 +- projects/Unit/TestPublicationAddress.cs | 19 ++++++++++++--- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/projects/RabbitMQ.Client/client/api/PublicationAddress.cs b/projects/RabbitMQ.Client/client/api/PublicationAddress.cs index 6778809a1a..0befcf620f 100644 --- a/projects/RabbitMQ.Client/client/api/PublicationAddress.cs +++ b/projects/RabbitMQ.Client/client/api/PublicationAddress.cs @@ -106,14 +106,6 @@ public PublicationAddress(string exchangeType, string exchangeName, string routi /// 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) { @@ -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; + } + } + } + /// /// Reconstruct the "uri" from its constituents. /// diff --git a/projects/RabbitMQ.Client/client/impl/BasicProperties.cs b/projects/RabbitMQ.Client/client/impl/BasicProperties.cs index b824801b83..a8946397f7 100644 --- a/projects/RabbitMQ.Client/client/impl/BasicProperties.cs +++ b/projects/RabbitMQ.Client/client/impl/BasicProperties.cs @@ -115,7 +115,7 @@ public bool Persistent /// public PublicationAddress ReplyToAddress { - get { return PublicationAddress.Parse(ReplyTo); } + get { return PublicationAddress.TryParse(ReplyTo); } set { ReplyTo = value.ToString(); } } diff --git a/projects/Unit/TestPublicationAddress.cs b/projects/Unit/TestPublicationAddress.cs index 7e9661e3e1..e1b4683155 100644 --- a/projects/Unit/TestPublicationAddress.cs +++ b/projects/Unit/TestPublicationAddress.cs @@ -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]