Search in sources :

Example 21 with BindingContext

use of cn.taketoday.web.BindingContext in project today-framework by TAKETODAY.

the class RequestParamMethodArgumentResolverTests method optionalMultipartFileWithoutMultipartRequest.

@Test
public void optionalMultipartFileWithoutMultipartRequest() throws Throwable {
    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(new DefaultConversionService());
    ServletRequestContext webRequest = new ServletRequestContext(null, request, new MockHttpServletResponse());
    BindingContext binderFactory = new BindingContext(initializer);
    webRequest.setBindingContext(binderFactory);
    ResolvableMethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, MultipartFile.class);
    Object actual = resolver.resolveArgument(webRequest, param);
    assertThat(actual).isEqualTo(Optional.empty());
}
Also used : ConfigurableWebBindingInitializer(cn.taketoday.web.bind.support.ConfigurableWebBindingInitializer) RequestParam(cn.taketoday.web.annotation.RequestParam) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) DefaultConversionService(cn.taketoday.core.conversion.support.DefaultConversionService) BindingContext(cn.taketoday.web.BindingContext) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 22 with BindingContext

use of cn.taketoday.web.BindingContext in project today-framework by TAKETODAY.

the class RequestParamMethodArgumentResolverTests method resolvePartListMissing.

@Test
public void resolvePartListMissing() throws Throwable {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContentType("multipart/form-data");
    request.addPart(new MockPart("other", "Hello World 3".getBytes()));
    webRequest = new ServletRequestContext(null, request, null);
    webRequest.setBindingContext(new BindingContext());
    ResolvableMethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, Part.class);
    assertThatExceptionOfType(MissingRequestPartException.class).isThrownBy(() -> resolver.resolveArgument(webRequest, param));
}
Also used : RequestParam(cn.taketoday.web.annotation.RequestParam) MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) MockPart(cn.taketoday.web.testfixture.servlet.MockPart) BindingContext(cn.taketoday.web.BindingContext) Test(org.junit.jupiter.api.Test)

Example 23 with BindingContext

use of cn.taketoday.web.BindingContext in project today-framework by TAKETODAY.

the class RequestParamMethodArgumentResolverTests method resolveMultipartFileListNotannot.

@Test
public void resolveMultipartFileListNotannot() throws Throwable {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected1 = new MockMultipartFile("multipartFileList", "Hello World 1".getBytes());
    MultipartFile expected2 = new MockMultipartFile("multipartFileList", "Hello World 2".getBytes());
    request.addFile(expected1);
    request.addFile(expected2);
    webRequest = new ServletRequestContext(null, request, null);
    webRequest.setBindingContext(new BindingContext());
    ResolvableMethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
    Object result = resolver.resolveArgument(webRequest, param);
    boolean condition = result instanceof List;
    assertThat(condition).isTrue();
    assertThat(result).isEqualTo(Arrays.asList(expected1, expected2));
}
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) List(java.util.List) BindingContext(cn.taketoday.web.BindingContext) Test(org.junit.jupiter.api.Test)

Example 24 with BindingContext

use of cn.taketoday.web.BindingContext in project today-framework by TAKETODAY.

the class RequestParamMethodArgumentResolverTests method missingRequestParamEmptyValueNotRequired.

@Test
public void missingRequestParamEmptyValueNotRequired() throws Throwable {
    RequestContextDataBinder binder = new RequestContextDataBinder(null);
    binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    request.addParameter("name", "");
    ServletRequestContext webRequest = new ServletRequestContext(null, request, new MockHttpServletResponse());
    BindingContext binderFactory = mock(BindingContext.class);
    given(binderFactory.createBinder(webRequest, null, "name")).willReturn(binder);
    given(binderFactory.createBinder(webRequest, "name")).willReturn(binder);
    webRequest.setBindingContext(binderFactory);
    ResolvableMethodParameter param = this.testMethod.annot(requestParam().notRequired()).arg(String.class);
    Object arg = resolver.resolveArgument(webRequest, param);
    assertThat(arg).isNull();
}
Also used : RequestContextDataBinder(cn.taketoday.web.bind.RequestContextDataBinder) StringTrimmerEditor(cn.taketoday.beans.propertyeditors.StringTrimmerEditor) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) BindingContext(cn.taketoday.web.BindingContext) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 25 with BindingContext

use of cn.taketoday.web.BindingContext in project today-framework by TAKETODAY.

the class RequestParamMethodArgumentResolverTests method missingOptionalParamValue.

@Test
@SuppressWarnings("rawtypes")
public void missingOptionalParamValue() throws Throwable {
    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(new DefaultConversionService());
    ServletRequestContext webRequest = new ServletRequestContext(null, request, new MockHttpServletResponse());
    BindingContext binderFactory = new BindingContext(initializer);
    webRequest.setBindingContext(binderFactory);
    ResolvableMethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, Integer.class);
    Object result = resolver.resolveArgument(webRequest, param);
    assertThat(result).isEqualTo(Optional.empty());
    result = resolver.resolveArgument(webRequest, param);
    assertThat(result.getClass()).isEqualTo(Optional.class);
    assertThat(((Optional) result).isPresent()).isFalse();
}
Also used : ConfigurableWebBindingInitializer(cn.taketoday.web.bind.support.ConfigurableWebBindingInitializer) RequestParam(cn.taketoday.web.annotation.RequestParam) Optional(java.util.Optional) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) DefaultConversionService(cn.taketoday.core.conversion.support.DefaultConversionService) BindingContext(cn.taketoday.web.BindingContext) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Aggregations

BindingContext (cn.taketoday.web.BindingContext)75 Test (org.junit.jupiter.api.Test)53 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)31 ResolvableMethodParameter (cn.taketoday.web.handler.method.ResolvableMethodParameter)30 RequestParam (cn.taketoday.web.annotation.RequestParam)23 ConfigurableWebBindingInitializer (cn.taketoday.web.bind.support.ConfigurableWebBindingInitializer)17 MockHttpServletResponse (cn.taketoday.web.testfixture.servlet.MockHttpServletResponse)13 DefaultConversionService (cn.taketoday.core.conversion.support.DefaultConversionService)11 MockHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockHttpServletRequest)11 RequestContextDataBinder (cn.taketoday.web.bind.RequestContextDataBinder)9 MockMultipartFile (cn.taketoday.web.testfixture.servlet.MockMultipartFile)9 MockMultipartHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockMultipartHttpServletRequest)9 MultipartFile (cn.taketoday.web.multipart.MultipartFile)8 DefaultFormattingConversionService (cn.taketoday.format.support.DefaultFormattingConversionService)7 Nullable (cn.taketoday.lang.Nullable)7 Optional (java.util.Optional)7 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)6 MethodParameter (cn.taketoday.core.MethodParameter)6 MockPart (cn.taketoday.web.testfixture.servlet.MockPart)6 RequestPart (cn.taketoday.web.annotation.RequestPart)5