use of org.springframework.web.multipart.MultipartFile in project spring-framework by spring-projects.
the class RequestParamMapMethodArgumentResolverTests method resolveMapOfMultipartFile.
@Test
@SuppressWarnings("unchecked")
public void resolveMapOfMultipartFile() throws Exception {
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
MultipartFile expected1 = new MockMultipartFile("mfile", "Hello World".getBytes());
MultipartFile expected2 = new MockMultipartFile("other", "Hello World 3".getBytes());
request.addFile(expected1);
request.addFile(expected2);
webRequest = new ServletWebRequest(request);
MethodParameter param = this.testMethod.annot(requestParam().noName()).arg(Map.class, String.class, MultipartFile.class);
Object result = resolver.resolveArgument(param, null, webRequest, null);
boolean condition = result instanceof Map;
assertThat(condition).isTrue();
Map<String, MultipartFile> resultMap = (Map<String, MultipartFile>) result;
assertThat(resultMap.size()).isEqualTo(2);
assertThat(resultMap.get("mfile")).isEqualTo(expected1);
assertThat(resultMap.get("other")).isEqualTo(expected2);
}
use of org.springframework.web.multipart.MultipartFile in project spring-framework by spring-projects.
the class ByteArrayMultipartFileEditorTests method setValueAsMultipartFileWithBadBytes.
@Test
public void setValueAsMultipartFileWithBadBytes() throws Exception {
MultipartFile file = mock(MultipartFile.class);
given(file.getBytes()).willThrow(new IOException());
assertThatIllegalArgumentException().isThrownBy(() -> editor.setValue(file));
}
use of org.springframework.web.multipart.MultipartFile in project spring-framework by spring-projects.
the class RequestPartServletServerHttpRequestTests method getBodyWithWrappedRequest.
// SPR-13317
@Test
public void getBodyWithWrappedRequest() throws Exception {
byte[] bytes = "content".getBytes("UTF-8");
MultipartFile part = new MockMultipartFile("part", "", "application/json", bytes);
this.mockRequest.addFile(part);
HttpServletRequest wrapped = new HttpServletRequestWrapper(this.mockRequest);
ServerHttpRequest request = new RequestPartServletServerHttpRequest(wrapped, "part");
byte[] result = FileCopyUtils.copyToByteArray(request.getBody());
assertThat(result).isEqualTo(bytes);
}
use of org.springframework.web.multipart.MultipartFile in project spring-framework by spring-projects.
the class RequestParamMethodArgumentResolverTests method supportsParameter.
@Test
public void supportsParameter() {
resolver = new RequestParamMethodArgumentResolver(null, true);
MethodParameter param = this.testMethod.annot(requestParam().notRequired("bar")).arg(String.class);
assertThat(resolver.supportsParameter(param)).isTrue();
param = this.testMethod.annotPresent(RequestParam.class).arg(String[].class);
assertThat(resolver.supportsParameter(param)).isTrue();
param = this.testMethod.annot(requestParam().name("name")).arg(Map.class);
assertThat(resolver.supportsParameter(param)).isTrue();
param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile.class);
assertThat(resolver.supportsParameter(param)).isTrue();
param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
assertThat(resolver.supportsParameter(param)).isTrue();
param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile[].class);
assertThat(resolver.supportsParameter(param)).isTrue();
param = this.testMethod.annotPresent(RequestParam.class).arg(Part.class);
assertThat(resolver.supportsParameter(param)).isTrue();
param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, Part.class);
assertThat(resolver.supportsParameter(param)).isTrue();
param = this.testMethod.annotPresent(RequestParam.class).arg(Part[].class);
assertThat(resolver.supportsParameter(param)).isTrue();
param = this.testMethod.annot(requestParam().noName()).arg(Map.class);
assertThat(resolver.supportsParameter(param)).isFalse();
param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
assertThat(resolver.supportsParameter(param)).isTrue();
param = this.testMethod.annotNotPresent().arg(MultipartFile.class);
assertThat(resolver.supportsParameter(param)).isTrue();
param = this.testMethod.annotNotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
assertThat(resolver.supportsParameter(param)).isTrue();
param = this.testMethod.annotNotPresent(RequestParam.class).arg(Part.class);
assertThat(resolver.supportsParameter(param)).isTrue();
param = this.testMethod.annot(requestPart()).arg(MultipartFile.class);
assertThat(resolver.supportsParameter(param)).isFalse();
param = this.testMethod.annot(requestParam()).arg(String.class);
assertThat(resolver.supportsParameter(param)).isTrue();
param = this.testMethod.annot(requestParam().notRequired()).arg(String.class);
assertThat(resolver.supportsParameter(param)).isTrue();
param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, Integer.class);
assertThat(resolver.supportsParameter(param)).isTrue();
param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, MultipartFile.class);
assertThat(resolver.supportsParameter(param)).isTrue();
resolver = new RequestParamMethodArgumentResolver(null, false);
param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
assertThat(resolver.supportsParameter(param)).isFalse();
param = this.testMethod.annotPresent(RequestPart.class).arg(MultipartFile.class);
assertThat(resolver.supportsParameter(param)).isFalse();
}
use of org.springframework.web.multipart.MultipartFile in project spring-framework by spring-projects.
the class RequestParamMethodArgumentResolverTests method resolveMultipartFileArrayMissing.
@Test
public void resolveMultipartFileArrayMissing() throws Exception {
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
request.addFile(new MockMultipartFile("other", "Hello World 3".getBytes()));
webRequest = new ServletWebRequest(request);
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile[].class);
assertThatExceptionOfType(MissingServletRequestPartException.class).isThrownBy(() -> resolver.resolveArgument(param, null, webRequest, null));
}
Aggregations