Support RabbitMQ URLs with URL-encoded vhost.
This commit is contained in:
@@ -75,8 +75,8 @@ public class CloudFoundryConnectorAmqpServiceTest extends AbstractCloudFoundryCo
|
||||
public void rabbitServiceCreationMultipleUris() {
|
||||
when(mockEnvironment.getEnvValue("VCAP_SERVICES"))
|
||||
.thenReturn(getServicesPayload(
|
||||
getRabbitServicePayloadMultipleUris("rabbit-1", hostname, hostname2, port, username, password, "q-1", "vhost1"),
|
||||
getRabbitServicePayloadMultipleUris("rabbit-2", hostname, hostname2, port, username, password, "q-2", "vhost2")));
|
||||
getRabbitServicePayloadMultipleUris("rabbit-1", hostname, hostname2, port, username, password, "q-1", "v%2Fhost1"),
|
||||
getRabbitServicePayloadMultipleUris("rabbit-2", hostname, hostname2, port, username, password, "q-2", "v%2Fhost2")));
|
||||
|
||||
List<ServiceInfo> serviceInfos = testCloudConnector.getServiceInfos();
|
||||
assertServiceFoundOfType(serviceInfos, "rabbit-1", AmqpServiceInfo.class);
|
||||
@@ -85,6 +85,7 @@ public class CloudFoundryConnectorAmqpServiceTest extends AbstractCloudFoundryCo
|
||||
AmqpServiceInfo amqpServiceInfo = (AmqpServiceInfo) serviceInfos.get(0);
|
||||
assertNotNull(amqpServiceInfo.getUri());
|
||||
assertTrue(amqpServiceInfo.getUri().contains(hostname));
|
||||
assertEquals("v/host1", amqpServiceInfo.getVirtualHost());
|
||||
assertNotNull(amqpServiceInfo.getManagementUri());
|
||||
assertTrue(amqpServiceInfo.getManagementUri().contains(hostname));
|
||||
|
||||
|
||||
@@ -5,6 +5,8 @@ import org.springframework.cloud.service.ServiceInfo.ServiceLabel;
|
||||
import org.springframework.cloud.service.UriBasedServiceInfo;
|
||||
import org.springframework.cloud.util.UriInfo;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -25,6 +27,8 @@ public class AmqpServiceInfo extends UriBasedServiceInfo {
|
||||
private List<String> uris;
|
||||
private List<String> managementUris;
|
||||
|
||||
private String virtualHost;
|
||||
|
||||
public AmqpServiceInfo(String id, String host, int port, String username, String password, String virtualHost) {
|
||||
this(id, host, port, username, password, virtualHost, null);
|
||||
}
|
||||
@@ -32,6 +36,7 @@ public class AmqpServiceInfo extends UriBasedServiceInfo {
|
||||
public AmqpServiceInfo(String id, String host, int port, String username, String password, String virtualHost, String managementUri) {
|
||||
super(id, AMQP_SCHEME, host, port, username, password, virtualHost);
|
||||
this.managementUri = managementUri;
|
||||
this.virtualHost = decode(getUriInfo().getPath());
|
||||
}
|
||||
|
||||
public AmqpServiceInfo(String id, String uri, String managementUri, List<String> uris, List<String> managementUris) {
|
||||
@@ -47,11 +52,24 @@ public class AmqpServiceInfo extends UriBasedServiceInfo {
|
||||
public AmqpServiceInfo(String id, String uri, String managementUri) throws CloudException {
|
||||
super(id, uri);
|
||||
this.managementUri = managementUri;
|
||||
this.virtualHost = decode(getUriInfo().getPath());
|
||||
}
|
||||
|
||||
private String decode(String value) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return URLDecoder.decode(value, "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
@ServiceProperty(category="connection")
|
||||
public String getVirtualHost() {
|
||||
return getUriInfo().getPath();
|
||||
return virtualHost;
|
||||
}
|
||||
|
||||
@ServiceProperty(category="connection")
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
package org.springframework.cloud.service.common;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Ramnivas Laddad
|
||||
*
|
||||
*/
|
||||
public class AmqpServiceInfoTest {
|
||||
@Test
|
||||
public void allArgs() {
|
||||
AmqpServiceInfo serviceInfo = new AmqpServiceInfo("id", "myhost", 12345, "myuser", "mypass", "myvhost");
|
||||
|
||||
assertEquals("myhost", serviceInfo.getHost());
|
||||
assertEquals(12345, serviceInfo.getPort());
|
||||
assertEquals("myuser", serviceInfo.getUserName());
|
||||
assertEquals("mypass", serviceInfo.getPassword());
|
||||
assertEquals("myvhost", serviceInfo.getVirtualHost());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allArgsEncodedVhost() {
|
||||
AmqpServiceInfo serviceInfo = new AmqpServiceInfo("id", "myhost", 12345, "myuser", "mypass", "my%2Fvhost");
|
||||
|
||||
assertEquals("myhost", serviceInfo.getHost());
|
||||
assertEquals(12345, serviceInfo.getPort());
|
||||
assertEquals("myuser", serviceInfo.getUserName());
|
||||
assertEquals("mypass", serviceInfo.getPassword());
|
||||
assertEquals("my/vhost", serviceInfo.getVirtualHost());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allArgsEncodedRootVhost() {
|
||||
AmqpServiceInfo serviceInfo = new AmqpServiceInfo("id", "myhost", 12345, "myuser", "mypass", "%2F");
|
||||
|
||||
assertEquals("myhost", serviceInfo.getHost());
|
||||
assertEquals(12345, serviceInfo.getPort());
|
||||
assertEquals("myuser", serviceInfo.getUserName());
|
||||
assertEquals("mypass", serviceInfo.getPassword());
|
||||
assertEquals("/", serviceInfo.getVirtualHost());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void uriBasedParsing() {
|
||||
AmqpServiceInfo serviceInfo = new AmqpServiceInfo("id", "amqp://myuser:mypass@myhost:12345/myvhost");
|
||||
|
||||
assertEquals("myhost", serviceInfo.getHost());
|
||||
assertEquals(12345, serviceInfo.getPort());
|
||||
assertEquals("myuser", serviceInfo.getUserName());
|
||||
assertEquals("mypass", serviceInfo.getPassword());
|
||||
assertEquals("myvhost", serviceInfo.getVirtualHost());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void uriBasedParsingEncodedVhost() {
|
||||
AmqpServiceInfo serviceInfo = new AmqpServiceInfo("id", "amqp://myuser:mypass@myhost:12345/my%2Fvhost");
|
||||
|
||||
assertEquals("myhost", serviceInfo.getHost());
|
||||
assertEquals(12345, serviceInfo.getPort());
|
||||
assertEquals("myuser", serviceInfo.getUserName());
|
||||
assertEquals("mypass", serviceInfo.getPassword());
|
||||
assertEquals("my/vhost", serviceInfo.getVirtualHost());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void uriBasedParsingEncodedRootVhost() {
|
||||
AmqpServiceInfo serviceInfo = new AmqpServiceInfo("id", "amqp://myuser:mypass@myhost:12345/%2F");
|
||||
|
||||
assertEquals("myhost", serviceInfo.getHost());
|
||||
assertEquals(12345, serviceInfo.getPort());
|
||||
assertEquals("myuser", serviceInfo.getUserName());
|
||||
assertEquals("mypass", serviceInfo.getPassword());
|
||||
assertEquals("/", serviceInfo.getVirtualHost());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void uriBasedParsingDefaultVhost() {
|
||||
AmqpServiceInfo serviceInfo = new AmqpServiceInfo("id", "amqp://myuser:mypass@myhost:12345/");
|
||||
|
||||
assertEquals("myhost", serviceInfo.getHost());
|
||||
assertEquals(12345, serviceInfo.getPort());
|
||||
assertEquals("myuser", serviceInfo.getUserName());
|
||||
assertEquals("mypass", serviceInfo.getPassword());
|
||||
assertNull(serviceInfo.getVirtualHost());
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void missingScheme() {
|
||||
new AmqpServiceInfo("id", "://myuser:mypass@:12345/myvhost");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void amqpsSchemeAccepted() {
|
||||
AmqpServiceInfo serviceInfo = new AmqpServiceInfo("id", "amqps://myuser:mypass@myhost:12345/myvhost");
|
||||
assertEquals("amqps", serviceInfo.getScheme());
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void missingHost() {
|
||||
new AmqpServiceInfo("id", "amqp://myuser:mypass@:12345/myvhost");
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void badUserInfo() {
|
||||
new AmqpServiceInfo("id", "amqp://myuser@myhost/myvhost");
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void missingUserInfo() {
|
||||
new AmqpServiceInfo("id", "amqp://myhost:12345/myvhost");
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void badVirtualHost() {
|
||||
new AmqpServiceInfo("id", "amqp://myuser:mypass@myhost:12345/a/b");
|
||||
}
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
package org.springframework.cloud.service.rabbit;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.cloud.service.common.AmqpServiceInfo;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Ramnivas Laddad
|
||||
*
|
||||
*/
|
||||
public class RabbitServiceInfoTest {
|
||||
@Test
|
||||
public void uriBasedParsing() {
|
||||
AmqpServiceInfo serviceInfo = new AmqpServiceInfo("id", "amqp://myuser:mypass@myhost:12345/myvhost");
|
||||
|
||||
assertEquals("myhost", serviceInfo.getHost());
|
||||
assertEquals(12345, serviceInfo.getPort());
|
||||
assertEquals("myuser", serviceInfo.getUserName());
|
||||
assertEquals("mypass", serviceInfo.getPassword());
|
||||
assertEquals("myvhost", serviceInfo.getVirtualHost());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void uriBasedParsingDefaultVhost() {
|
||||
AmqpServiceInfo serviceInfo = new AmqpServiceInfo("id", "amqp://myuser:mypass@myhost:12345/");
|
||||
|
||||
assertEquals("myhost", serviceInfo.getHost());
|
||||
assertEquals(12345, serviceInfo.getPort());
|
||||
assertEquals("myuser", serviceInfo.getUserName());
|
||||
assertEquals("mypass", serviceInfo.getPassword());
|
||||
assertNull(serviceInfo.getVirtualHost());
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void missingScheme() {
|
||||
new AmqpServiceInfo("id", "://myuser:mypass@:12345/myvhost");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void amqpsSchemeAccepted() {
|
||||
AmqpServiceInfo serviceInfo = new AmqpServiceInfo("id", "amqps://myuser:mypass@myhost:12345/myvhost");
|
||||
assertEquals("amqps", serviceInfo.getScheme());
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void missingHost() {
|
||||
new AmqpServiceInfo("id", "amqp://myuser:mypass@:12345/myvhost");
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void badUserInfo() {
|
||||
new AmqpServiceInfo("id", "amqp://myuser@myhost/myvhost");
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void missingUserInfo() {
|
||||
new AmqpServiceInfo("id", "amqp://myhost:12345/myvhost");
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void badVirtualHost() {
|
||||
new AmqpServiceInfo("id", "amqp://myuser:mypass@myhost:12345/a/b");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user