use of org.springframework.mock.web.test.MockMultipartFile 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());
assertArrayEquals(bytes, result);
}
use of org.springframework.mock.web.test.MockMultipartFile 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);
assertNotNull(mav);
assertEquals(1, mav.getModelMap().get("error count"));
}
use of org.springframework.mock.web.test.MockMultipartFile in project spring-framework by spring-projects.
the class RequestPartMethodArgumentResolverTests method resolveOptionalMultipartFileList.
@Test
public void resolveOptionalMultipartFileList() throws Exception {
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
MultipartFile expected = new MockMultipartFile("requestPart", "Hello World".getBytes());
request.addFile(expected);
request.addFile(new MockMultipartFile("otherPart", "", "text/plain", "Hello World".getBytes()));
webRequest = new ServletWebRequest(request);
Object actualValue = resolver.resolveArgument(optionalMultipartFileList, null, webRequest, null);
assertTrue(actualValue instanceof Optional);
assertEquals("Invalid result", Collections.singletonList(expected), ((Optional) actualValue).get());
actualValue = resolver.resolveArgument(optionalMultipartFileList, null, webRequest, null);
assertTrue(actualValue instanceof Optional);
assertEquals("Invalid result", Collections.singletonList(expected), ((Optional) actualValue).get());
}
use of org.springframework.mock.web.test.MockMultipartFile in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method multipartFilesAsStringArray.
@Test
public void multipartFilesAsStringArray() throws Exception {
initServletWithControllers(MultipartController.class);
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
request.setRequestURI("/stringArray");
request.addFile(new MockMultipartFile("content", "Juergen".getBytes()));
request.addFile(new MockMultipartFile("content", "Eva".getBytes()));
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("Juergen-Eva", response.getContentAsString());
}
use of org.springframework.mock.web.test.MockMultipartFile in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method multipartFileAsSingleString.
@Test
public void multipartFileAsSingleString() throws Exception {
initServletWithControllers(MultipartController.class);
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
request.setRequestURI("/singleString");
request.addFile(new MockMultipartFile("content", "Juergen".getBytes()));
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("Juergen", response.getContentAsString());
}
Aggregations