From 0fd5cef6c81daca9dcc85d09832fa56b28923161 Mon Sep 17 00:00:00 2001 From: ctlove0523 <478309639@qq.com> Date: Sat, 17 Apr 2021 08:13:01 +0800 Subject: [PATCH] Matches http scheme regardless of case Fixes gh-2218 --- .../gateway/filter/NettyRoutingFilter.java | 2 +- .../filter/NettyRoutingFilterTests.java | 37 ++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/NettyRoutingFilter.java b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/NettyRoutingFilter.java index fcd9eda6..60ce0020 100644 --- a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/NettyRoutingFilter.java +++ b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/NettyRoutingFilter.java @@ -109,7 +109,7 @@ public class NettyRoutingFilter implements GlobalFilter, Ordered { String scheme = requestUrl.getScheme(); if (isAlreadyRouted(exchange) - || (!"http".equals(scheme) && !"https".equals(scheme))) { + || (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme))) { return chain.filter(exchange); } setAlreadyRouted(exchange); diff --git a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/NettyRoutingFilterTests.java b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/NettyRoutingFilterTests.java index 149b7a18..76dc1b7b 100644 --- a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/NettyRoutingFilterTests.java +++ b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/NettyRoutingFilterTests.java @@ -17,9 +17,14 @@ package org.springframework.cloud.gateway.filter; import org.hamcrest.Matchers; +import org.junit.Assert; +import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; +import reactor.core.publisher.Mono; +import reactor.netty.DisposableServer; +import reactor.netty.http.server.HttpServer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringBootConfiguration; @@ -27,20 +32,29 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; +import org.springframework.cloud.gateway.test.BaseWebClientTests; import org.springframework.cloud.gateway.test.PermitAllSecurityConfiguration; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Import; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.reactive.server.WebTestClient; +import org.springframework.util.SocketUtils; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -public class NettyRoutingFilterTests { +public class NettyRoutingFilterTests extends BaseWebClientTests { + + private static int port; @Autowired private ApplicationContext context; + @BeforeClass + public static void beforeAll() { + port = SocketUtils.findAvailableTcpPort(); + } + @Test @Ignore public void mockServerWorks() { @@ -50,6 +64,26 @@ public class NettyRoutingFilterTests { .value(Matchers.lessThan(500)); } + @Test // gh-2207 + public void testCaseInsensitiveScheme() { + DisposableServer server = HttpServer.create().port(port).host("127.0.0.1").route( + routes -> routes.get("/issue", (request, response) -> response.sendString(Mono.just("issue2207")))) + .bindNow(); + + try { + testClient.get().uri("/issue").exchange().expectStatus().isOk().expectBody() + .consumeWith(entityExchangeResult -> { + Assert.assertNotNull(entityExchangeResult); + Assert.assertNotNull(entityExchangeResult.getResponseBody()); + String content = new String(entityExchangeResult.getResponseBody()); + Assert.assertEquals("issue2207", content); + }); + } + finally { + server.disposeNow(); + } + } + @SpringBootConfiguration @EnableAutoConfiguration @Import(PermitAllSecurityConfiguration.class) @@ -61,6 +95,7 @@ public class NettyRoutingFilterTests { .route(p -> p.path("/mockexample") .filters(f -> f.prefixPath("/httpbin")) .uri("http://example.com")) + .route(p -> p.path("/issue").uri("HTTP://127.0.0.1:" + port)) .build(); }