Search in sources :

Example 1 with MockMultipartFile

use of cn.taketoday.web.testfixture.servlet.MockMultipartFile in project today-framework by TAKETODAY.

the class RequestContextDataBinderTests method testMultipartFileAsStringArray.

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

Example 2 with MockMultipartFile

use of cn.taketoday.web.testfixture.servlet.MockMultipartFile in project today-framework by TAKETODAY.

the class RequestContextDataBinderTests method testMultipartFileAsString.

@Test
public void testMultipartFileAsString() {
    TestBean target = new TestBean();
    RequestContextDataBinder binder = new RequestContextDataBinder(target);
    binder.registerCustomEditor(String.class, new StringMultipartFileEditor());
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    request.addFile(new MockMultipartFile("name", "Juergen".getBytes()));
    binder.bind(new MockMultipartServletRequestContext(request, null));
    assertThat(target.getName()).isEqualTo("Juergen");
}
Also used : MockMultipartFile(cn.taketoday.web.testfixture.servlet.MockMultipartFile) MockMultipartServletRequestContext(cn.taketoday.web.servlet.MockMultipartServletRequestContext) StringMultipartFileEditor(cn.taketoday.web.multipart.support.StringMultipartFileEditor) MockMultipartHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockMultipartHttpServletRequest) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test)

Example 3 with MockMultipartFile

use of cn.taketoday.web.testfixture.servlet.MockMultipartFile in project today-framework by TAKETODAY.

the class RequestParamMethodArgumentResolverTests method resolveMultipartFile.

@Test
public void resolveMultipartFile() throws Throwable {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected = new MockMultipartFile("mfile", "Hello World".getBytes());
    request.addFile(expected);
    webRequest = new ServletRequestContext(null, request, null);
    webRequest.setBindingContext(new BindingContext());
    ResolvableMethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile.class);
    Object result = resolver.resolveArgument(webRequest, param);
    boolean condition = result instanceof MultipartFile;
    assertThat(condition).isTrue();
    assertThat(result).as("Invalid result").isEqualTo(expected);
}
Also used : MockMultipartFile(cn.taketoday.web.testfixture.servlet.MockMultipartFile) MockMultipartFile(cn.taketoday.web.testfixture.servlet.MockMultipartFile) MultipartFile(cn.taketoday.web.multipart.MultipartFile) MockMultipartHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockMultipartHttpServletRequest) RequestParam(cn.taketoday.web.annotation.RequestParam) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) BindingContext(cn.taketoday.web.BindingContext) Test(org.junit.jupiter.api.Test)

Example 4 with MockMultipartFile

use of cn.taketoday.web.testfixture.servlet.MockMultipartFile in project today-framework by TAKETODAY.

the class RequestParamMapMethodArgumentResolverTests method resolveMultiValueMapOfMultipartFile.

@Test
@SuppressWarnings("unchecked")
public void resolveMultiValueMapOfMultipartFile() throws Throwable {
    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 ServletRequestContext(null, request, null);
    ResolvableMethodParameter param = this.testMethod.annot(requestParam().noName()).arg(MultiValueMap.class, String.class, MultipartFile.class);
    Object result = resolver.resolveArgument(webRequest, param);
    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(cn.taketoday.web.testfixture.servlet.MockMultipartFile) MockMultipartFile(cn.taketoday.web.testfixture.servlet.MockMultipartFile) MultipartFile(cn.taketoday.web.multipart.MultipartFile) MockMultipartHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockMultipartHttpServletRequest) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) MultiValueMap(cn.taketoday.core.MultiValueMap) Test(org.junit.jupiter.api.Test)

Example 5 with MockMultipartFile

use of cn.taketoday.web.testfixture.servlet.MockMultipartFile in project today-framework by TAKETODAY.

the class RequestParamMethodArgumentResolverTests method resolveMultipartFileNotAnnot.

@Test
public void resolveMultipartFileNotAnnot() throws Throwable {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected = new MockMultipartFile("multipartFileNotAnnot", "Hello World".getBytes());
    request.addFile(expected);
    ServletRequestContext webRequest = new ServletRequestContext(null, request, null);
    webRequest.setBindingContext(new BindingContext());
    ResolvableMethodParameter param = this.testMethod.annotNotPresent().arg(MultipartFile.class);
    Object result = resolver.resolveArgument(webRequest, param);
    boolean condition = result instanceof MultipartFile;
    assertThat(condition).isTrue();
    assertThat(result).as("Invalid result").isEqualTo(expected);
}
Also used : MockMultipartFile(cn.taketoday.web.testfixture.servlet.MockMultipartFile) MockMultipartFile(cn.taketoday.web.testfixture.servlet.MockMultipartFile) MultipartFile(cn.taketoday.web.multipart.MultipartFile) MockMultipartHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockMultipartHttpServletRequest) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) BindingContext(cn.taketoday.web.BindingContext) Test(org.junit.jupiter.api.Test)

Aggregations

MockMultipartFile (cn.taketoday.web.testfixture.servlet.MockMultipartFile)14 MockMultipartHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockMultipartHttpServletRequest)14 Test (org.junit.jupiter.api.Test)14 ResolvableMethodParameter (cn.taketoday.web.handler.method.ResolvableMethodParameter)11 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)11 MultipartFile (cn.taketoday.web.multipart.MultipartFile)10 BindingContext (cn.taketoday.web.BindingContext)9 RequestParam (cn.taketoday.web.annotation.RequestParam)8 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)3 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)3 StringMultipartFileEditor (cn.taketoday.web.multipart.support.StringMultipartFileEditor)3 MockMultipartServletRequestContext (cn.taketoday.web.servlet.MockMultipartServletRequestContext)3 List (java.util.List)3 LinkedMultiValueMap (cn.taketoday.core.LinkedMultiValueMap)2 MultiValueMap (cn.taketoday.core.MultiValueMap)2 DefaultConversionService (cn.taketoday.core.conversion.support.DefaultConversionService)1 ConfigurableWebBindingInitializer (cn.taketoday.web.bind.support.ConfigurableWebBindingInitializer)1 MockHttpServletResponse (cn.taketoday.web.testfixture.servlet.MockHttpServletResponse)1 Map (java.util.Map)1 Optional (java.util.Optional)1