Skip to content

Commit

Permalink
Fix routing key with # matching for topic exchange
Browse files Browse the repository at this point in the history
  • Loading branch information
ledoyen committed Jul 22, 2020
1 parent d0043d1 commit f430870
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,24 @@ public MockTopicExchange(String name, AmqArguments arguments, ReceiverRegistry r
super(name, TYPE, arguments, receiverRegistry);
}

/**
* <a href="https://www.rabbitmq.com/tutorials/tutorial-five-python.html">
* https://www.rabbitmq.com/tutorials/tutorial-five-python.html</a>
*
* <p>sharp / hash character substitutes to <b>zero</b> or more words</p>
* <p>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.</p>
* <p>Considering the key <b>some.#.key.*</b>, all these keys can match:</p>
* <ul>
* <li><b>some.key.redeyes</b> where # matches for no words and * for 1 word (redeyes)</li>
* <li><b>some.pink.key.blueeyes</b> where # matches for 1 words (pink) and * for 1 word (blueeyes)</li>
* <li><b>some.pink.rabbit.key.random</b> where # matches for 2 words (pink, rabbit) and * for 1 word (random)</li>
* </ul>
*/
protected boolean match(BindConfiguration bindConfiguration, String routingKey, Map<String, Object> headers) {
String bindingRegex = bindConfiguration.bindingKey
.replace("*", "([^\\.]+)")
.replace(".#", "(\\.(.*))?")
.replace("#", "(.+)");
return routingKey.matches(bindingRegex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));
Expand Down

0 comments on commit f430870

Please sign in to comment.