Skip to content

Commit fd3322f

Browse files
committed
Encode path segments in address format
1 parent 7500592 commit fd3322f

File tree

4 files changed

+97
-4
lines changed

4 files changed

+97
-4
lines changed

src/main/java/com/rabbitmq/client/amqp/impl/DefaultAddressBuilder.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
1818
package com.rabbitmq.client.amqp.impl;
1919

20+
import static com.rabbitmq.client.amqp.impl.UriUtils.encodePathSegment;
21+
2022
import com.rabbitmq.client.amqp.AddressBuilder;
2123

2224
abstract class DefaultAddressBuilder<T> implements AddressBuilder<T> {
@@ -57,12 +59,15 @@ public T queue(String queue) {
5759
String address() {
5860
if (this.exchange != null) {
5961
if (this.key != null && !this.key.isEmpty()) {
60-
return "/exchange/" + this.exchange + "/key/" + this.key;
62+
return "/exchange/"
63+
+ encodePathSegment(this.exchange)
64+
+ "/key/"
65+
+ encodePathSegment(this.key);
6166
} else {
62-
return "/exchange/" + this.exchange;
67+
return "/exchange/" + encodePathSegment(this.exchange);
6368
}
6469
} else if (this.queue != null) {
65-
return "/queue/" + this.queue;
70+
return "/queue/" + encodePathSegment(this.queue);
6671
} else {
6772
return null;
6873
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) 2024 Broadcom. All Rights Reserved.
2+
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
// If you have any questions regarding licensing, please contact us at
17+
18+
package com.rabbitmq.client.amqp.impl;
19+
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
22+
import org.junit.jupiter.api.Test;
23+
24+
public class AmqpMessageTest {
25+
26+
@Test
27+
void toShouldBePathEncoded() {
28+
assertThat(new AmqpMessage().toAddress().exchange("foo bar").message().to())
29+
.isEqualTo("/exchange/foo%20bar");
30+
}
31+
32+
@Test
33+
void replyToShouldBePathEncoded() {
34+
assertThat(new AmqpMessage().replyToAddress().exchange("foo bar").message().replyTo())
35+
.isEqualTo("/exchange/foo%20bar");
36+
}
37+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) 2024 Broadcom. All Rights Reserved.
2+
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
// If you have any questions regarding licensing, please contact us at
17+
18+
package com.rabbitmq.client.amqp.impl;
19+
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
22+
import org.junit.jupiter.api.Test;
23+
24+
public class DefaultAddressBuilderTest {
25+
26+
@Test
27+
void pathSegmentsShouldBePercentEncoded() {
28+
assertThat(b().exchange("foo").address()).isEqualTo("/exchange/foo");
29+
assertThat(b().exchange("foo").key("bar").address()).isEqualTo("/exchange/foo/key/bar");
30+
assertThat(b().exchange("foo bar").address()).isEqualTo("/exchange/foo%20bar");
31+
assertThat(b().exchange("foo").key("b ar").address()).isEqualTo("/exchange/foo/key/b%20ar");
32+
assertThat(b().queue("foo").address()).isEqualTo("/queue/foo");
33+
assertThat(b().queue("foo bar").address()).isEqualTo("/queue/foo%20bar");
34+
}
35+
36+
TestAddressBuilder b() {
37+
return new TestAddressBuilder();
38+
}
39+
40+
private static class TestAddressBuilder extends DefaultAddressBuilder<TestAddressBuilder> {
41+
42+
TestAddressBuilder() {
43+
super(null);
44+
}
45+
46+
@Override
47+
TestAddressBuilder result() {
48+
return this;
49+
}
50+
}
51+
}

src/test/java/com/rabbitmq/client/amqp/impl/UriUtilsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
public class UriUtilsTest {
2727

2828
@ParameterizedTest
29-
@CsvSource({"test,test", "foo bar,foo%20bar", "foo%bar,foo%25bar"})
29+
@CsvSource({"test,test", "foo bar,foo%20bar", "foo%bar,foo%25bar", "/,%2F"})
3030
void encodePathSegmentTest(String segment, String expected) {
3131
assertThat(encodePathSegment(segment)).isEqualTo(expected);
3232
}

0 commit comments

Comments
 (0)