Search in sources :

Example 16 with BindingContext

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

the class ErrorsMethodArgumentResolverTests method bindingResult.

@Test
public void bindingResult() throws Throwable {
    BindingContext mavContainer = new BindingContext();
    mavContainer.addAttribute("ignore1", "value1");
    mavContainer.addAttribute("ignore2", "value2");
    mavContainer.addAttribute("ignore3", "value3");
    mavContainer.addAttribute("ignore4", "value4");
    mavContainer.addAttribute("ignore5", "value5");
    mavContainer.addAllAttributes(bindingResult.getModel());
    webRequest.setBindingContext(mavContainer);
    Object actual = resolver.resolveArgument(webRequest, paramErrors);
    assertThat(bindingResult).isSameAs(actual);
}
Also used : BindingContext(cn.taketoday.web.BindingContext) Test(org.junit.jupiter.api.Test)

Example 17 with BindingContext

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

the class RequestParamMethodArgumentResolverTests method missingRequestParamEmptyValueConvertedToNull.

// SPR-10578
@Test
public void missingRequestParamEmptyValueConvertedToNull() throws Throwable {
    RequestContextDataBinder binder = new RequestContextDataBinder(null);
    binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    ServletRequestContext webRequest = new ServletRequestContext(null, request, new MockHttpServletResponse());
    BindingContext binderFactory = mock(BindingContext.class);
    given(binderFactory.createBinder(webRequest, null, "stringNotAnnot")).willReturn(binder);
    given(binderFactory.createBinder(webRequest, "stringNotAnnot")).willReturn(binder);
    webRequest.setBindingContext(binderFactory);
    request.addParameter("stringNotAnnot", "");
    ResolvableMethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
    // Object arg = resolver.resolveArgument(webRequest, param);
    // assertThat(arg).isNull();
    assertThatThrownBy(() -> resolver.resolveArgument(webRequest, param)).isInstanceOf(MissingRequestParameterException.class).hasMessage("Required request parameter 'stringNotAnnot' for method parameter type String is present but converted to null");
}
Also used : RequestParam(cn.taketoday.web.annotation.RequestParam) RequestContextDataBinder(cn.taketoday.web.bind.RequestContextDataBinder) StringTrimmerEditor(cn.taketoday.beans.propertyeditors.StringTrimmerEditor) MissingRequestParameterException(cn.taketoday.web.bind.MissingRequestParameterException) 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 18 with BindingContext

use of cn.taketoday.web.BindingContext 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 19 with BindingContext

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

the class RequestParamMethodArgumentResolverTests method resolveOptionalParamValue.

@Test
@SuppressWarnings("rawtypes")
public void resolveOptionalParamValue() 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());
    request.addParameter("name", "123");
    result = resolver.resolveArgument(webRequest, param);
    assertThat(result.getClass()).isEqualTo(Optional.class);
    assertThat(((Optional) result).get()).isEqualTo(123);
}
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)

Example 20 with BindingContext

use of cn.taketoday.web.BindingContext 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

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