From 98e76f67851585f4e58cec851a8145062c089154 Mon Sep 17 00:00:00 2001 From: ileler Date: Tue, 3 Jul 2018 10:26:12 +0800 Subject: [PATCH] Validates Route.uri has a scheme --- .../springframework/cloud/gateway/route/Route.java | 4 +++- .../cloud/gateway/route/RouteTests.java | 14 +++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/route/Route.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/route/Route.java index 42ea5417e..588356911 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/route/Route.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/route/Route.java @@ -114,7 +114,9 @@ public class Route implements Ordered { public B uri(URI uri) { this.uri = uri; - if (this.uri.getPort() < 0 && this.uri.getScheme().startsWith("http")) { + String scheme = this.uri.getScheme(); + Assert.hasText(scheme, "The parameter [" + this.uri + "] format is incorrect, scheme 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; this.uri = UriComponentsBuilder.fromUri(this.uri) diff --git a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/route/RouteTests.java b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/route/RouteTests.java index 9eec371ac..cfdcd33de 100644 --- a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/route/RouteTests.java +++ b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/route/RouteTests.java @@ -17,12 +17,17 @@ package org.springframework.cloud.gateway.route; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import static org.assertj.core.api.Assertions.assertThat; public class RouteTests { + @Rule + public ExpectedException exception = ExpectedException.none(); + @Test public void defeaultHttpPort() { Route route = Route.async().id("1") @@ -47,7 +52,6 @@ public class RouteTests { .hasPort(443); } - @Test public void fullUri() { Route route = Route.async().id("1") @@ -59,4 +63,12 @@ public class RouteTests { .hasScheme("http") .hasPort(8080); } + + @Test + public void nullScheme() { + exception.expect(IllegalArgumentException.class); + Route.async().id("1") + .predicate(exchange -> true) + .uri("/pathonly"); + } }