Simplify form data handling in HttpRequestValues
Closes gh-29615
This commit is contained in:
parent
913163884a
commit
db53b618c1
|
@ -17,14 +17,11 @@
|
|||
package org.springframework.web.service.invoker;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
|
||||
|
@ -34,7 +31,6 @@ import org.springframework.http.HttpHeaders;
|
|||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.client.MultipartBodyBuilder;
|
||||
import org.springframework.http.codec.FormHttpMessageWriter;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
@ -202,8 +198,6 @@ public final class HttpRequestValues {
|
|||
*/
|
||||
public final static class Builder {
|
||||
|
||||
private static final Function<MultiValueMap<String, String>, byte[]> FORM_DATA_SERIALIZER = new FormDataSerializer();
|
||||
|
||||
@Nullable
|
||||
private HttpMethod httpMethod;
|
||||
|
||||
|
@ -403,7 +397,7 @@ public final class HttpRequestValues {
|
|||
|
||||
if (isFormData) {
|
||||
Assert.isTrue(bodyValue == null && this.body == null, "Expected body or request params, not both");
|
||||
bodyValue = FORM_DATA_SERIALIZER.apply(this.requestParams);
|
||||
bodyValue = new LinkedMultiValueMap<>(this.requestParams);
|
||||
}
|
||||
else if (uri != null) {
|
||||
uri = UriComponentsBuilder.fromUri(uri)
|
||||
|
@ -458,16 +452,4 @@ public final class HttpRequestValues {
|
|||
|
||||
}
|
||||
|
||||
|
||||
private static class FormDataSerializer
|
||||
extends FormHttpMessageWriter implements Function<MultiValueMap<String, String>, byte[]> {
|
||||
|
||||
@Override
|
||||
public byte[] apply(MultiValueMap<String, String> requestParams) {
|
||||
Charset charset = StandardCharsets.UTF_8;
|
||||
return serializeForm(requestParams, charset).getBytes(charset);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
package org.springframework.web.service.invoker;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
|
@ -29,7 +30,6 @@ import org.springframework.http.MediaType;
|
|||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
|
@ -58,8 +58,9 @@ public class HttpRequestValuesTests {
|
|||
.build();
|
||||
|
||||
Object body = requestValues.getBodyValue();
|
||||
assertThat(body).isNotNull().isInstanceOf(byte[].class);
|
||||
assertThat(new String((byte[]) body, UTF_8)).isEqualTo("param1=1st+value¶m2=2nd+value+A¶m2=2nd+value+B");
|
||||
assertThat((MultiValueMap<String, String>) body).hasSize(2)
|
||||
.containsEntry("param1", List.of("1st value"))
|
||||
.containsEntry("param2", List.of("2nd value A", "2nd value B"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -16,13 +16,15 @@
|
|||
|
||||
package org.springframework.web.service.invoker;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.service.annotation.PostExchange;
|
||||
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
|
@ -58,8 +60,10 @@ public class RequestParamArgumentResolverTests {
|
|||
this.service.postForm("value 1", "value 2");
|
||||
|
||||
Object body = this.client.getRequestValues().getBodyValue();
|
||||
assertThat(body).isNotNull().isInstanceOf(byte[].class);
|
||||
assertThat(new String((byte[]) body, UTF_8)).isEqualTo("param1=value+1¶m2=value+2");
|
||||
assertThat(body).isNotNull().isInstanceOf(MultiValueMap.class);
|
||||
assertThat((MultiValueMap<String, String>) body).hasSize(2)
|
||||
.containsEntry("param1", List.of("value 1"))
|
||||
.containsEntry("param2", List.of("value 2"));
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -26,6 +26,7 @@ import java.util.function.Consumer;
|
|||
|
||||
import okhttp3.mockwebserver.MockResponse;
|
||||
import okhttp3.mockwebserver.MockWebServer;
|
||||
import okhttp3.mockwebserver.RecordedRequest;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
@ -33,10 +34,14 @@ import reactor.core.publisher.Mono;
|
|||
import reactor.test.StepVerifier;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestAttribute;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import org.springframework.web.service.annotation.GetExchange;
|
||||
import org.springframework.web.service.annotation.PostExchange;
|
||||
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
@ -68,8 +73,7 @@ public class WebClientHttpServiceProxyTests {
|
|||
|
||||
|
||||
@Test
|
||||
void greeting() throws Exception {
|
||||
|
||||
void greeting() {
|
||||
prepareResponse(response ->
|
||||
response.setHeader("Content-Type", "text/plain").setBody("Hello Spring!"));
|
||||
|
||||
|
@ -81,7 +85,6 @@ public class WebClientHttpServiceProxyTests {
|
|||
|
||||
@Test
|
||||
void greetingWithRequestAttribute() {
|
||||
|
||||
Map<String, Object> attributes = new HashMap<>();
|
||||
|
||||
WebClient webClient = WebClient.builder()
|
||||
|
@ -115,6 +118,21 @@ public class WebClientHttpServiceProxyTests {
|
|||
assertThat(this.server.takeRequest().getRequestUrl().uri()).isEqualTo(dynamicUri);
|
||||
}
|
||||
|
||||
@Test
|
||||
void formData() throws Exception {
|
||||
prepareResponse(response -> response.setResponseCode(201));
|
||||
|
||||
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
|
||||
map.add("param1", "value 1");
|
||||
map.add("param2", "value 2");
|
||||
|
||||
initHttpService().postForm(map);
|
||||
|
||||
RecordedRequest request = this.server.takeRequest();
|
||||
assertThat(request.getHeaders().get("Content-Type")).isEqualTo("application/x-www-form-urlencoded;charset=UTF-8");
|
||||
assertThat(request.getBody().readUtf8()).isEqualTo("param1=value+1¶m2=value+2");
|
||||
}
|
||||
|
||||
private TestHttpService initHttpService() {
|
||||
WebClient webClient = WebClient.builder().baseUrl(this.server.url("/").toString()).build();
|
||||
return initHttpService(webClient);
|
||||
|
@ -145,6 +163,9 @@ public class WebClientHttpServiceProxyTests {
|
|||
@GetExchange("/greetings/{id}")
|
||||
String getGreetingById(@Nullable URI uri, @PathVariable String id);
|
||||
|
||||
@PostExchange(contentType = "application/x-www-form-urlencoded")
|
||||
void postForm(@RequestParam MultiValueMap<String, String> params);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue