Merge branch '6.0.x'

This commit is contained in:
rstoyanchev
2023-05-30 17:18:01 +01:00
8 changed files with 91 additions and 37 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -389,31 +389,30 @@ public final class HttpRequestValues {
Map<String, String> uriVars = (this.uriVars != null ? new HashMap<>(this.uriVars) : Collections.emptyMap());
Object bodyValue = this.bodyValue;
if (this.multipartBuilder != null) {
Assert.isTrue(bodyValue == null && this.body == null, "Expected body or request parts, not both");
bodyValue = this.multipartBuilder.build();
}
if (!CollectionUtils.isEmpty(this.requestParams)) {
boolean isFormData = (this.headers != null &&
MediaType.APPLICATION_FORM_URLENCODED.equals(this.headers.getContentType()));
if (isFormData) {
Assert.isTrue(bodyValue == null && this.body == null, "Expected body or request params, not both");
if (hasContentType(MediaType.APPLICATION_FORM_URLENCODED)) {
Assert.isTrue(this.multipartBuilder == null, "Cannot add parts to form data request");
Assert.isTrue(bodyValue == null && this.body == null, "Cannot set body of form data request");
bodyValue = new LinkedMultiValueMap<>(this.requestParams);
}
else if (uri != null) {
// insert into prepared URI
uri = UriComponentsBuilder.fromUri(uri)
.queryParams(UriUtils.encodeQueryParams(this.requestParams))
.build(true)
.toUri();
}
else {
// append to URI template
uriVars = (uriVars.isEmpty() ? new HashMap<>() : uriVars);
uriTemplate = appendQueryParams(uriTemplate, uriVars, this.requestParams);
}
}
else if (this.multipartBuilder != null) {
Assert.isTrue(bodyValue == null && this.body == null, "Expected body or request parts, not both");
bodyValue = this.multipartBuilder.build();
}
HttpHeaders headers = HttpHeaders.EMPTY;
if (this.headers != null) {
@@ -432,6 +431,10 @@ public final class HttpRequestValues {
bodyValue, this.body, this.bodyElementType);
}
private boolean hasContentType(MediaType mediaType) {
return (this.headers != null && mediaType.equals(this.headers.getContentType()));
}
private String appendQueryParams(
String uriTemplate, Map<String, String> uriVars, MultiValueMap<String, String> requestParams) {

View File

@@ -50,7 +50,7 @@ class HttpRequestValuesTests {
@ParameterizedTest
@ValueSource(strings = {"POST", "PUT", "PATCH"})
@SuppressWarnings("unchecked")
void requestParamAsFormData(String httpMethod) {
void formData(String httpMethod) {
HttpRequestValues requestValues = HttpRequestValues.builder().setHttpMethod(HttpMethod.valueOf(httpMethod))
.setContentType(MediaType.APPLICATION_FORM_URLENCODED)
@@ -65,7 +65,7 @@ class HttpRequestValuesTests {
}
@Test
void requestParamAsQueryParamsInUriTemplate() {
void queryParamsWithUriTemplate() {
HttpRequestValues requestValues = HttpRequestValues.builder().setHttpMethod(HttpMethod.POST)
.setUriTemplate("/path")
@@ -99,23 +99,25 @@ class HttpRequestValuesTests {
}
@Test
void requestParamAsQueryParamsInUri() {
void queryParamsWithPreparedUri() {
URI uri = URI.create("/my%20path");
HttpRequestValues requestValues = HttpRequestValues.builder().setHttpMethod(HttpMethod.POST)
.setUri(URI.create("/path"))
.setUri(uri)
.addRequestParameter("param1", "1st value")
.addRequestParameter("param2", "2nd value A", "2nd value B")
.build();
assertThat(requestValues.getUri().toString())
.isEqualTo("/path?param1=1st%20value&param2=2nd%20value%20A&param2=2nd%20value%20B");
.isEqualTo("/my%20path?param1=1st%20value&param2=2nd%20value%20A&param2=2nd%20value%20B");
}
@Test
void requestPart() {
HttpHeaders entityHeaders = new HttpHeaders();
entityHeaders.add("foo", "bar");
HttpEntity<String> entity = new HttpEntity<>("body", entityHeaders);
HttpHeaders headers = new HttpHeaders();
headers.add("foo", "bar");
HttpEntity<String> entity = new HttpEntity<>("body", headers);
HttpRequestValues requestValues = HttpRequestValues.builder()
.addRequestPart("form field", "form value")
@@ -129,4 +131,24 @@ class HttpRequestValuesTests {
assertThat(map.getFirst("entity")).isEqualTo(entity);
}
@Test
void requestPartAndRequestParam() {
HttpRequestValues requestValues = HttpRequestValues.builder()
.setUriTemplate("/path")
.addRequestPart("form field", "form value")
.addRequestParameter("query param", "query value")
.build();
String uriTemplate = requestValues.getUriTemplate();
assertThat(uriTemplate).isNotNull();
assertThat(uriTemplate).isEqualTo("/path?{queryParam0}={queryParam0[0]}");
@SuppressWarnings("unchecked")
MultiValueMap<String, HttpEntity<?>> map = (MultiValueMap<String, HttpEntity<?>>) requestValues.getBodyValue();
assertThat(map).hasSize(1);
assertThat(map.getFirst("form field").getBody()).isEqualTo("form value");
}
}

View File

@@ -65,9 +65,9 @@ class RequestPartArgumentResolverTests {
Object body = this.client.getRequestValues().getBodyValue();
assertThat(body).isInstanceOf(MultiValueMap.class);
@SuppressWarnings("unchecked")
MultiValueMap<String, HttpEntity<?>> map = (MultiValueMap<String, HttpEntity<?>>) body;
assertThat(map.getFirst("part1").getBody()).isEqualTo("part 1");
assertThat(map.getFirst("part2")).isEqualTo(part2);
assertThat(((Mono<?>) map.getFirst("part3").getBody()).block()).isEqualTo("part 3");