Add MultipartFile support to HTTP interface client
See gh-30728
This commit is contained in:
committed by
rstoyanchev
parent
3f40452511
commit
e69a1d22f9
@@ -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.
|
||||
@@ -33,16 +33,20 @@ import org.junit.jupiter.api.Test;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
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.bind.annotation.RequestPart;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
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 org.springframework.web.testfixture.servlet.MockMultipartFile;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -52,6 +56,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* using {@link WebClient} and {@link MockWebServer}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Olga Maciaszek-Sharma
|
||||
*/
|
||||
public class WebClientHttpServiceProxyTests {
|
||||
|
||||
@@ -133,6 +138,25 @@ public class WebClientHttpServiceProxyTests {
|
||||
assertThat(request.getBody().readUtf8()).isEqualTo("param1=value+1¶m2=value+2");
|
||||
}
|
||||
|
||||
@Test // gh-30342
|
||||
void multipart() throws InterruptedException {
|
||||
prepareResponse(response -> response.setResponseCode(201));
|
||||
String fileName = "testFileName";
|
||||
String originalFileName = "originalTestFileName";
|
||||
MultipartFile file = new MockMultipartFile(fileName, originalFileName,
|
||||
MediaType.APPLICATION_JSON_VALUE, "test".getBytes());
|
||||
|
||||
initHttpService().postMultipart(file, "test2");
|
||||
|
||||
RecordedRequest request = this.server.takeRequest();
|
||||
assertThat(request.getHeaders().get("Content-Type")).startsWith("multipart/form-data;boundary=");
|
||||
assertThat(request.getBody().readUtf8())
|
||||
.containsSubsequence("Content-Disposition: form-data; name=\"file\"; filename=\"originalTestFileName\"",
|
||||
"Content-Type: application/json", "Content-Length: 4", "test",
|
||||
"Content-Disposition: form-data; name=\"anotherPart\"",
|
||||
"Content-Type: text/plain;charset=UTF-8", "Content-Length: 5", "test2");
|
||||
}
|
||||
|
||||
private TestHttpService initHttpService() {
|
||||
WebClient webClient = WebClient.builder().baseUrl(this.server.url("/").toString()).build();
|
||||
return initHttpService(webClient);
|
||||
@@ -166,6 +190,9 @@ public class WebClientHttpServiceProxyTests {
|
||||
@PostExchange(contentType = "application/x-www-form-urlencoded")
|
||||
void postForm(@RequestParam MultiValueMap<String, String> params);
|
||||
|
||||
@PostExchange(contentType = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||
void postMultipart(MultipartFile file, @RequestPart String anotherPart);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user