use of org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest in project spring-framework by spring-projects.
the class RequestMappingHandlerAdapterIntegrationTests method handleAndValidateRequestPart.
@Test
public void handleAndValidateRequestPart() throws Exception {
MockMultipartHttpServletRequest multipartRequest = new MockMultipartHttpServletRequest();
multipartRequest.addFile(new MockMultipartFile("requestPart", "", "text/plain", "content".getBytes("UTF-8")));
HandlerMethod handlerMethod = handlerMethod("handleAndValidateRequestPart", String.class, Errors.class, Model.class);
ModelAndView mav = handlerAdapter.handle(multipartRequest, response, handlerMethod);
assertThat(mav).isNotNull();
assertThat(mav.getModelMap().get("error count")).isEqualTo(1);
}
use of org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest in project spring-framework by spring-projects.
the class WebRequestDataBinderTests method testMultipartFileAsStringArray.
@Test
public void testMultipartFileAsStringArray() {
TestBean target = new TestBean();
WebRequestDataBinder binder = new WebRequestDataBinder(target);
binder.registerCustomEditor(String.class, new StringMultipartFileEditor());
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
request.addFile(new MockMultipartFile("stringArray", "Juergen".getBytes()));
binder.bind(new ServletWebRequest(request));
assertThat(target.getStringArray().length).isEqualTo(1);
assertThat(target.getStringArray()[0]).isEqualTo("Juergen");
}
use of org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest 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.testfixture.servlet.MockMultipartHttpServletRequest in project spring-framework by spring-projects.
the class RequestPartServletServerHttpRequestTests method getBodyViaRequestParameter.
// SPR-13096
@Test
public void getBodyViaRequestParameter() throws Exception {
MockMultipartHttpServletRequest mockRequest = new MockMultipartHttpServletRequest() {
@Override
public HttpHeaders getMultipartHeaders(String paramOrFileName) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(new MediaType("application", "octet-stream", StandardCharsets.ISO_8859_1));
return headers;
}
};
byte[] bytes = { (byte) 0xC4 };
mockRequest.setParameter("part", new String(bytes, StandardCharsets.ISO_8859_1));
ServerHttpRequest request = new RequestPartServletServerHttpRequest(mockRequest, "part");
byte[] result = FileCopyUtils.copyToByteArray(request.getBody());
assertThat(result).isEqualTo(bytes);
}
use of org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest 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