Skip to content

Commit 0bc994e

Browse files
Thiago MorettoAdrian Cole
authored andcommitted
Removes trailing slash from the URL just when just present
1 parent c32c8eb commit 0bc994e

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

‎core/src/main/java/feign/RequestTemplate.java‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@ private static String urlEncode(Object arg) {
8989
}
9090
}
9191

92+
private static boolean isHttpUrl(CharSequence value) {
93+
return value.length() >= 4 && value.subSequence(0, 3).equals("http".substring(0, 3));
94+
}
95+
96+
private static CharSequence removeTrailingSlash(CharSequence charSequence) {
97+
if (charSequence != null && charSequence.length() > 0 && charSequence.charAt(charSequence.length() - 1) == '/') {
98+
return charSequence.subSequence(0, charSequence.length() - 1);
99+
} else {
100+
return charSequence;
101+
}
102+
}
103+
92104
/**
93105
* Expands a {@code template}, such as {@code username}, using the {@code variables} supplied. Any
94106
* unresolved parameters will remain. <br> Note that if you'd like curly braces literally in the
@@ -249,6 +261,12 @@ public RequestTemplate append(CharSequence value) {
249261

250262
/* @see #url() */
251263
public RequestTemplate insert(int pos, CharSequence value) {
264+
if(isHttpUrl(value)) {
265+
value = removeTrailingSlash(value);
266+
if(url.length() > 0 && url.charAt(0) != '/') {
267+
url.insert(0, '/');
268+
}
269+
}
252270
url.insert(pos, pullAnyQueriesOutOfUrl(new StringBuilder(value)));
253271
return this;
254272
}

‎core/src/test/java/feign/FeignBuilderTest.java‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,50 @@ public void testDefaults() throws Exception {
5454
.hasBody("request data");
5555
}
5656

57+
@Test
58+
public void testUrlPathConcatUrlTrailingSlash() throws Exception {
59+
server.enqueue(new MockResponse().setBody("response data"));
60+
61+
String url = "http://localhost:" + server.getPort() + "/";
62+
TestInterface api = Feign.builder().target(TestInterface.class, url);
63+
64+
api.codecPost("request data");
65+
assertThat(server.takeRequest()).hasPath("/");
66+
}
67+
68+
@Test
69+
public void testUrlPathConcatNoPathOnRequestLine() throws Exception {
70+
server.enqueue(new MockResponse().setBody("response data"));
71+
72+
String url = "http://localhost:" + server.getPort() + "/";
73+
TestInterface api = Feign.builder().target(TestInterface.class, url);
74+
75+
api.getNoPath();
76+
assertThat(server.takeRequest()).hasPath("/");
77+
}
78+
79+
@Test
80+
public void testUrlPathConcatNoInitialSlashOnPath() throws Exception {
81+
server.enqueue(new MockResponse().setBody("response data"));
82+
83+
String url = "http://localhost:" + server.getPort() + "/";
84+
TestInterface api = Feign.builder().target(TestInterface.class, url);
85+
86+
api.getNoInitialSlashOnSlash();
87+
assertThat(server.takeRequest()).hasPath("/api/thing");
88+
}
89+
90+
@Test
91+
public void testUrlPathConcatNoInitialSlashOnPathNoTrailingSlashOnUrl() throws Exception {
92+
server.enqueue(new MockResponse().setBody("response data"));
93+
94+
String url = "http://localhost:" + server.getPort();
95+
TestInterface api = Feign.builder().target(TestInterface.class, url);
96+
97+
api.getNoInitialSlashOnSlash();
98+
assertThat(server.takeRequest()).hasPath("/api/thing");
99+
}
100+
57101
@Test
58102
public void testOverrideEncoder() throws Exception {
59103
server.enqueue(new MockResponse().setBody("response data"));
@@ -143,6 +187,11 @@ public InvocationHandler create(Target target, Map<Method, MethodHandler> dispat
143187
}
144188

145189
interface TestInterface {
190+
@RequestLine("GET")
191+
Response getNoPath();
192+
193+
@RequestLine("GET api/thing")
194+
Response getNoInitialSlashOnSlash();
146195

147196
@RequestLine("POST /")
148197
Response codecPost(String data);

0 commit comments

Comments
 (0)