use of org.springframework.mock.web.test.MockMultipartFile in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method multipartFileAsStringArray.
@Test
public void multipartFileAsStringArray() throws Exception {
initServletWithControllers(MultipartController.class);
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
request.setRequestURI("/stringArray");
request.addFile(new MockMultipartFile("content", "Juergen".getBytes()));
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("Juergen", response.getContentAsString());
}
use of org.springframework.mock.web.test.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);
assertTrue(result instanceof Optional);
assertEquals("Invalid result", expected, ((Optional<?>) result).get());
}
use of org.springframework.mock.web.test.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();
assertNotNull(headers);
assertEquals(MediaType.APPLICATION_JSON, headers.getContentType());
}
use of org.springframework.mock.web.test.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());
assertArrayEquals(bytes, result);
}
use of org.springframework.mock.web.test.MockMultipartFile in project spring-framework by spring-projects.
the class WebRequestDataBinderTests method testMultipartFilesAsStringArray.
@Test
public void testMultipartFilesAsStringArray() {
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()));
request.addFile(new MockMultipartFile("stringArray", "Eva".getBytes()));
binder.bind(new ServletWebRequest(request));
assertEquals(2, target.getStringArray().length);
assertEquals("Juergen", target.getStringArray()[0]);
assertEquals("Eva", target.getStringArray()[1]);
}
Aggregations