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 b72ed92729
commit 7d6a5c0d0c
2 changed files with 65 additions and 2 deletions

View File

@@ -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);
}

View File

@@ -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");
}
}