|
| 1 | +package org.springframework.cloud.gateway.server.mvc.handler; |
| 2 | + |
| 3 | +import org.apache.commons.lang3.StringUtils; |
| 4 | +import org.junit.jupiter.api.BeforeEach; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +import org.springframework.cloud.gateway.server.mvc.common.MvcUtils; |
| 8 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 9 | + |
| 10 | +import java.nio.charset.StandardCharsets; |
| 11 | +import java.util.Map; |
| 12 | + |
| 13 | +import static org.assertj.core.api.Assertions.assertThat; |
| 14 | +import static org.mockito.Mockito.never; |
| 15 | +import static org.mockito.Mockito.spy; |
| 16 | +import static org.mockito.Mockito.verify; |
| 17 | + |
| 18 | +public class GatewayMvcMultipartResolverTest { |
| 19 | + private MockHttpServletRequest mockRequest; |
| 20 | + |
| 21 | + @BeforeEach |
| 22 | + public void setUp() { |
| 23 | + mockRequest = new MockHttpServletRequest(); |
| 24 | + mockRequest.setContentType("multipart/form-data; boundary=----boundary"); |
| 25 | + } |
| 26 | + |
| 27 | + private GatewayMvcMultipartResolver.GatewayMultipartHttpServletRequest buildWrapper() { |
| 28 | + return new GatewayMvcMultipartResolver.GatewayMultipartHttpServletRequest(mockRequest); |
| 29 | + } |
| 30 | + |
| 31 | + private void makeGatewayRequest() { |
| 32 | + mockRequest.setAttribute(MvcUtils.GATEWAY_ROUTE_ID_ATTR, "my-route"); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void getParameterMapGatewayRequestMultipleDistinctParams() { |
| 37 | + makeGatewayRequest(); |
| 38 | + mockRequest.setQueryString("foo=bar&baz=qux"); |
| 39 | + |
| 40 | + Map<String, String[]> result = buildWrapper().getParameterMap(); |
| 41 | + |
| 42 | + assertThat(result).containsKeys("foo", "baz"); |
| 43 | + assertThat(result.get("foo")).containsExactly("bar"); |
| 44 | + assertThat(result.get("baz")).containsExactly("qux"); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void getParameterMapGatewayRequestNullQueryStringReturnsEmptyMap() { |
| 49 | + makeGatewayRequest(); |
| 50 | + mockRequest.setQueryString(null); |
| 51 | + |
| 52 | + Map<String, String[]> result = buildWrapper().getParameterMap(); |
| 53 | + |
| 54 | + assertThat(result).isEmpty(); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void getParameterMapGatewayRequestEmptyQueryStringReturnsEmptyMap() { |
| 59 | + makeGatewayRequest(); |
| 60 | + mockRequest.setQueryString(StringUtils.EMPTY); |
| 61 | + |
| 62 | + Map<String, String[]> result = buildWrapper().getParameterMap(); |
| 63 | + |
| 64 | + assertThat(result).isEmpty(); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void getParameterMap_gatewayRequest_multipartBodyIsNotParsed() { |
| 69 | + makeGatewayRequest(); |
| 70 | + |
| 71 | + String body = """ |
| 72 | + ------TestBoundary\r |
| 73 | + Content-Disposition: form-data; name="file"; filename="test.txt"\r |
| 74 | + Content-Type: text/plain\r |
| 75 | + \r |
| 76 | + file content here\r |
| 77 | + ------TestBoundary\r |
| 78 | + Content-Disposition: form-data; name="field1"\r |
| 79 | + \r |
| 80 | + value1\r |
| 81 | + ------TestBoundary--\r |
| 82 | + """; |
| 83 | + |
| 84 | + mockRequest.setContentType("multipart/form-data; boundary=----TestBoundary"); |
| 85 | + mockRequest.setContent(body.getBytes(StandardCharsets.UTF_8)); |
| 86 | + mockRequest.setQueryString("queryParam=fromQuery"); |
| 87 | + |
| 88 | + GatewayMvcMultipartResolver.GatewayMultipartHttpServletRequest wrapper = |
| 89 | + spy(new GatewayMvcMultipartResolver.GatewayMultipartHttpServletRequest(mockRequest)); |
| 90 | + |
| 91 | + Map<String, String[]> params = wrapper.getParameterMap(); |
| 92 | + |
| 93 | + // multipart parsing isn't triggered |
| 94 | + verify(wrapper, never()).initializeMultipart(); |
| 95 | + |
| 96 | + // Body parts must not appear — only query string should be visible |
| 97 | + assertThat(params).containsOnlyKeys("queryParam"); |
| 98 | + assertThat(params.get("queryParam")).containsExactly("fromQuery"); |
| 99 | + assertThat(params).doesNotContainKey("field1").doesNotContainKey("file"); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void getParameterMapNonGatewayRequestNoAttributesDelegatesToSuper() { |
| 104 | + mockRequest.setParameter("superKey", "superValue"); |
| 105 | + |
| 106 | + Map<String, String[]> result = buildWrapper().getParameterMap(); |
| 107 | + |
| 108 | + assertThat(result).containsKey("superKey"); |
| 109 | + assertThat(result.get("superKey")).containsExactly("superValue"); |
| 110 | + } |
| 111 | +} |
0 commit comments