|
| 1 | +/* |
| 2 | + * Copyright 2025-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cloud.gateway.server.mvc.handler; |
| 18 | + |
| 19 | +import java.net.URI; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.stream.Stream; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import org.springframework.beans.factory.ObjectProvider; |
| 26 | +import org.springframework.cloud.gateway.server.mvc.common.AbstractProxyExchange; |
| 27 | +import org.springframework.cloud.gateway.server.mvc.common.MvcUtils; |
| 28 | +import org.springframework.cloud.gateway.server.mvc.config.GatewayMvcProperties; |
| 29 | +import org.springframework.cloud.gateway.server.mvc.filter.HttpHeadersFilter.RequestHttpHeadersFilter; |
| 30 | +import org.springframework.cloud.gateway.server.mvc.filter.HttpHeadersFilter.ResponseHttpHeadersFilter; |
| 31 | +import org.springframework.http.HttpHeaders; |
| 32 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 33 | +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; |
| 34 | +import org.springframework.web.servlet.function.ServerRequest; |
| 35 | +import org.springframework.web.servlet.function.ServerResponse; |
| 36 | + |
| 37 | +import static org.assertj.core.api.Assertions.assertThat; |
| 38 | + |
| 39 | +/** |
| 40 | + * @author raccoonback |
| 41 | + */ |
| 42 | +class ProxyExchangeHandlerFunctionTest { |
| 43 | + |
| 44 | + @Test |
| 45 | + void keepOriginalEncodingOfQueryParameter() { |
| 46 | + TestProxyExchange proxyExchange = new TestProxyExchange(); |
| 47 | + ProxyExchangeHandlerFunction function = new ProxyExchangeHandlerFunction(proxyExchange, new ObjectProvider<>() { |
| 48 | + @Override |
| 49 | + public Stream<RequestHttpHeadersFilter> stream() { |
| 50 | + return Stream.of((httpHeaders, serverRequest) -> new HttpHeaders()); |
| 51 | + } |
| 52 | + }, new ObjectProvider<>() { |
| 53 | + @Override |
| 54 | + public Stream<ResponseHttpHeadersFilter> stream() { |
| 55 | + return Stream.of((httpHeaders, serverRequest) -> new HttpHeaders()); |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + function.onApplicationEvent(null); |
| 60 | + |
| 61 | + MockHttpServletRequest servletRequest = MockMvcRequestBuilders |
| 62 | + .get("http://localhost/é?foo=value1 value2&bar=value3=") |
| 63 | + .buildRequest(null); |
| 64 | + servletRequest.setAttribute(MvcUtils.GATEWAY_REQUEST_URL_ATTR, URI.create("http://localhost:8080")); |
| 65 | + ServerRequest request = ServerRequest.create(servletRequest, Collections.emptyList()); |
| 66 | + |
| 67 | + function.handle(request); |
| 68 | + |
| 69 | + URI uri = proxyExchange.getRequest().getUri(); |
| 70 | + |
| 71 | + assertThat(uri).hasToString("http://localhost:8080/%C3%A9?foo=value1%20value2&bar=value3%3D") |
| 72 | + .hasPath("/é") |
| 73 | + .hasParameter("foo", "value1 value2") |
| 74 | + .hasParameter("bar", "value3="); |
| 75 | + } |
| 76 | + |
| 77 | + private class TestProxyExchange extends AbstractProxyExchange { |
| 78 | + |
| 79 | + private Request request; |
| 80 | + |
| 81 | + protected TestProxyExchange() { |
| 82 | + super(new GatewayMvcProperties()); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public ServerResponse exchange(Request request) { |
| 87 | + this.request = request; |
| 88 | + |
| 89 | + return ServerResponse.ok().build(); |
| 90 | + } |
| 91 | + |
| 92 | + public Request getRequest() { |
| 93 | + return request; |
| 94 | + } |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments