Search in sources :

Example 6 with MockMultipartFile

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

Example 7 with MockMultipartFile

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

the class WebRequestDataBinderTests method testMultipartFileAsStringArray.

@Test
public void testMultipartFileAsStringArray() {
    TestBean target = new TestBean();
    WebRequestDataBinder binder = new WebRequestDataBinder(target);
    binder.registerCustomEditor(String.class, new StringMultipartFileEditor());
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    request.addFile(new MockMultipartFile("stringArray", "Juergen".getBytes()));
    binder.bind(new ServletWebRequest(request));
    assertThat(target.getStringArray().length).isEqualTo(1);
    assertThat(target.getStringArray()[0]).isEqualTo("Juergen");
}
Also used : MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) StringMultipartFileEditor(org.springframework.web.multipart.support.StringMultipartFileEditor) MockMultipartHttpServletRequest(org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Example 8 with MockMultipartFile

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

the class RequestParamMapMethodArgumentResolverTests method resolveMapOfMultipartFile.

@Test
@SuppressWarnings("unchecked")
public void resolveMapOfMultipartFile() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected1 = new MockMultipartFile("mfile", "Hello World".getBytes());
    MultipartFile expected2 = new MockMultipartFile("other", "Hello World 3".getBytes());
    request.addFile(expected1);
    request.addFile(expected2);
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annot(requestParam().noName()).arg(Map.class, String.class, MultipartFile.class);
    Object result = resolver.resolveArgument(param, null, webRequest, null);
    boolean condition = result instanceof Map;
    assertThat(condition).isTrue();
    Map<String, MultipartFile> resultMap = (Map<String, MultipartFile>) result;
    assertThat(resultMap.size()).isEqualTo(2);
    assertThat(resultMap.get("mfile")).isEqualTo(expected1);
    assertThat(resultMap.get("other")).isEqualTo(expected2);
}
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) Map(java.util.Map) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.jupiter.api.Test)

Example 9 with MockMultipartFile

use of org.springframework.web.testfixture.servlet.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());
    assertThat(result).isEqualTo(bytes);
}
Also used : MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockMultipartHttpServletRequest(org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest) MultipartFile(org.springframework.web.multipart.MultipartFile) MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) HttpServletRequestWrapper(jakarta.servlet.http.HttpServletRequestWrapper) ServerHttpRequest(org.springframework.http.server.ServerHttpRequest) Test(org.junit.jupiter.api.Test)

Example 10 with MockMultipartFile

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

the class RequestPartServletServerHttpRequestTests method getMethod.

@Test
public void getMethod() throws Exception {
    this.mockRequest.addFile(new MockMultipartFile("part", "", "", "content".getBytes("UTF-8")));
    ServerHttpRequest request = new RequestPartServletServerHttpRequest(this.mockRequest, "part");
    this.mockRequest.setMethod("POST");
    assertThat(request.getMethod()).isEqualTo(HttpMethod.POST);
}
Also used : MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) ServerHttpRequest(org.springframework.http.server.ServerHttpRequest) 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