Matches http scheme regardless of case

Fixes gh-2218
This commit is contained in:
ctlove0523
2021-04-17 08:13:01 +08:00
committed by spencergibb
parent 0bf4dcfaa5
commit 0fd5cef6c8
2 changed files with 37 additions and 2 deletions

View File

@@ -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);

View File

@@ -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();
}