Skip to content

Commit

Permalink
Consistent handling of parts in HttpRequestValues
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev authored and mdeinum committed Jun 29, 2023
1 parent 942d54d commit 53cf462
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -389,14 +389,15 @@ public HttpRequestValues build() {
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) {
Expand All @@ -412,10 +413,6 @@ else if (uri != null) {
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) {
Expand All @@ -434,6 +431,10 @@ else if (this.multipartBuilder != null) {
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) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,24 @@ void requestPart() {
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");
}

}

0 comments on commit 53cf462

Please sign in to comment.