use of org.springframework.web.testfixture.servlet.MockMultipartFile in project spring-framework by spring-projects.
the class RequestPartServletServerHttpRequestTests method getBody.
@Test
public void getBody() throws Exception {
byte[] bytes = "content".getBytes("UTF-8");
MultipartFile part = new MockMultipartFile("part", "", "application/json", bytes);
this.mockRequest.addFile(part);
ServerHttpRequest request = new RequestPartServletServerHttpRequest(this.mockRequest, "part");
byte[] result = FileCopyUtils.copyToByteArray(request.getBody());
assertThat(result).isEqualTo(bytes);
}
use of org.springframework.web.testfixture.servlet.MockMultipartFile in project spring-framework by spring-projects.
the class RequestPartServletServerHttpRequestTests method getContentType.
@Test
public void getContentType() throws Exception {
MultipartFile part = new MockMultipartFile("part", "", "application/json", "content".getBytes("UTF-8"));
this.mockRequest.addFile(part);
ServerHttpRequest request = new RequestPartServletServerHttpRequest(this.mockRequest, "part");
HttpHeaders headers = request.getHeaders();
assertThat(headers).isNotNull();
assertThat(headers.getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
}
use of org.springframework.web.testfixture.servlet.MockMultipartFile in project spring-framework by spring-projects.
the class RequestParamMethodArgumentResolverTests method isMultipartRequestHttpPut.
// SPR-9079
@Test
public void isMultipartRequestHttpPut() throws Exception {
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
MultipartFile expected = new MockMultipartFile("multipartFileList", "Hello World".getBytes());
request.addFile(expected);
request.setMethod("PUT");
webRequest = new ServletWebRequest(request);
MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
Object actual = resolver.resolveArgument(param, null, webRequest, null);
boolean condition = actual instanceof List;
assertThat(condition).isTrue();
assertThat(((List<?>) actual).get(0)).isEqualTo(expected);
}
use of org.springframework.web.testfixture.servlet.MockMultipartFile in project spring-framework by spring-projects.
the class RequestParamMethodArgumentResolverTests method resolveOptionalMultipartFile.
@Test
public void resolveOptionalMultipartFile() throws Exception {
ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
initializer.setConversionService(new DefaultConversionService());
WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
MultipartFile expected = new MockMultipartFile("mfile", "Hello World".getBytes());
request.addFile(expected);
webRequest = new ServletWebRequest(request);
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, MultipartFile.class);
Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);
boolean condition = result instanceof Optional;
assertThat(condition).isTrue();
assertThat(((Optional<?>) result).get()).as("Invalid result").isEqualTo(expected);
}
use of org.springframework.web.testfixture.servlet.MockMultipartFile in project spring-framework by spring-projects.
the class RequestParamMethodArgumentResolverTests method resolveMultipartFileList.
@Test
public void resolveMultipartFileList() throws Exception {
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
MultipartFile expected1 = new MockMultipartFile("mfilelist", "Hello World 1".getBytes());
MultipartFile expected2 = new MockMultipartFile("mfilelist", "Hello World 2".getBytes());
request.addFile(expected1);
request.addFile(expected2);
request.addFile(new MockMultipartFile("other", "Hello World 3".getBytes()));
webRequest = new ServletWebRequest(request);
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
Object result = resolver.resolveArgument(param, null, webRequest, null);
boolean condition = result instanceof List;
assertThat(condition).isTrue();
assertThat(result).isEqualTo(Arrays.asList(expected1, expected2));
}
Aggregations