use of org.springframework.web.testfixture.servlet.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);
assertThat(mav).isNotNull();
assertThat(mav.getModelMap().get("error count")).isEqualTo(1);
}
use of org.springframework.web.testfixture.servlet.MockMultipartFile 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.MockMultipartFile 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.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());
assertThat(result).isEqualTo(bytes);
}
use of org.springframework.web.testfixture.servlet.MockMultipartFile in project spring-framework by spring-projects.
the class RequestPartServletServerHttpRequestTests method getMethod.
@Test
public void getMethod() throws Exception {
this.mockRequest.addFile(new MockMultipartFile("part", "", "", "content".getBytes("UTF-8")));
ServerHttpRequest request = new RequestPartServletServerHttpRequest(this.mockRequest, "part");
this.mockRequest.setMethod("POST");
assertThat(request.getMethod()).isEqualTo(HttpMethod.POST);
}
Aggregations