From 8d6aeebb6199b29dbb63d699ce4ad4b0f405ba0c Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Mon, 22 Aug 2016 15:01:20 -0600 Subject: [PATCH] Feign URI tests updated to be more inclusive. Previously with a `@FeignClient(name="http://bad_hostname")` would pass the start time test and fail later in runtime. The test now fails fast. fixes gh-1257 --- .../netflix/feign/FeignClientsRegistrar.java | 11 ++- .../feign/FeignClientsRegistrarTests.java | 75 +++++++++++++++++++ 2 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/FeignClientsRegistrarTests.java diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/FeignClientsRegistrar.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/FeignClientsRegistrar.java index 9d31319b..9020c6e7 100644 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/FeignClientsRegistrar.java +++ b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/FeignClientsRegistrar.java @@ -201,7 +201,7 @@ class FeignClientsRegistrar implements ImportBeanDefinitionRegistrar, annotation.getAliasedString("name", FeignClient.class, null); } - private String getName(Map attributes) { + /* for testing */ String getName(Map attributes) { String name = (String) attributes.get("serviceId"); if (!StringUtils.hasText(name)) { name = (String) attributes.get("name"); @@ -216,7 +216,14 @@ class FeignClientsRegistrar implements ImportBeanDefinitionRegistrar, String host = null; try { - host = new URI("http://" + name).getHost(); + String url; + if (!name.startsWith("http://") && !name.startsWith("https://")) { + url = "http://" + name; + } else { + url = name; + } + host = new URI(url).getHost(); + } catch (URISyntaxException e) { } diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/FeignClientsRegistrarTests.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/FeignClientsRegistrarTests.java new file mode 100644 index 00000000..fb349686 --- /dev/null +++ b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/FeignClientsRegistrarTests.java @@ -0,0 +1,75 @@ +/* + * Copyright 2013-2016 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.cloud.netflix.feign; + +import java.util.Collections; + +import org.junit.Test; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +/** + * @author Spencer Gibb + */ +public class FeignClientsRegistrarTests { + + @Test(expected = IllegalStateException.class) + public void badNameHttpPrefix() { + testGetName("http://bad_hostname"); + } + + + @Test(expected = IllegalStateException.class) + public void badNameHttpsPrefix() { + testGetName("https://bad_hostname"); + } + + @Test(expected = IllegalStateException.class) + public void badName() { + testGetName("bad_hostname"); + } + + @Test(expected = IllegalStateException.class) + public void badNameStartsWithHttp() { + testGetName("http_bad_hostname"); + } + + @Test + public void goodName() { + String name = testGetName("good-name"); + assertThat("name was wrong", name, is("good-name")); + } + + @Test + public void goodNameHttpPrefix() { + String name = testGetName("http://good-name"); + assertThat("name was wrong", name, is("http://good-name")); + } + + @Test + public void goodNameHttpsPrefix() { + String name = testGetName("https://goodname"); + assertThat("name was wrong", name, is("https://goodname")); + } + + private String testGetName(String name) { + FeignClientsRegistrar registrar = new FeignClientsRegistrar(); + return registrar.getName(Collections.singletonMap("name", name)); + } +}