Skip to content

Commit 9c69c0a

Browse files
committed
RibbonClient use replaceFirst to remove host rather than other parts of the uri.
refactor RibbonClient to clean url in static method fixes OpenFeigngh-221
1 parent 27dc373 commit 9c69c0a

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

ribbon/src/main/java/feign/ribbon/RibbonClient.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public Response execute(Request request, Request.Options options) throws IOExcep
6363
try {
6464
URI asUri = URI.create(request.url());
6565
String clientName = asUri.getHost();
66-
URI uriWithoutHost = URI.create(request.url().replace(asUri.getHost(), ""));
66+
URI uriWithoutHost = cleanUrl(request.url(), clientName);
6767
LBClient.RibbonRequest ribbonRequest =
6868
new LBClient.RibbonRequest(delegate, request, uriWithoutHost);
6969
return lbClient(clientName).executeWithLoadBalancer(ribbonRequest,
@@ -76,6 +76,10 @@ public Response execute(Request request, Request.Options options) throws IOExcep
7676
}
7777
}
7878

79+
static URI cleanUrl(String originalUrl, String host) {
80+
return URI.create(originalUrl.replaceFirst(host, ""));
81+
}
82+
7983
private LBClient lbClient(String clientName) {
8084
return lbClientFactory.create(clientName);
8185
}

ribbon/src/test/java/feign/ribbon/RibbonClientTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static org.junit.Assert.assertThat;
2222

2323
import java.io.IOException;
24+
import java.net.URI;
2425
import java.net.URL;
2526

2627
import org.junit.After;
@@ -172,6 +173,18 @@ public void testFeignOptionsClientConfig() {
172173
assertEquals(2, config.getProperties().size());
173174
}
174175

176+
@Test
177+
public void testCleanUrlWithMatchingHostAndPart() throws IOException {
178+
URI uri = RibbonClient.cleanUrl("http://questions/questions/answer/123", "questions");
179+
assertEquals("http:///questions/answer/123", uri.toString());
180+
}
181+
182+
@Test
183+
public void testCleanUrl() throws IOException {
184+
URI uri = RibbonClient.cleanUrl("http://myservice/questions/answer/123", "myservice");
185+
assertEquals("http:///questions/answer/123", uri.toString());
186+
}
187+
175188
private String client() {
176189
return testName.getMethodName();
177190
}

0 commit comments

Comments
 (0)