RabbitMQ virtual hosts can how start with a slash
The autoconfig strips out slashes where necessary to make a valid hostname Fixes gh-1128
This commit is contained in:
parent
3411995736
commit
ad1636fd34
|
@ -110,10 +110,15 @@ public class RabbitProperties {
|
|||
}
|
||||
|
||||
public void setVirtualHost(String virtualHost) {
|
||||
while (virtualHost.startsWith("/") && virtualHost.length() > 0) {
|
||||
virtualHost = virtualHost.substring(1);
|
||||
}
|
||||
this.virtualHost = "/" + virtualHost;
|
||||
if ("".equals(virtualHost) || virtualHost.equals("/")) {
|
||||
this.virtualHost = "/";
|
||||
} else {
|
||||
// remove all trailing /
|
||||
while (virtualHost.startsWith("/") && virtualHost.length() > 0) {
|
||||
virtualHost = virtualHost.substring(1);
|
||||
}
|
||||
this.virtualHost = virtualHost;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -16,6 +16,9 @@
|
|||
|
||||
package org.springframework.boot.autoconfigure.amqp;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
@ -30,9 +33,6 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
|
|||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
/**
|
||||
* Tests for {@link RabbitAutoConfiguration}.
|
||||
*
|
||||
|
@ -74,7 +74,7 @@ public class RabbitAutoconfigurationTests {
|
|||
.getBean(CachingConnectionFactory.class);
|
||||
assertEquals("remote-server", connectionFactory.getHost());
|
||||
assertEquals(9000, connectionFactory.getPort());
|
||||
assertEquals("/vhost", connectionFactory.getVirtualHost());
|
||||
assertEquals("vhost", connectionFactory.getVirtualHost());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -89,18 +89,6 @@ public class RabbitAutoconfigurationTests {
|
|||
assertEquals("/", connectionFactory.getVirtualHost());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRabbitTemplateVirtualHostMissingSlash() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(TestConfiguration.class, RabbitAutoConfiguration.class);
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"spring.rabbitmq.virtual_host:foo");
|
||||
this.context.refresh();
|
||||
CachingConnectionFactory connectionFactory = this.context
|
||||
.getBean(CachingConnectionFactory.class);
|
||||
assertEquals("/foo", connectionFactory.getVirtualHost());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRabbitTemplateDefaultVirtualHost() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
|
|
|
@ -16,11 +16,11 @@
|
|||
|
||||
package org.springframework.boot.autoconfigure.amqp;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Tests for {@link RabbitProperties}.
|
||||
*
|
||||
|
@ -28,7 +28,7 @@ import static org.junit.Assert.assertNull;
|
|||
*/
|
||||
public class RabbitPropertiesTests {
|
||||
|
||||
private RabbitProperties properties = new RabbitProperties();
|
||||
private final RabbitProperties properties = new RabbitProperties();
|
||||
|
||||
@Test
|
||||
public void addressesNotSet() {
|
||||
|
@ -50,4 +50,28 @@ public class RabbitPropertiesTests {
|
|||
assertEquals(9999, this.properties.getPort());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultVirtualHost() {
|
||||
this.properties.setVirtualHost("/");
|
||||
assertEquals("/", this.properties.getVirtualHost());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testemptyVirtualHost() {
|
||||
this.properties.setVirtualHost("");
|
||||
assertEquals("/", this.properties.getVirtualHost());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomVirtualHost() {
|
||||
this.properties.setVirtualHost("myvHost");
|
||||
assertEquals("myvHost", this.properties.getVirtualHost());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomFalsyVirtualHost() {
|
||||
this.properties.setVirtualHost("/myvHost");
|
||||
assertEquals("myvHost", this.properties.getVirtualHost());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue