Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion core/src/main/java/feign/template/UriUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ public static String decode(String value, Charset charset) {
* @return true if the uri is absolute.
*/
public static boolean isAbsolute(String uri) {
return uri != null && uri.regionMatches(true, 0, "http", 0, 4);
return uri != null
&& (uri.regionMatches(true, 0, "http://", 0, 7)
|| uri.regionMatches(true, 0, "https://", 0, 8));
}

/**
Expand Down
31 changes: 25 additions & 6 deletions core/src/test/java/feign/RequestTemplateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

public class RequestTemplateTest {

Expand Down Expand Up @@ -129,12 +131,29 @@ void canInsertAbsoluteHrefWithCaseInsensitiveScheme() {
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These integration-level tests are useful, but UriUtils.isAbsolute is a pure utility method and UriUtilsTest.java is the natural home for direct unit tests of it. Adding a few cases there (e.g. httpbin/get returns false, httpbin.org returns false, HTTP://X returns true) would make the unit contract explicit and easier to find when the method changes in the future.


@Test
void rejectsAbsoluteUriWithCaseInsensitiveScheme() {
for (String scheme : Arrays.asList("HTTP", "HTTPS", "hTtP", "hTtPs")) {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> new RequestTemplate().uri(scheme + "://example.com/path"))
.withMessage("url values must be not be absolute.");
}
void acceptsRelativeUriStartingWithHttp() {
RequestTemplate template =
new RequestTemplate()
.method(HttpMethod.GET)
.uri("httpbin/get")
.target("https://example.com");

assertThat(template).hasUrl("https://example.com/httpbin/get");
}

@Test
void rejectsTargetWithoutScheme() {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> new RequestTemplate().target("httpbin.org"))
.withMessage("target values must be absolute.");
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The for loop over schemes means a single test failure won't tell you which variant broke. The project already uses @ParameterizedTest + @MethodSource for data-driven cases elsewhere; that pattern would be more consistent here and give a clearer failure message per scheme:

@ParameterizedTest
@ValueSource(strings = {"HTTP", "HTTPS", "hTtP", "hTtPs"})
void rejectsAbsoluteUriWithCaseInsensitiveScheme(String scheme) {
  assertThatExceptionOfType(IllegalArgumentException.class)
      .isThrownBy(() -> new RequestTemplate().uri(scheme + "://example.com/path"))
      .withMessage("url values must be not be absolute.");
}

@ParameterizedTest
@ValueSource(strings = {"HTTP", "HTTPS", "hTtP", "hTtPs"})
void rejectsAbsoluteUriWithCaseInsensitiveScheme(String scheme) {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> new RequestTemplate().uri(scheme + "://example.com/path"))
.withMessage("url values must be not be absolute.");
}

@Test
Expand Down
15 changes: 15 additions & 0 deletions core/src/test/java/feign/template/UriUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,24 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.NullSource;
import org.junit.jupiter.params.provider.ValueSource;

class UriUtilsTest {

@ParameterizedTest
@ValueSource(strings = {"http://example.com", "https://example.com", "HTTP://X", "hTtPs://x"})
void isAbsoluteWithHttpSchemeAndSeparator(String uri) {
assertThat(UriUtils.isAbsolute(uri)).isTrue();
}

@ParameterizedTest
@NullSource
@ValueSource(strings = {"httpbin/get", "httpbin.org", "https.x.org", "http:/x", "/path", ""})
void isNotAbsoluteWithoutHttpSchemeAndSeparator(String uri) {
assertThat(UriUtils.isAbsolute(uri)).isFalse();
}

/** pct-encode a String, ensuring that all reserved characters are encoded. */
@Test
void pctEncode() {
Expand Down