|
| 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.BeansException; |
| 26 | +import org.springframework.beans.factory.ObjectProvider; |
| 27 | +import org.springframework.cloud.gateway.server.mvc.common.AbstractProxyExchange; |
| 28 | +import org.springframework.cloud.gateway.server.mvc.common.MvcUtils; |
| 29 | +import org.springframework.cloud.gateway.server.mvc.config.GatewayMvcProperties; |
| 30 | +import org.springframework.cloud.gateway.server.mvc.filter.HttpHeadersFilter.RequestHttpHeadersFilter; |
| 31 | +import org.springframework.cloud.gateway.server.mvc.filter.HttpHeadersFilter.ResponseHttpHeadersFilter; |
| 32 | +import org.springframework.http.HttpHeaders; |
| 33 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 34 | +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; |
| 35 | +import org.springframework.web.servlet.function.ServerRequest; |
| 36 | +import org.springframework.web.servlet.function.ServerResponse; |
| 37 | + |
| 38 | +import static org.assertj.core.api.Assertions.assertThat; |
| 39 | + |
| 40 | +/** |
| 41 | + * @author raccoonback |
| 42 | + */ |
| 43 | +class ProxyExchangeHandlerFunctionTest { |
| 44 | + |
| 45 | + @Test |
| 46 | + void keepOriginalEncodingOfQueryParameter() { |
| 47 | + TestProxyExchange proxyExchange = new TestProxyExchange(); |
| 48 | + ProxyExchangeHandlerFunction function = new ProxyExchangeHandlerFunction(proxyExchange, new ObjectProvider<>() { |
| 49 | + @Override |
| 50 | + public RequestHttpHeadersFilter getObject() throws BeansException { |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public RequestHttpHeadersFilter getObject(Object... args) throws BeansException { |
| 56 | + return null; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public RequestHttpHeadersFilter getIfAvailable() throws BeansException { |
| 61 | + return null; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public RequestHttpHeadersFilter getIfUnique() throws BeansException { |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public Stream<RequestHttpHeadersFilter> orderedStream() { |
| 71 | + return Stream.of((httpHeaders, serverRequest) -> new HttpHeaders()); |
| 72 | + } |
| 73 | + |
| 74 | + }, new ObjectProvider<>() { |
| 75 | + |
| 76 | + @Override |
| 77 | + public ResponseHttpHeadersFilter getObject() throws BeansException { |
| 78 | + return null; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public ResponseHttpHeadersFilter getObject(Object... args) throws BeansException { |
| 83 | + return null; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public ResponseHttpHeadersFilter getIfAvailable() throws BeansException { |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public ResponseHttpHeadersFilter getIfUnique() throws BeansException { |
| 93 | + return null; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public Stream<ResponseHttpHeadersFilter> orderedStream() { |
| 98 | + return Stream.of((httpHeaders, serverRequest) -> new HttpHeaders()); |
| 99 | + |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + function.onApplicationEvent(null); |
| 104 | + |
| 105 | + MockHttpServletRequest servletRequest = MockMvcRequestBuilders |
| 106 | + .get("http://localhost/é?foo=value1 value2&bar=value3=&qux=value4+") |
| 107 | + .buildRequest(null); |
| 108 | + servletRequest.setAttribute(MvcUtils.GATEWAY_REQUEST_URL_ATTR, URI.create("http://localhost:8080")); |
| 109 | + ServerRequest request = ServerRequest.create(servletRequest, Collections.emptyList()); |
| 110 | + |
| 111 | + function.handle(request); |
| 112 | + |
| 113 | + URI uri = proxyExchange.getRequest().getUri(); |
| 114 | + |
| 115 | + assertThat(uri).hasToString("http://localhost:8080/%C3%A9?foo=value1%20value2&bar=value3%3D&qux=value4%2B") |
| 116 | + .hasPath("/é") |
| 117 | + .hasParameter("foo", "value1 value2") |
| 118 | + .hasParameter("bar", "value3=") |
| 119 | + .hasParameter("qux", "value4+"); |
| 120 | + } |
| 121 | + |
| 122 | + private class TestProxyExchange extends AbstractProxyExchange { |
| 123 | + |
| 124 | + private Request request; |
| 125 | + |
| 126 | + protected TestProxyExchange() { |
| 127 | + super(new GatewayMvcProperties()); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public ServerResponse exchange(Request request) { |
| 132 | + this.request = request; |
| 133 | + |
| 134 | + return ServerResponse.ok().build(); |
| 135 | + } |
| 136 | + |
| 137 | + public Request getRequest() { |
| 138 | + return request; |
| 139 | + } |
| 140 | + |
| 141 | + } |
| 142 | + |
| 143 | +} |
0 commit comments