Skip to content

Commit

Permalink
remove MultipartFileArgumentResolver; merge its tests into RequestPar…
Browse files Browse the repository at this point in the history
…tArgumentResolverTests

Signed-off-by: Dmitrii Bocharov <bdshadow@gmail.com>
  • Loading branch information
bdshadow committed Sep 8, 2023
1 parent 24f0128 commit c8ee799
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 186 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.service.annotation.PostExchange;
import org.springframework.web.testfixture.servlet.MockMultipartFile;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -47,6 +50,9 @@ class RequestPartArgumentResolverTests {
private final Service service =
HttpServiceProxyFactory.builderFor(this.client).build().createClient(Service.class);

private static final MockMultipartFile mockMultipartFile = new MockMultipartFile(
"testFileName", "originalTestFileName", "text/plain", "test".getBytes());


// Base class functionality should be tested in NamedValueArgumentResolverTests.
// Form data vs query params tested in HttpRequestValuesTests.
Expand All @@ -69,6 +75,54 @@ void requestPart() {
assertThat(map.getFirst("optionalPart").getBody()).isEqualTo("part 4");
}

@Test
void multipartFile() {
this.service.postMultipartFile(mockMultipartFile);
testMultipartFile(mockMultipartFile, "file");
}

@Test
void requestPartMultipartFile() {
this.service.postRequestPartMultipartFile(mockMultipartFile);
testMultipartFile(mockMultipartFile, "myFile");
}

@Test
void requestPartOptionalMultipartFile() {
this.service.postRequestPartOptionalMultipartFile(Optional.of(mockMultipartFile));
testMultipartFile(mockMultipartFile, "file");
}

@Test
void optionalMultipartFile() {
this.service.postOptionalMultipartFile(Optional.empty(), "anotherPart");
Object value = client.getRequestValues().getBodyValue();

assertThat(value).isInstanceOf(MultiValueMap.class);
@SuppressWarnings("unchecked")
MultiValueMap<String, HttpEntity<?>> map = (MultiValueMap<String, HttpEntity<?>>) value;
assertThat(map).containsOnlyKeys("anotherPart");
}

private void testMultipartFile(MultipartFile testFile, String partName) {
Object value = this.client.getRequestValues().getBodyValue();

assertThat(value).isInstanceOf(MultiValueMap.class);
@SuppressWarnings("unchecked")
MultiValueMap<String, HttpEntity<?>> map = (MultiValueMap<String, HttpEntity<?>>) value;
assertThat(map).hasSize(1);

HttpEntity<?> entity = map.getFirst(partName);
assertThat(entity).isNotNull();
assertThat(entity.getBody()).isEqualTo(testFile.getResource());

HttpHeaders headers = entity.getHeaders();
assertThat(headers.getContentType()).isEqualTo(MediaType.TEXT_PLAIN);
assertThat(headers.getContentDisposition().getType()).isEqualTo("form-data");
assertThat(headers.getContentDisposition().getName()).isEqualTo(partName);
assertThat(headers.getContentDisposition().getFilename()).isEqualTo(testFile.getOriginalFilename());
}


private interface Service {

Expand All @@ -78,6 +132,17 @@ void postMultipart(
@RequestPart Mono<String> part3,
@RequestPart Optional<String> optionalPart);

@PostExchange
void postMultipartFile(MultipartFile file);

@PostExchange
void postRequestPartMultipartFile(@RequestPart(name = "myFile") MultipartFile file);

@PostExchange
void postRequestPartOptionalMultipartFile(@RequestPart Optional<MultipartFile> file);

@PostExchange
void postOptionalMultipartFile(Optional<MultipartFile> file, @RequestPart String anotherPart);
}

}

0 comments on commit c8ee799

Please sign in to comment.