Do not prepend slash to URIs with a scheme.

See gh-206.
This commit is contained in:
Mark Paluch
2018-03-01 15:31:50 +01:00
parent 6592b69d3b
commit 056f73f0f4
2 changed files with 72 additions and 0 deletions

View File

@@ -184,6 +184,10 @@ public class VaultClients {
*/
private String prepareUriTemplate(String uriTemplate) {
if (uriTemplate.startsWith("http:") || uriTemplate.startsWith("https:")) {
return uriTemplate;
}
if (getBaseUrl() != null) {
if (uriTemplate.startsWith("/") && getBaseUrl().endsWith("/")) {
return uriTemplate.substring(1);

View File

@@ -0,0 +1,68 @@
/*
* 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() {
final VaultEndpoint localhost = VaultEndpoint.create("localhost", 8200);
PrefixAwareUriTemplateHandler handler = new PrefixAwareUriTemplateHandler(
new VaultEndpointProvider() {
@Override
public VaultEndpoint getVaultEndpoint() {
return localhost;
}
});
URI uri = handler.expand("/path/{bar}", "bar");
assertThat(uri).hasHost("localhost").hasPort(8200).hasPath("/v1/path/bar");
}
@Test
public void shouldNotPrefixAbsoluteUrl() {
final VaultEndpoint localhost = VaultEndpoint.create("localhost", 8200);
PrefixAwareUriTemplateHandler handler = new PrefixAwareUriTemplateHandler(
new VaultEndpointProvider() {
@Override
public VaultEndpoint getVaultEndpoint() {
return localhost;
}
});
URI uri = handler.expand("https://foo/path/{bar}", "bar");
assertThat(uri).hasScheme("https").hasHost("foo").hasPort(-1)
.hasPath("/path/bar");
}
}