Search in sources :

Example 21 with MockMultipartHttpServletRequest

use of org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest 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)

Example 22 with MockMultipartHttpServletRequest

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

the class RequestPartServletServerHttpRequestTests method getBodyViaRequestParameterWithRequestEncoding.

@Test
public void getBodyViaRequestParameterWithRequestEncoding() throws Exception {
    MockMultipartHttpServletRequest mockRequest = new MockMultipartHttpServletRequest() {

        @Override
        public HttpHeaders getMultipartHeaders(String paramOrFileName) {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            return headers;
        }
    };
    byte[] bytes = { (byte) 0xC4 };
    mockRequest.setParameter("part", new String(bytes, StandardCharsets.ISO_8859_1));
    mockRequest.setCharacterEncoding("iso-8859-1");
    ServerHttpRequest request = new RequestPartServletServerHttpRequest(mockRequest, "part");
    byte[] result = FileCopyUtils.copyToByteArray(request.getBody());
    assertThat(result).isEqualTo(bytes);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) MockMultipartHttpServletRequest(org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest) ServerHttpRequest(org.springframework.http.server.ServerHttpRequest) Test(org.junit.jupiter.api.Test)

Example 23 with MockMultipartHttpServletRequest

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

the class RequestParamMethodArgumentResolverTests method isMultipartRequestHttpPut.

// SPR-9079
@Test
public void isMultipartRequestHttpPut() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected = new MockMultipartFile("multipartFileList", "Hello World".getBytes());
    request.addFile(expected);
    request.setMethod("PUT");
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
    Object actual = resolver.resolveArgument(param, null, webRequest, null);
    boolean condition = actual instanceof List;
    assertThat(condition).isTrue();
    assertThat(((List<?>) actual).get(0)).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) RequestParam(org.springframework.web.bind.annotation.RequestParam) List(java.util.List) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Example 24 with MockMultipartHttpServletRequest

use of org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest 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);
    boolean condition = result instanceof Optional;
    assertThat(condition).isTrue();
    assertThat(((Optional<?>) result).get()).as("Invalid result").isEqualTo(expected);
}
Also used : MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) RequestParam(org.springframework.web.bind.annotation.RequestParam) Optional(java.util.Optional) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) MockMultipartHttpServletRequest(org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest) MethodParameter(org.springframework.core.MethodParameter) DefaultDataBinderFactory(org.springframework.web.bind.support.DefaultDataBinderFactory) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Example 25 with MockMultipartHttpServletRequest

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

the class RequestParamMethodArgumentResolverTests method resolveMultipartFileList.

@Test
public void resolveMultipartFileList() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected1 = new MockMultipartFile("mfilelist", "Hello World 1".getBytes());
    MultipartFile expected2 = new MockMultipartFile("mfilelist", "Hello World 2".getBytes());
    request.addFile(expected1);
    request.addFile(expected2);
    request.addFile(new MockMultipartFile("other", "Hello World 3".getBytes()));
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
    Object result = resolver.resolveArgument(param, null, webRequest, null);
    boolean condition = result instanceof List;
    assertThat(condition).isTrue();
    assertThat(result).isEqualTo(Arrays.asList(expected1, expected2));
}
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) RequestParam(org.springframework.web.bind.annotation.RequestParam) List(java.util.List) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Aggregations

MockMultipartHttpServletRequest (org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest)29 Test (org.junit.jupiter.api.Test)24 MockMultipartFile (org.springframework.web.testfixture.servlet.MockMultipartFile)24 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)21 MultipartFile (org.springframework.web.multipart.MultipartFile)13 MethodParameter (org.springframework.core.MethodParameter)12 RequestParam (org.springframework.web.bind.annotation.RequestParam)8 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)6 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 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)2 HttpHeaders (org.springframework.http.HttpHeaders)2 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)2 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)2 ServerHttpRequest (org.springframework.http.server.ServerHttpRequest)2 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)2