Commit 6a826441 by Ryan Baxter

Don’t add URL protocol when url is set using EL. Fixes #1522.

parent ac3401bc
...@@ -243,7 +243,7 @@ class FeignClientsRegistrar implements ImportBeanDefinitionRegistrar, ...@@ -243,7 +243,7 @@ class FeignClientsRegistrar implements ImportBeanDefinitionRegistrar,
private String getUrl(Map<String, Object> attributes) { private String getUrl(Map<String, Object> attributes) {
String url = resolve((String) attributes.get("url")); String url = resolve((String) attributes.get("url"));
if (StringUtils.hasText(url)) { if (StringUtils.hasText(url) && !(url.startsWith("#{") && url.endsWith("}"))) {
if (!url.contains("://")) { if (!url.contains("://")) {
url = "http://" + url; url = "http://" + url;
} }
......
...@@ -30,6 +30,7 @@ import org.junit.BeforeClass; ...@@ -30,6 +30,7 @@ import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
...@@ -75,6 +76,11 @@ public class FeignHttpClientUrlTests { ...@@ -75,6 +76,11 @@ public class FeignHttpClientUrlTests {
@Autowired @Autowired
private UrlClient urlClient; private UrlClient urlClient;
@Autowired
private BeanUrlClient beanClient;
@Autowired BeanUrlClientNoProtocol beanClientNoProtocol;
// this tests that // this tests that
@FeignClient(name = "localappurl", url = "http://localhost:${server.port}/") @FeignClient(name = "localappurl", url = "http://localhost:${server.port}/")
protected interface UrlClient { protected interface UrlClient {
...@@ -82,17 +88,41 @@ public class FeignHttpClientUrlTests { ...@@ -82,17 +88,41 @@ public class FeignHttpClientUrlTests {
Hello getHello(); Hello getHello();
} }
@FeignClient(name = "beanappurl", url = "#{SERVER_URL}")
protected interface BeanUrlClient {
@RequestMapping(method = RequestMethod.GET, value = "/hello")
Hello getHello();
}
@FeignClient(name = "beanappurlnoprotocol", url = "#{SERVER_URL_NO_PROTOCOL}")
protected interface BeanUrlClientNoProtocol {
@RequestMapping(method = RequestMethod.GET, value = "/hello")
Hello getHello();
}
@Configuration @Configuration
@EnableAutoConfiguration @EnableAutoConfiguration
@RestController @RestController
@EnableFeignClients(clients = { UrlClient.class }) @EnableFeignClients(clients = { UrlClient.class, BeanUrlClient.class, BeanUrlClientNoProtocol.class })
protected static class TestConfig { protected static class TestConfig {
@Value("${server.port}")
private int port;
@RequestMapping(method = RequestMethod.GET, value = "/hello") @RequestMapping(method = RequestMethod.GET, value = "/hello")
public Hello getHello() { public Hello getHello() {
return new Hello("hello world 1"); return new Hello("hello world 1");
} }
@Bean(name="SERVER_URL")
public String serverUrl() {
return "http://localhost:" + port + "/";
}
@Bean(name="SERVER_URL_NO_PROTOCOL")
public String serverUrlNoProtocol() {
return "localhost:" + port + "/";
}
@Bean @Bean
public Targeter feignTargeter() { public Targeter feignTargeter() {
return new Targeter() { return new Targeter() {
...@@ -122,6 +152,20 @@ public class FeignHttpClientUrlTests { ...@@ -122,6 +152,20 @@ public class FeignHttpClientUrlTests {
assertEquals("first hello didn't match", new Hello("hello world 1"), hello); assertEquals("first hello didn't match", new Hello("hello world 1"), hello);
} }
@Test
public void testBeanUrl() {
Hello hello = this.beanClient.getHello();
assertNotNull("hello was null", hello);
assertEquals("first hello didn't match", new Hello("hello world 1"), hello);
}
@Test
public void testBeanUrlNoProtocol() {
Hello hello = this.beanClientNoProtocol.getHello();
assertNotNull("hello was null", hello);
assertEquals("first hello didn't match", new Hello("hello world 1"), hello);
}
@Data @Data
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment