Browse Source

Removes trailing slash from the URL just when just present

pull/263/merge
Thiago Moretto 10 years ago committed by Adrian Cole
parent
commit
0a95151c95
  1. 18
      core/src/main/java/feign/RequestTemplate.java
  2. 49
      core/src/test/java/feign/FeignBuilderTest.java

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

@ -89,6 +89,18 @@ public final class RequestTemplate implements Serializable { @@ -89,6 +89,18 @@ public final class RequestTemplate implements Serializable {
}
}
private static boolean isHttpUrl(CharSequence value) {
return value.length() >= 4 && value.subSequence(0, 3).equals("http".substring(0, 3));
}
private static CharSequence removeTrailingSlash(CharSequence charSequence) {
if (charSequence != null && charSequence.length() > 0 && charSequence.charAt(charSequence.length() - 1) == '/') {
return charSequence.subSequence(0, charSequence.length() - 1);
} else {
return charSequence;
}
}
/**
* Expands a {@code template}, such as {@code username}, using the {@code variables} supplied. Any
* unresolved parameters will remain. <br> Note that if you'd like curly braces literally in the
@ -249,6 +261,12 @@ public final class RequestTemplate implements Serializable { @@ -249,6 +261,12 @@ public final class RequestTemplate implements Serializable {
/* @see #url() */
public RequestTemplate insert(int pos, CharSequence value) {
if(isHttpUrl(value)) {
value = removeTrailingSlash(value);
if(url.length() > 0 && url.charAt(0) != '/') {
url.insert(0, '/');
}
}
url.insert(pos, pullAnyQueriesOutOfUrl(new StringBuilder(value)));
return this;
}

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

@ -54,6 +54,50 @@ public class FeignBuilderTest { @@ -54,6 +54,50 @@ public class FeignBuilderTest {
.hasBody("request data");
}
@Test
public void testUrlPathConcatUrlTrailingSlash() throws Exception {
server.enqueue(new MockResponse().setBody("response data"));
String url = "http://localhost:" + server.getPort() + "/";
TestInterface api = Feign.builder().target(TestInterface.class, url);
api.codecPost("request data");
assertThat(server.takeRequest()).hasPath("/");
}
@Test
public void testUrlPathConcatNoPathOnRequestLine() throws Exception {
server.enqueue(new MockResponse().setBody("response data"));
String url = "http://localhost:" + server.getPort() + "/";
TestInterface api = Feign.builder().target(TestInterface.class, url);
api.getNoPath();
assertThat(server.takeRequest()).hasPath("/");
}
@Test
public void testUrlPathConcatNoInitialSlashOnPath() throws Exception {
server.enqueue(new MockResponse().setBody("response data"));
String url = "http://localhost:" + server.getPort() + "/";
TestInterface api = Feign.builder().target(TestInterface.class, url);
api.getNoInitialSlashOnSlash();
assertThat(server.takeRequest()).hasPath("/api/thing");
}
@Test
public void testUrlPathConcatNoInitialSlashOnPathNoTrailingSlashOnUrl() throws Exception {
server.enqueue(new MockResponse().setBody("response data"));
String url = "http://localhost:" + server.getPort();
TestInterface api = Feign.builder().target(TestInterface.class, url);
api.getNoInitialSlashOnSlash();
assertThat(server.takeRequest()).hasPath("/api/thing");
}
@Test
public void testOverrideEncoder() throws Exception {
server.enqueue(new MockResponse().setBody("response data"));
@ -143,6 +187,11 @@ public class FeignBuilderTest { @@ -143,6 +187,11 @@ public class FeignBuilderTest {
}
interface TestInterface {
@RequestLine("GET")
Response getNoPath();
@RequestLine("GET api/thing")
Response getNoInitialSlashOnSlash();
@RequestLine("POST /")
Response codecPost(String data);

Loading…
Cancel
Save