Search in sources :

Example 6 with MockMultipartFile

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

the class RequestPartServletServerHttpRequestTests method getBodyWithWrappedRequest.

// SPR-13317
@Test
public void getBodyWithWrappedRequest() throws Exception {
    byte[] bytes = "content".getBytes("UTF-8");
    MultipartFile part = new MockMultipartFile("part", "", "application/json", bytes);
    this.mockRequest.addFile(part);
    HttpServletRequest wrapped = new HttpServletRequestWrapper(this.mockRequest);
    ServerHttpRequest request = new RequestPartServletServerHttpRequest(wrapped, "part");
    byte[] result = FileCopyUtils.copyToByteArray(request.getBody());
    assertArrayEquals(bytes, result);
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) ServerHttpRequest(org.springframework.http.server.ServerHttpRequest) Test(org.junit.Test)

Example 7 with MockMultipartFile

use of org.springframework.mock.web.test.MockMultipartFile 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);
    assertNotNull(mav);
    assertEquals(1, mav.getModelMap().get("error count"));
}
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 8 with MockMultipartFile

use of org.springframework.mock.web.test.MockMultipartFile 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);
    assertTrue(actualValue instanceof Optional);
    assertEquals("Invalid result", Collections.singletonList(expected), ((Optional) actualValue).get());
    actualValue = resolver.resolveArgument(optionalMultipartFileList, null, webRequest, null);
    assertTrue(actualValue instanceof Optional);
    assertEquals("Invalid result", Collections.singletonList(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 9 with MockMultipartFile

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

the class ServletAnnotationControllerHandlerMethodTests method multipartFilesAsStringArray.

@Test
public void multipartFilesAsStringArray() throws Exception {
    initServletWithControllers(MultipartController.class);
    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);
    assertEquals("Juergen-Eva", 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 10 with MockMultipartFile

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

the class ServletAnnotationControllerHandlerMethodTests method multipartFileAsSingleString.

@Test
public void multipartFileAsSingleString() throws Exception {
    initServletWithControllers(MultipartController.class);
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    request.setRequestURI("/singleString");
    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)

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