Search in sources :

Example 21 with MockMultipartFile

use of org.springframework.mock.web.test.MockMultipartFile in project spring-framework by spring-projects.

the class RequestMappingHandlerAdapterIntegrationTests method handleRequestPart.

@Test
public void handleRequestPart() throws Exception {
    MockMultipartHttpServletRequest multipartRequest = new MockMultipartHttpServletRequest();
    multipartRequest.addFile(new MockMultipartFile("requestPart", "", "text/plain", "content".getBytes("UTF-8")));
    HandlerMethod handlerMethod = handlerMethod("handleRequestPart", String.class, Model.class);
    ModelAndView mav = handlerAdapter.handle(multipartRequest, response, handlerMethod);
    assertNotNull(mav);
    assertEquals("content", mav.getModelMap().get("requestPart"));
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) ModelAndView(org.springframework.web.servlet.ModelAndView) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.Test)

Example 22 with MockMultipartFile

use of org.springframework.mock.web.test.MockMultipartFile in project spring-framework by spring-projects.

the class RequestPartMethodArgumentResolverTests method setup.

@Before
@SuppressWarnings("unchecked")
public void setup() throws Exception {
    messageConverter = mock(HttpMessageConverter.class);
    given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
    resolver = new RequestPartMethodArgumentResolver(Collections.<HttpMessageConverter<?>>singletonList(messageConverter));
    reset(messageConverter);
    byte[] content = "doesn't matter as long as not empty".getBytes(StandardCharsets.UTF_8);
    multipartFile1 = new MockMultipartFile("requestPart", "", "text/plain", content);
    multipartFile2 = new MockMultipartFile("requestPart", "", "text/plain", content);
    multipartRequest = new MockMultipartHttpServletRequest();
    multipartRequest.addFile(multipartFile1);
    multipartRequest.addFile(multipartFile2);
    multipartRequest.addFile(new MockMultipartFile("otherPart", "", "text/plain", content));
    webRequest = new ServletWebRequest(multipartRequest, new MockHttpServletResponse());
    Method method = ReflectionUtils.findMethod(getClass(), "handle", (Class<?>[]) null);
    paramRequestPart = new SynthesizingMethodParameter(method, 0);
    paramRequestPart.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
    paramNamedRequestPart = new SynthesizingMethodParameter(method, 1);
    paramValidRequestPart = new SynthesizingMethodParameter(method, 2);
    paramMultipartFile = new SynthesizingMethodParameter(method, 3);
    paramMultipartFileList = new SynthesizingMethodParameter(method, 4);
    paramMultipartFileArray = new SynthesizingMethodParameter(method, 5);
    paramInt = new SynthesizingMethodParameter(method, 6);
    paramMultipartFileNotAnnot = new SynthesizingMethodParameter(method, 7);
    paramMultipartFileNotAnnot.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
    paramPart = new SynthesizingMethodParameter(method, 8);
    paramPart.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
    paramPartList = new SynthesizingMethodParameter(method, 9);
    paramPartArray = new SynthesizingMethodParameter(method, 10);
    paramRequestParamAnnot = new SynthesizingMethodParameter(method, 11);
    optionalMultipartFile = new SynthesizingMethodParameter(method, 12);
    optionalMultipartFile.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
    optionalMultipartFileList = new SynthesizingMethodParameter(method, 13);
    optionalMultipartFileList.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
    optionalPart = new SynthesizingMethodParameter(method, 14);
    optionalPart.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
    optionalPartList = new SynthesizingMethodParameter(method, 15);
    optionalPartList.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
    optionalRequestPart = new SynthesizingMethodParameter(method, 16);
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) LocalVariableTableParameterNameDiscoverer(org.springframework.core.LocalVariableTableParameterNameDiscoverer) SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) Method(java.lang.reflect.Method) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Before(org.junit.Before)

Example 23 with MockMultipartFile

use of org.springframework.mock.web.test.MockMultipartFile in project spring-framework by spring-projects.

the class RequestPartMethodArgumentResolverTests method resolveOptionalMultipartFileArgument.

@Test
public void resolveOptionalMultipartFileArgument() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected = new MockMultipartFile("optionalMultipartFile", "Hello World".getBytes());
    request.addFile(expected);
    request.addFile(new MockMultipartFile("otherPart", "", "text/plain", "Hello World".getBytes()));
    webRequest = new ServletWebRequest(request);
    Object actualValue = resolver.resolveArgument(optionalMultipartFile, null, webRequest, null);
    assertTrue(actualValue instanceof Optional);
    assertEquals("Invalid result", expected, ((Optional) actualValue).get());
    actualValue = resolver.resolveArgument(optionalMultipartFile, null, webRequest, null);
    assertTrue(actualValue instanceof Optional);
    assertEquals("Invalid result", expected, ((Optional) actualValue).get());
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) Optional(java.util.Optional) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 24 with MockMultipartFile

use of org.springframework.mock.web.test.MockMultipartFile in project spring-framework by spring-projects.

the class RequestPartMethodArgumentResolverTests method resolveMultipartFileNotAnnotArgument.

@Test
public void resolveMultipartFileNotAnnotArgument() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected = new MockMultipartFile("multipartFileNotAnnot", "Hello World".getBytes());
    request.addFile(expected);
    request.addFile(new MockMultipartFile("otherPart", "", "text/plain", "Hello World".getBytes()));
    webRequest = new ServletWebRequest(request);
    Object result = resolver.resolveArgument(paramMultipartFileNotAnnot, null, webRequest, null);
    assertTrue(result instanceof MultipartFile);
    assertEquals("Invalid result", expected, result);
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) 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