Search in sources :

Example 11 with MockMultipartFile

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());
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 12 with MockMultipartFile

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());
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) RequestParam(org.springframework.web.bind.annotation.RequestParam) Optional(java.util.Optional) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) MethodParameter(org.springframework.core.MethodParameter) DefaultDataBinderFactory(org.springframework.web.bind.support.DefaultDataBinderFactory) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.Test)

Example 13 with MockMultipartFile

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());
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) HttpHeaders(org.springframework.http.HttpHeaders) MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) ServerHttpRequest(org.springframework.http.server.ServerHttpRequest) Test(org.junit.Test)

Example 14 with MockMultipartFile

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);
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) ServerHttpRequest(org.springframework.http.server.ServerHttpRequest) Test(org.junit.Test)

Example 15 with MockMultipartFile

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]);
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) StringMultipartFileEditor(org.springframework.web.multipart.support.StringMultipartFileEditor) MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Aggregations

MockMultipartFile (org.springframework.mock.web.test.MockMultipartFile)24 Test (org.junit.Test)23 MockMultipartHttpServletRequest (org.springframework.mock.web.test.MockMultipartHttpServletRequest)20 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)14 MultipartFile (org.springframework.web.multipart.MultipartFile)13 MethodParameter (org.springframework.core.MethodParameter)7 RequestParam (org.springframework.web.bind.annotation.RequestParam)6 ServerHttpRequest (org.springframework.http.server.ServerHttpRequest)5 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)4 List (java.util.List)3 Optional (java.util.Optional)3 ITestBean (org.springframework.tests.sample.beans.ITestBean)3 TestBean (org.springframework.tests.sample.beans.TestBean)3 StringMultipartFileEditor (org.springframework.web.multipart.support.StringMultipartFileEditor)3 HandlerMethod (org.springframework.web.method.HandlerMethod)2 InvocableHandlerMethod (org.springframework.web.method.support.InvocableHandlerMethod)2 ModelAndView (org.springframework.web.servlet.ModelAndView)2 Method (java.lang.reflect.Method)1 URI (java.net.URI)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1