Search in sources :

Example 1 with MockMultipartFile

use of org.springframework.mock.web.test.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));
    assertEquals(1, target.getStringArray().length);
    assertEquals("Juergen", target.getStringArray()[0]);
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) StringMultipartFileEditor(org.springframework.web.multipart.support.StringMultipartFileEditor) MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 2 with MockMultipartFile

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

the class RequestPartServletServerHttpRequestTests method getURI.

@Test
public void getURI() throws Exception {
    this.mockRequest.addFile(new MockMultipartFile("part", "", "application/json", "content".getBytes("UTF-8")));
    ServerHttpRequest request = new RequestPartServletServerHttpRequest(this.mockRequest, "part");
    URI uri = new URI("http://example.com/path?query");
    this.mockRequest.setServerName(uri.getHost());
    this.mockRequest.setServerPort(uri.getPort());
    this.mockRequest.setRequestURI(uri.getPath());
    this.mockRequest.setQueryString(uri.getQuery());
    assertEquals(uri, request.getURI());
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) ServerHttpRequest(org.springframework.http.server.ServerHttpRequest) URI(java.net.URI) Test(org.junit.Test)

Example 3 with MockMultipartFile

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

the class RequestParamMethodArgumentResolverTests method resolveMultipartFileNotAnnot.

@Test
public void resolveMultipartFileNotAnnot() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected = new MockMultipartFile("multipartFileNotAnnot", "Hello World".getBytes());
    request.addFile(expected);
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annotNotPresent().arg(MultipartFile.class);
    Object result = resolver.resolveArgument(param, null, webRequest, null);
    assertTrue(result instanceof MultipartFile);
    assertEquals("Invalid result", expected, result);
}
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) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 4 with MockMultipartFile

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

the class RequestParamMethodArgumentResolverTests method resolveMultipartFile.

@Test
public void resolveMultipartFile() throws Exception {
    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(MultipartFile.class);
    Object result = resolver.resolveArgument(param, null, webRequest, null);
    assertTrue(result instanceof MultipartFile);
    assertEquals("Invalid result", expected, result);
}
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) RequestParam(org.springframework.web.bind.annotation.RequestParam) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 5 with MockMultipartFile

use of org.springframework.mock.web.test.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");
    assertEquals(HttpMethod.POST, request.getMethod());
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) ServerHttpRequest(org.springframework.http.server.ServerHttpRequest) 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