diff --git a/spring-vault-core/src/main/java/org/springframework/vault/client/VaultClients.java b/spring-vault-core/src/main/java/org/springframework/vault/client/VaultClients.java index 3314a165..38ce1ea1 100644 --- a/spring-vault-core/src/main/java/org/springframework/vault/client/VaultClients.java +++ b/spring-vault-core/src/main/java/org/springframework/vault/client/VaultClients.java @@ -208,15 +208,20 @@ public class VaultClients { } /** - * Strip/add leading slashes from {@code uriTemplate} depending on wheter the base - * url* has a trailing slash. + * Strip/add leading slashes from {@code uriTemplate} depending on whether the base + * url has a trailing slash. * * @param uriTemplate * @return */ static String prepareUriTemplate(@Nullable String baseUrl, String uriTemplate) { + if (uriTemplate.startsWith("http:") || uriTemplate.startsWith("https:")) { + return uriTemplate; + } + if (baseUrl != null) { + if (uriTemplate.startsWith("/") && baseUrl.endsWith("/")) { return uriTemplate.substring(1); } diff --git a/spring-vault-core/src/test/java/org/springframework/vault/client/VaultClientsUnitTests.java b/spring-vault-core/src/test/java/org/springframework/vault/client/VaultClientsUnitTests.java new file mode 100644 index 00000000..b81361aa --- /dev/null +++ b/spring-vault-core/src/test/java/org/springframework/vault/client/VaultClientsUnitTests.java @@ -0,0 +1,58 @@ +/* + * Copyright 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.vault.client; + +import java.net.URI; + +import org.junit.Test; + +import org.springframework.vault.client.VaultClients.PrefixAwareUriTemplateHandler; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Unit tests for + * {@link org.springframework.vault.client.VaultClients.PrefixAwareUriTemplateHandler}. + * + * @author Mark Paluch + */ +public class VaultClientsUnitTests { + + @Test + public void shouldPrefixRelativeUrl() { + + VaultEndpoint localhost = VaultEndpoint.create("localhost", 8200); + PrefixAwareUriTemplateHandler handler = new PrefixAwareUriTemplateHandler( + () -> localhost); + + URI uri = handler.expand("/path/{bar}", "bar"); + + assertThat(uri).hasHost("localhost").hasPort(8200).hasPath("/v1/path/bar"); + } + + @Test + public void shouldNotPrefixAbsoluteUrl() { + + VaultEndpoint localhost = VaultEndpoint.create("localhost", 8200); + PrefixAwareUriTemplateHandler handler = new PrefixAwareUriTemplateHandler( + () -> localhost); + + URI uri = handler.expand("https://foo/path/{bar}", "bar"); + + assertThat(uri).hasScheme("https").hasHost("foo").hasPort(-1) + .hasPath("/path/bar"); + } +}