From f4308700dcfe0882fe1c5ca4eb9e71d10f3a98ed Mon Sep 17 00:00:00 2001 From: ledoyen Date: Tue, 21 Jul 2020 20:59:25 +0200 Subject: [PATCH] Fix routing key with # matching for topic exchange --- .../rabbitmq/mock/exchange/MockTopicExchange.java | 15 +++++++++++++++ .../rabbitmq/mock/exchange/ExchangeTest.java | 12 ++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/github/fridujo/rabbitmq/mock/exchange/MockTopicExchange.java b/src/main/java/com/github/fridujo/rabbitmq/mock/exchange/MockTopicExchange.java index 56cd5e01..035f0d95 100644 --- a/src/main/java/com/github/fridujo/rabbitmq/mock/exchange/MockTopicExchange.java +++ b/src/main/java/com/github/fridujo/rabbitmq/mock/exchange/MockTopicExchange.java @@ -13,9 +13,24 @@ public MockTopicExchange(String name, AmqArguments arguments, ReceiverRegistry r super(name, TYPE, arguments, receiverRegistry); } + /** + * + * https://www.rabbitmq.com/tutorials/tutorial-five-python.html + * + *

sharp / hash character substitutes to zero or more words

+ *

An easy thought representation of routing keys is a list of words, words being separated by dots, + * and topic exchange binding keys an description of this kind of list.

+ *

Considering the key some.#.key.*, all these keys can match:

+ * + */ protected boolean match(BindConfiguration bindConfiguration, String routingKey, Map headers) { String bindingRegex = bindConfiguration.bindingKey .replace("*", "([^\\.]+)") + .replace(".#", "(\\.(.*))?") .replace("#", "(.+)"); return routingKey.matches(bindingRegex); } diff --git a/src/test/java/com/github/fridujo/rabbitmq/mock/exchange/ExchangeTest.java b/src/test/java/com/github/fridujo/rabbitmq/mock/exchange/ExchangeTest.java index 5fad1993..1adc02d2 100644 --- a/src/test/java/com/github/fridujo/rabbitmq/mock/exchange/ExchangeTest.java +++ b/src/test/java/com/github/fridujo/rabbitmq/mock/exchange/ExchangeTest.java @@ -108,7 +108,10 @@ class TopicTest { "some.key, some.key", "*.orange.*, quick.orange.rabbit", "*.*.rabbit, quick.orange.rabbit", - "lazy.#, lazy.pink.rabbit" + "lazy.#, lazy", + "lazy.#, lazy.pink", + "lazy.#, lazy.pink.rabbit", + "some.#.key.*, some.stuff.key.1", }) void binding_key_matches_routing_key(String bindingKey, String routingKey) { MultipleReceiverExchange topicExchange = (MultipleReceiverExchange) mockExchangeFactory.build("test", BuiltinExchangeType.TOPIC.getType(), empty(), mock(ReceiverRegistry.class)); @@ -124,7 +127,12 @@ void binding_key_matches_routing_key(String bindingKey, String routingKey) { "*.orange.*, quick.orange.male.rabbit", "*.*.rabbit, quick.orange.fox", "*.*.rabbit, quick.orange.male.rabbit", - "lazy.#, quick.brown.fox" + "lazy.#, quick.brown.fox", + "lazy.#, lazy1.brown.fox", + "some.#.key.*, some.stuff.key", + "some.#.key.*, some.stuff.key", + "some.#.key.*, some.stuff.key.", + "some.#.key.*, some.stuff.key.one.two", }) void binding_key_does_not_match_routing_key(String bindingKey, String routingKey) { MultipleReceiverExchange topicExchange = (MultipleReceiverExchange) mockExchangeFactory.build("test", BuiltinExchangeType.TOPIC.getType(), empty(), mock(ReceiverRegistry.class));