Skip to content

Commit aa9ee8b

Browse files
committed
* Add RoutingKey type and tests.
1 parent 9846e1c commit aa9ee8b

File tree

3 files changed

+76
-6
lines changed

3 files changed

+76
-6
lines changed

projects/RabbitMQ.Client/PublicAPI.Unshipped.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,8 @@ RabbitMQ.Client.ReadOnlyBasicProperties.ReplyToAddress.get -> RabbitMQ.Client.Pu
692692
RabbitMQ.Client.ReadOnlyBasicProperties.Timestamp.get -> RabbitMQ.Client.AmqpTimestamp
693693
RabbitMQ.Client.ReadOnlyBasicProperties.Type.get -> string
694694
RabbitMQ.Client.ReadOnlyBasicProperties.UserId.get -> string
695+
RabbitMQ.Client.RoutingKey
696+
RabbitMQ.Client.RoutingKey.RoutingKey() -> void
695697
RabbitMQ.Client.ShutdownEventArgs
696698
RabbitMQ.Client.ShutdownEventArgs.Cause.get -> object
697699
RabbitMQ.Client.ShutdownEventArgs.ClassId.get -> ushort
@@ -887,7 +889,8 @@ virtual RabbitMQ.Client.TcpClientAdapter.ReceiveTimeout.set -> void
887889
~override RabbitMQ.Client.AmqpString.ToString() -> string
888890
~override RabbitMQ.Client.Events.AsyncEventingBasicConsumer.HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, System.ReadOnlyMemory<byte> exchange, System.ReadOnlyMemory<byte> routingKey, in RabbitMQ.Client.ReadOnlyBasicProperties properties, System.ReadOnlyMemory<byte> body) -> System.Threading.Tasks.Task
889891
~override RabbitMQ.Client.Events.EventingBasicConsumer.HandleBasicDeliverAsync(string consumerTag, ulong deliveryTag, bool redelivered, System.ReadOnlyMemory<byte> exchange, System.ReadOnlyMemory<byte> routingKey, RabbitMQ.Client.ReadOnlyBasicProperties properties, System.ReadOnlyMemory<byte> body) -> System.Threading.Tasks.Task
890-
~RabbitMQ.Client.AmqpString.AmqpString(string value, ushort maxLen, System.Text.Encoding validEncoding, string validatorRegex) -> void
892+
~RabbitMQ.Client.AmqpString.AmqpString(string value, ushort maxLen, System.Text.Encoding encoding) -> void
893+
~RabbitMQ.Client.AmqpString.AmqpString(string value, ushort maxLen, System.Text.Encoding encoding, string validatorRegex) -> void
891894
~RabbitMQ.Client.AmqpTcpEndpoint.AmqpTcpEndpoint(string hostName, int portOrMinusOne, RabbitMQ.Client.SslOption ssl, uint maxInboundMessageBodySize) -> void
892895
~RabbitMQ.Client.ConnectionFactory.CreateConnectionAsync(RabbitMQ.Client.IEndpointResolver endpointResolver, string clientProvidedName, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<RabbitMQ.Client.IConnection>
893896
~RabbitMQ.Client.ConnectionFactory.CreateConnectionAsync(string clientProvidedName, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<RabbitMQ.Client.IConnection>
@@ -937,6 +940,7 @@ virtual RabbitMQ.Client.TcpClientAdapter.ReceiveTimeout.set -> void
937940
~RabbitMQ.Client.IConnectionFactory.CreateConnectionAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<RabbitMQ.Client.IConnection>
938941
~RabbitMQ.Client.ICredentialsRefresher.Register(RabbitMQ.Client.ICredentialsProvider provider, RabbitMQ.Client.ICredentialsRefresher.NotifyCredentialRefreshedAsync callback) -> RabbitMQ.Client.ICredentialsProvider
939942
~RabbitMQ.Client.QueueName.QueueName(string exchangeName) -> void
943+
~RabbitMQ.Client.RoutingKey.RoutingKey(string exchangeName) -> void
940944
~RabbitMQ.Client.TimerBasedCredentialRefresher.Register(RabbitMQ.Client.ICredentialsProvider provider, RabbitMQ.Client.ICredentialsRefresher.NotifyCredentialRefreshedAsync callback) -> RabbitMQ.Client.ICredentialsProvider
941945
~RabbitMQ.Client.TopologyRecoveryExceptionHandler.BindingRecoveryExceptionHandlerAsync.get -> System.Func<RabbitMQ.Client.IRecordedBinding, System.Exception, RabbitMQ.Client.IConnection, System.Threading.Tasks.Task>
942946
~RabbitMQ.Client.TopologyRecoveryExceptionHandler.BindingRecoveryExceptionHandlerAsync.set -> void
@@ -969,7 +973,9 @@ virtual RabbitMQ.Client.TcpClientAdapter.ReceiveTimeout.set -> void
969973
~static RabbitMQ.Client.IConnectionExtensions.CloseAsync(this RabbitMQ.Client.IConnection connection, ushort reasonCode, string reasonText) -> System.Threading.Tasks.Task
970974
~static RabbitMQ.Client.IConnectionExtensions.CloseAsync(this RabbitMQ.Client.IConnection connection, ushort reasonCode, string reasonText, System.TimeSpan timeout) -> System.Threading.Tasks.Task
971975
~static RabbitMQ.Client.QueueName.explicit operator RabbitMQ.Client.QueueName(string value) -> RabbitMQ.Client.QueueName
976+
~static RabbitMQ.Client.RoutingKey.explicit operator RabbitMQ.Client.RoutingKey(string value) -> RabbitMQ.Client.RoutingKey
972977
~static readonly RabbitMQ.Client.ExchangeName.Empty -> RabbitMQ.Client.ExchangeName
973978
~static readonly RabbitMQ.Client.QueueName.Empty -> RabbitMQ.Client.QueueName
979+
~static readonly RabbitMQ.Client.RoutingKey.Empty -> RabbitMQ.Client.RoutingKey
974980
~virtual RabbitMQ.Client.AsyncDefaultBasicConsumer.HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, System.ReadOnlyMemory<byte> exchange, System.ReadOnlyMemory<byte> routingKey, in RabbitMQ.Client.ReadOnlyBasicProperties properties, System.ReadOnlyMemory<byte> body) -> System.Threading.Tasks.Task
975981
~virtual RabbitMQ.Client.DefaultBasicConsumer.HandleBasicDeliverAsync(string consumerTag, ulong deliveryTag, bool redelivered, System.ReadOnlyMemory<byte> exchange, System.ReadOnlyMemory<byte> routingKey, RabbitMQ.Client.ReadOnlyBasicProperties properties, System.ReadOnlyMemory<byte> body) -> System.Threading.Tasks.Task

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

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,37 @@ public AmqpString()
4646
_stringBytes = ReadOnlyMemory<byte>.Empty;
4747
}
4848

49-
public AmqpString(string value, ushort maxLen, Encoding validEncoding, string validatorRegex)
49+
public AmqpString(string value, ushort maxLen, Encoding encoding)
50+
: this(value, maxLen, encoding, null)
51+
{
52+
}
53+
54+
public AmqpString(string value, ushort maxLen, Encoding encoding, string validatorRegex)
5055
{
5156
if (value.Length > maxLen)
5257
{
5358
throw new ArgumentOutOfRangeException(nameof(value));
5459
}
5560

56-
var re = new Regex(validatorRegex);
57-
if (false == re.IsMatch(value))
61+
if (false == string.IsNullOrWhiteSpace(validatorRegex))
5862
{
59-
throw new ArgumentOutOfRangeException(nameof(value));
63+
var re = new Regex(validatorRegex);
64+
if (false == re.IsMatch(value))
65+
{
66+
throw new ArgumentOutOfRangeException(nameof(value));
67+
}
68+
}
69+
70+
if (encoding == Encoding.ASCII)
71+
{
72+
if (false == isAscii(value))
73+
{
74+
throw new ArgumentOutOfRangeException(nameof(value));
75+
}
6076
}
6177

6278
_value = value;
63-
_stringBytes = new ReadOnlyMemory<byte>(validEncoding.GetBytes(value));
79+
_stringBytes = new ReadOnlyMemory<byte>(encoding.GetBytes(value));
6480
}
6581

6682
public override string ToString()
@@ -77,6 +93,11 @@ public static implicit operator ReadOnlyMemory<byte>(AmqpString amqpString)
7793
{
7894
return amqpString._stringBytes;
7995
}
96+
97+
private bool isAscii(string value)
98+
{
99+
return Encoding.UTF8.GetByteCount(value) == value.Length;
100+
}
80101
}
81102

82103
/*
@@ -132,4 +153,30 @@ public static explicit operator QueueName(string value)
132153
return new QueueName(value);
133154
}
134155
}
156+
157+
/*
158+
* From the spec:
159+
* <field name="routing-key" domain="shortstr" label="Message routing key">
160+
* <doc> Specifies the routing key for the message. The routing key is used for routing messages depending on the exchange configuration. </doc>
161+
* </field>
162+
* <domain name = "shortstr" type="shortstr" label="short string (max. 256 characters)"/>
163+
*/
164+
public class RoutingKey : AmqpString
165+
{
166+
public static readonly RoutingKey Empty = new RoutingKey();
167+
168+
public RoutingKey() : base()
169+
{
170+
}
171+
172+
public RoutingKey(string exchangeName)
173+
: base(exchangeName, 256, Encoding.ASCII)
174+
{
175+
}
176+
177+
public static explicit operator RoutingKey(string value)
178+
{
179+
return new RoutingKey(value);
180+
}
181+
}
135182
}

projects/Test/Unit/TestAmqpString.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,22 @@ public void TestInvalidQueueNameThrows(string arg)
7474
{
7575
Assert.Throws<ArgumentOutOfRangeException>(() => new QueueName(arg));
7676
}
77+
78+
[Theory]
79+
[InlineData("$key-ABC:123.abc_456*blargh///~~~?")]
80+
[InlineData("")]
81+
public void TestValidRoutingKeys(string arg)
82+
{
83+
var e = new RoutingKey(arg);
84+
Assert.Equal(e, arg);
85+
}
86+
87+
[Theory]
88+
[InlineData("key-Евгений")]
89+
[InlineData("O0s5Vu04DVyVSMp8twUjsxa0gjcQ9ixV43U10VDVS6wPTQr14mC2x8cK6fl1G0INoJlCs4v7291iOMlBLj171F09a3VBx4sFc7SKaF683YeG9nn5IQAhQZxDE6grPr20EmHPZpMy3TO3R5aW976WTNVPDzZC5vprruJgaBOh8P9ZKRwc77v2nXUnDcz1t2rX84V8l5deX6V3l0Fv4S0bQzSbGdpD5jiPSZ44yBsZVXtv61saCgu0FNGoFSuV9207s6PiY80XbjBK9nY39R947i2s03cAg261412IS1FzS4uEoJJ6cWZ50P0SJ3S4yqvzx0n4TN4NsROlWyEwaUG4I5Glrj1mI2N28QGbkf5t8Kyo7EavaqME5TrvhPxtJGY1p")]
90+
public void TestInvalidRoutingKeyThrows(string arg)
91+
{
92+
Assert.Throws<ArgumentOutOfRangeException>(() => new RoutingKey(arg));
93+
}
7794
}
7895
}

0 commit comments

Comments
 (0)