Skip to content

Commit

Permalink
Validate URI has host unless scheme is forward
Browse files Browse the repository at this point in the history
Fixes gh-2919
  • Loading branch information
spencergibb committed Mar 12, 2024
1 parent f69bb58 commit de105a1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ public B uri(URI uri) {
this.uri = uri;
String scheme = this.uri.getScheme();
Assert.hasText(scheme, "The parameter [" + this.uri + "] format is incorrect, scheme can not be empty");
if (!scheme.equalsIgnoreCase("forward")) {
Assert.hasText(this.uri.getHost(),
"The parameter [" + this.uri + "] format is incorrect, host can not be empty");
}
if (this.uri.getPort() < 0 && scheme.startsWith("http")) {
// default known http ports
int port = this.uri.getScheme().equals("https") ? 443 : 80;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,26 @@ public void nullScheme() {
.isInstanceOf(IllegalArgumentException.class);
}

@Test
public void emptyHostFails() {
assertThatThrownBy(() -> Route.async().id("1").predicate(exchange -> true).uri("localhost:8080"))
.isInstanceOf(IllegalArgumentException.class);
}

@Test
public void noOpWorks() {
Route route = Route.async().id("1").predicate(exchange -> true).uri("no://op").build();

assertThat(route.getUri()).hasScheme("no").hasHost("op");
}

@Test
public void forwardWorks() {
Route route = Route.async().id("1").predicate(exchange -> true).uri("forward:/some/path").build();

assertThat(route.getUri()).hasScheme("forward").hasPath("/some/path");
}

@Test
public void defaultMetadataToEmpty() {
Route route = Route.async().id("1").predicate(exchange -> true).uri("http://acme.com:8080").build();
Expand Down

0 comments on commit de105a1

Please sign in to comment.