Search in sources :

Example 16 with MockMultipartFile

use of org.springframework.web.testfixture.servlet.MockMultipartFile in project spring-framework by spring-projects.

the class RequestPartMethodArgumentResolverTests method setup.

@BeforeEach
@SuppressWarnings("unchecked")
public void setup() throws Exception {
    messageConverter = mock(HttpMessageConverter.class);
    given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
    resolver = new RequestPartMethodArgumentResolver(Collections.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) {

        @Override
        public InputStream getInputStream() throws IOException {
            CloseTrackingInputStream in = new CloseTrackingInputStream(super.getInputStream());
            trackedStream = in;
            return in;
        }
    };
    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.web.testfixture.servlet.MockMultipartFile) LocalVariableTableParameterNameDiscoverer(org.springframework.core.LocalVariableTableParameterNameDiscoverer) SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) FilterInputStream(java.io.FilterInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Method(java.lang.reflect.Method) ResolvableMethod(org.springframework.web.testfixture.method.ResolvableMethod) MockMultipartHttpServletRequest(org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 17 with MockMultipartFile

use of org.springframework.web.testfixture.servlet.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);
    boolean condition1 = actualValue instanceof Optional;
    assertThat(condition1).isTrue();
    assertThat(((Optional<?>) actualValue).get()).as("Invalid result").isEqualTo(expected);
    actualValue = resolver.resolveArgument(optionalMultipartFile, null, webRequest, null);
    assertThat(actualValue instanceof Optional).isTrue();
    assertThat(((Optional<?>) actualValue).get()).as("Invalid result").isEqualTo(expected);
}
Also used : MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) MockMultipartHttpServletRequest(org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest) Optional(java.util.Optional) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Example 18 with MockMultipartFile

use of org.springframework.web.testfixture.servlet.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);
    assertThat(mav).isNotNull();
    assertThat(mav.getModelMap().get("requestPart")).isEqualTo("content");
}
Also used : MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) MockMultipartHttpServletRequest(org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest) ModelAndView(org.springframework.web.servlet.ModelAndView) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.jupiter.api.Test)

Example 19 with MockMultipartFile

use of org.springframework.web.testfixture.servlet.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);
    assertThat(result instanceof MultipartFile).isTrue();
    assertThat(result).as("Invalid result").isEqualTo(expected);
}
Also used : MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) MockMultipartHttpServletRequest(org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Example 20 with MockMultipartFile

use of org.springframework.web.testfixture.servlet.MockMultipartFile in project spring-framework by spring-projects.

the class RequestParamMapMethodArgumentResolverTests method resolveMultiValueMapOfMultipartFile.

@Test
@SuppressWarnings("unchecked")
public void resolveMultiValueMapOfMultipartFile() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected1 = new MockMultipartFile("mfilelist", "Hello World 1".getBytes());
    MultipartFile expected2 = new MockMultipartFile("mfilelist", "Hello World 2".getBytes());
    MultipartFile expected3 = new MockMultipartFile("other", "Hello World 3".getBytes());
    request.addFile(expected1);
    request.addFile(expected2);
    request.addFile(expected3);
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annot(requestParam().noName()).arg(MultiValueMap.class, String.class, MultipartFile.class);
    Object result = resolver.resolveArgument(param, null, webRequest, null);
    boolean condition = result instanceof MultiValueMap;
    assertThat(condition).isTrue();
    MultiValueMap<String, MultipartFile> resultMap = (MultiValueMap<String, MultipartFile>) result;
    assertThat(resultMap.size()).isEqualTo(2);
    assertThat(resultMap.get("mfilelist").size()).isEqualTo(2);
    assertThat(resultMap.get("mfilelist").get(0)).isEqualTo(expected1);
    assertThat(resultMap.get("mfilelist").get(1)).isEqualTo(expected2);
    assertThat(resultMap.get("other").size()).isEqualTo(1);
    assertThat(resultMap.get("other").get(0)).isEqualTo(expected3);
}
Also used : MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) MockMultipartHttpServletRequest(org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.jupiter.api.Test)

Aggregations

MockMultipartFile (org.springframework.web.testfixture.servlet.MockMultipartFile)29 MockMultipartHttpServletRequest (org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest)25 Test (org.junit.jupiter.api.Test)24 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)18 MultipartFile (org.springframework.web.multipart.MultipartFile)16 MethodParameter (org.springframework.core.MethodParameter)11 RequestParam (org.springframework.web.bind.annotation.RequestParam)8 ServerHttpRequest (org.springframework.http.server.ServerHttpRequest)5 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)5 PathPatternsParameterizedTest (org.springframework.web.servlet.handler.PathPatternsParameterizedTest)4 List (java.util.List)3 Optional (java.util.Optional)3 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)3 TestBean (org.springframework.beans.testfixture.beans.TestBean)3 StringMultipartFileEditor (org.springframework.web.multipart.support.StringMultipartFileEditor)3 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)2 MultiValueMap (org.springframework.util.MultiValueMap)2 HandlerMethod (org.springframework.web.method.HandlerMethod)2 InvocableHandlerMethod (org.springframework.web.method.support.InvocableHandlerMethod)2 MissingServletRequestPartException (org.springframework.web.multipart.support.MissingServletRequestPartException)2