Skip to content

Commit 9f0e9f5

Browse files
Merge pull request #831 from rabbitmq/rabbitmq-dotnet-client-827
Introduce PublicationAddress.TryParse and switch BasicProperties.ReplyToAddress to use it
2 parents ed65854 + 09bf7ac commit 9f0e9f5

7 files changed

+96
-16
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ public interface IBasicProperties : IContentHeader
121121
string ReplyTo { get; set; }
122122

123123
/// <summary>
124-
/// Convenience property; parses <see cref="ReplyTo"/> property using <see cref="PublicationAddress.Parse"/>,
124+
/// Convenience property; parses <see cref="ReplyTo"/> property using <see cref="PublicationAddress.TryParse"/>,
125125
/// and serializes it using <see cref="PublicationAddress.ToString"/>.
126-
/// Returns null if <see cref="ReplyTo"/> property cannot be parsed by <see cref="PublicationAddress.Parse"/>.
126+
/// Returns null if <see cref="ReplyTo"/> property cannot be parsed by <see cref="PublicationAddress.TryParse"/>.
127127
/// </summary>
128128
PublicationAddress ReplyToAddress { get; set; }
129129

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,32 @@ public static PublicationAddress Parse(string uriLikeString)
116116
return null;
117117
}
118118

119+
public static bool TryParse(string uriLikeString, out PublicationAddress result)
120+
{
121+
// Callers such as IBasicProperties.ReplyToAddress
122+
// expect null result for invalid input.
123+
// The regex.Match() throws on null arguments so we perform explicit check here
124+
if (uriLikeString == null)
125+
{
126+
result = null;
127+
return false;
128+
}
129+
else
130+
{
131+
try
132+
{
133+
var res = Parse(uriLikeString);
134+
result = res;
135+
return true;
136+
}
137+
catch
138+
{
139+
result = null;
140+
return false;
141+
}
142+
}
143+
}
144+
119145
/// <summary>
120146
/// Reconstruct the "uri" from its constituents.
121147
/// </summary>

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,16 @@ public bool Persistent
109109
public abstract string ReplyTo { get; set; }
110110

111111
/// <summary>
112-
/// Convenience property; parses <see cref="ReplyTo"/> property using <see cref="PublicationAddress.Parse"/>,
112+
/// Convenience property; parses <see cref="ReplyTo"/> property using <see cref="PublicationAddress.TryParse"/>,
113113
/// and serializes it using <see cref="PublicationAddress.ToString"/>.
114-
/// Returns null if <see cref="ReplyTo"/> property cannot be parsed by <see cref="PublicationAddress.Parse"/>.
114+
/// Returns null if <see cref="ReplyTo"/> property cannot be parsed by <see cref="PublicationAddress.TryParse"/>.
115115
/// </summary>
116116
public PublicationAddress ReplyToAddress
117117
{
118-
get { return PublicationAddress.Parse(ReplyTo); }
118+
get {
119+
PublicationAddress.TryParse(ReplyTo, out var result);
120+
return result;
121+
}
119122
set { ReplyTo = value.ToString(); }
120123
}
121124

projects/Unit/APIApproval.Approve.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@ namespace RabbitMQ.Client
483483
public string RoutingKey { get; }
484484
public override string ToString() { }
485485
public static RabbitMQ.Client.PublicationAddress Parse(string uriLikeString) { }
486+
public static bool TryParse(string uriLikeString, out RabbitMQ.Client.PublicationAddress result) { }
486487
}
487488
public class QueueDeclareOk
488489
{

projects/Unit/TestBasicProperties.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public void TestPersistentPropertyChangesDeliveryMode_PersistentTrueDelivery2()
5252
// Arrange
5353
var subject = new Framing.BasicProperties
5454
{
55-
5655
// Act
5756
Persistent = true
5857
};
@@ -120,5 +119,35 @@ public void TestNullableProperties_CanWrite(
120119
Assert.AreEqual(isCorrelationIdPresent, propertiesFromStream.IsCorrelationIdPresent());
121120
Assert.AreEqual(isMessageIdPresent, propertiesFromStream.IsMessageIdPresent());
122121
}
122+
123+
[Test]
124+
public void TestProperties_ReplyTo([Values(null, "foo_1", "fanout://name/key")] string replyTo)
125+
{
126+
// Arrange
127+
var subject = new Framing.BasicProperties
128+
{
129+
// Act
130+
ReplyTo = replyTo,
131+
};
132+
133+
// Assert
134+
bool isReplyToPresent = replyTo != null;
135+
PublicationAddress result;
136+
PublicationAddress.TryParse(replyTo, out result);
137+
string replyToAddress = result?.ToString();
138+
Assert.AreEqual(isReplyToPresent, subject.IsReplyToPresent());
139+
140+
var writer = new Impl.ContentHeaderPropertyWriter(new byte[1024]);
141+
subject.WritePropertiesTo(ref writer);
142+
143+
// Read from Stream
144+
var propertiesFromStream = new Framing.BasicProperties();
145+
var reader = new Impl.ContentHeaderPropertyReader(writer.Memory.Slice(0, writer.Offset));
146+
propertiesFromStream.ReadPropertiesFrom(ref reader);
147+
148+
Assert.AreEqual(replyTo, propertiesFromStream.ReplyTo);
149+
Assert.AreEqual(isReplyToPresent, propertiesFromStream.IsReplyToPresent());
150+
Assert.AreEqual(replyToAddress, propertiesFromStream.ReplyToAddress?.ToString());
151+
}
123152
}
124153
}

projects/Unit/TestPropertiesClone.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ private void TestBasicPropertiesClone(BasicProperties bp)
6868
bp.ContentType = "foo_1";
6969
bp.ContentEncoding = "foo_2";
7070
bp.Headers = new Dictionary<string, object>
71-
{
72-
{ "foo_3", "foo_4" },
73-
{ "foo_5", "foo_6" }
74-
};
71+
{
72+
{ "foo_3", "foo_4" },
73+
{ "foo_5", "foo_6" }
74+
};
7575
bp.DeliveryMode = 2;
7676
// Persistent also changes DeliveryMode's value to 2
7777
bp.Persistent = true;
@@ -123,6 +123,7 @@ private void TestBasicPropertiesClone(BasicProperties bp)
123123
Assert.AreEqual(12, bpClone.Priority);
124124
Assert.AreEqual("foo_7", bpClone.CorrelationId);
125125
Assert.AreEqual("foo_8", bpClone.ReplyTo);
126+
Assert.AreEqual(null, bpClone.ReplyToAddress);
126127
Assert.AreEqual("foo_9", bpClone.Expiration);
127128
Assert.AreEqual("foo_10", bpClone.MessageId);
128129
Assert.AreEqual(new AmqpTimestamp(123), bpClone.Timestamp);
@@ -141,10 +142,10 @@ private void TestBasicPropertiesNoneClone(BasicProperties bp)
141142
bp.ContentType = "foo_1";
142143
bp.ContentEncoding = "foo_2";
143144
bp.Headers = new Dictionary<string, object>
144-
{
145-
{ "foo_3", "foo_4" },
146-
{ "foo_5", "foo_6" }
147-
};
145+
{
146+
{ "foo_3", "foo_4" },
147+
{ "foo_5", "foo_6" }
148+
};
148149
bp.DeliveryMode = 2;
149150
// Persistent also changes DeliveryMode's value to 2
150151
bp.Persistent = true;

projects/Unit/TestPublicationAddress.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,29 @@ public void TestParseOk()
5757
}
5858

5959
[Test]
60-
public void TestParseFail()
60+
public void TestParseFailWithANE()
6161
{
62-
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+
PublicationAddress result;
75+
PublicationAddress.TryParse(null, out result);
76+
Assert.IsNull(result);
77+
78+
PublicationAddress.TryParse("not a valid URI", out result);
79+
Assert.IsNull(result);
80+
81+
PublicationAddress.TryParse("}}}}}}}}", out result);
82+
Assert.IsNull(result);
6383
}
6484

6585
[Test]

0 commit comments

Comments
 (0)