Skip to content

Commit 8681a8a

Browse files
committed
Map empty virtual host to "/" when parsed from an address
Previously, an address that ended in a "/" would result in the virtual host being an empty string. This was inconsistent with setVirtualHost which would map an empty string to "/". This commit updates the address parsing logic to call setVirtualHost rather than assigning the value directly to this.virtualHost. This ensures that the special handling for an empty string is applied consistently. Closes gh-3304
1 parent cd62596 commit 8681a8a

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@
2727
*
2828
* @author Greg Turnquist
2929
* @author Dave Syer
30+
* @author Andy Wilkinson
3031
*/
3132
@ConfigurationProperties(prefix = "spring.rabbitmq")
3233
public class RabbitProperties {
@@ -115,7 +116,7 @@ private String parseAddresses(String addresses) {
115116
}
116117
int index = address.indexOf("/");
117118
if (index >= 0 && index < address.length()) {
118-
this.virtualHost = address.substring(index + 1);
119+
setVirtualHost(address.substring(index + 1));
119120
address = address.substring(0, index);
120121
}
121122
if (!address.contains(":")) {

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitPropertiesTests.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525
* Tests for {@link RabbitProperties}.
2626
*
2727
* @author Dave Syer
28+
* @author Andy Wilkinson
2829
*/
2930
public class RabbitPropertiesTests {
3031

@@ -85,14 +86,23 @@ public void addressesSingleValuedWithCredentialsDefaultPort() {
8586
assertEquals("lemur.cloudamqp.com:5672", this.properties.getAddresses());
8687
}
8788

89+
@Test
90+
public void addressWithTrailingSlash() {
91+
this.properties.setAddresses("amqp://root:password@otherhost:1111/");
92+
assertEquals("otherhost", this.properties.getHost());
93+
assertEquals(1111, this.properties.getPort());
94+
assertEquals("root", this.properties.getUsername());
95+
assertEquals("/", this.properties.getVirtualHost());
96+
}
97+
8898
@Test
8999
public void testDefaultVirtualHost() {
90100
this.properties.setVirtualHost("/");
91101
assertEquals("/", this.properties.getVirtualHost());
92102
}
93103

94104
@Test
95-
public void testemptyVirtualHost() {
105+
public void testEmptyVirtualHost() {
96106
this.properties.setVirtualHost("");
97107
assertEquals("/", this.properties.getVirtualHost());
98108
}

0 commit comments

Comments
 (0)