Search in sources :

Example 1 with MockMultipartHttpServletRequest

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

the class ServletAnnotationControllerHandlerMethodTests method multipartFilesAsStringArray.

@PathPatternsParameterizedTest
void multipartFilesAsStringArray(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(MultipartController.class, usePathPatterns);
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    request.setRequestURI("/stringArray");
    request.addFile(new MockMultipartFile("content", "Juergen".getBytes()));
    request.addFile(new MockMultipartFile("content", "Eva".getBytes()));
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("Juergen-Eva");
}
Also used : MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) MockMultipartHttpServletRequest(org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 2 with MockMultipartHttpServletRequest

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

the class ServletAnnotationControllerHandlerMethodTests method multipartFileAsStringArray.

@PathPatternsParameterizedTest
void multipartFileAsStringArray(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(MultipartController.class, usePathPatterns);
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    request.setRequestURI("/stringArray");
    request.addFile(new MockMultipartFile("content", "Juergen".getBytes()));
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("Juergen");
}
Also used : MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) MockMultipartHttpServletRequest(org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 3 with MockMultipartHttpServletRequest

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

the class ServletAnnotationControllerHandlerMethodTests method dataClassBindingWithMultipartFile.

@PathPatternsParameterizedTest
void dataClassBindingWithMultipartFile(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(MultipartFileDataClassController.class, usePathPatterns);
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    request.setRequestURI("/bind");
    request.addFile(new MockMultipartFile("param1", "value1".getBytes(StandardCharsets.UTF_8)));
    request.addParameter("param2", "true");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("value1-true-0");
}
Also used : MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) MockMultipartHttpServletRequest(org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 4 with MockMultipartHttpServletRequest

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

the class RequestPartMethodArgumentResolverTests method resolveOptionalMultipartFileList.

@Test
public void resolveOptionalMultipartFileList() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected = new MockMultipartFile("requestPart", "Hello World".getBytes());
    request.addFile(expected);
    request.addFile(new MockMultipartFile("otherPart", "", "text/plain", "Hello World".getBytes()));
    webRequest = new ServletWebRequest(request);
    Object actualValue = resolver.resolveArgument(optionalMultipartFileList, null, webRequest, null);
    boolean condition1 = actualValue instanceof Optional;
    assertThat(condition1).isTrue();
    assertThat(((Optional<?>) actualValue).get()).as("Invalid result").isEqualTo(Collections.singletonList(expected));
    actualValue = resolver.resolveArgument(optionalMultipartFileList, null, webRequest, null);
    assertThat(actualValue instanceof Optional).isTrue();
    assertThat(((Optional<?>) actualValue).get()).as("Invalid result").isEqualTo(Collections.singletonList(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 5 with MockMultipartHttpServletRequest

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

the class RequestMappingHandlerAdapterIntegrationTests method handleAndValidateRequestPart.

@Test
public void handleAndValidateRequestPart() throws Exception {
    MockMultipartHttpServletRequest multipartRequest = new MockMultipartHttpServletRequest();
    multipartRequest.addFile(new MockMultipartFile("requestPart", "", "text/plain", "content".getBytes("UTF-8")));
    HandlerMethod handlerMethod = handlerMethod("handleAndValidateRequestPart", String.class, Errors.class, Model.class);
    ModelAndView mav = handlerAdapter.handle(multipartRequest, response, handlerMethod);
    assertThat(mav).isNotNull();
    assertThat(mav.getModelMap().get("error count")).isEqualTo(1);
}
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)

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