Search in sources :

Example 26 with BindingContext

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

the class MapMethodProcessorTests method setUp.

@BeforeEach
public void setUp() throws Exception {
    this.processor = new MapMethodProcessor();
    this.mavContainer = new BindingContext();
    this.webRequest = new ServletRequestContext(null, new MockHttpServletRequest(), null);
    webRequest.setBindingContext(mavContainer);
}
Also used : MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) BindingContext(cn.taketoday.web.BindingContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 27 with BindingContext

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

the class RequestParamMethodArgumentResolverTests method resolveMultipartFileArrayMissing.

@Test
public void resolveMultipartFileArrayMissing() throws Throwable {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    request.addFile(new MockMultipartFile("other", "Hello World 3".getBytes()));
    webRequest = new ServletRequestContext(null, request, null);
    webRequest.setBindingContext(new BindingContext());
    ResolvableMethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile[].class);
    assertThatExceptionOfType(MissingRequestPartException.class).isThrownBy(() -> resolver.resolveArgument(webRequest, param));
}
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 28 with BindingContext

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

the class RequestParamMethodArgumentResolverTests method resolveOptionalParamArray.

@Test
@SuppressWarnings("rawtypes")
public void resolveOptionalParamArray() 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", "456");
    result = resolver.resolveArgument(webRequest, param);
    assertThat(result.getClass()).isEqualTo(Optional.class);
    assertThat((Integer[]) ((Optional) result).get()).isEqualTo(new Integer[] { 123, 456 });
}
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 29 with BindingContext

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

the class RequestParamMethodArgumentResolverTests method isMultipartRequestHttpPut.

// SPR-9079
@Test
public void isMultipartRequestHttpPut() throws Throwable {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected = new MockMultipartFile("multipartFileList", "Hello World".getBytes());
    request.addFile(expected);
    request.setMethod("PUT");
    webRequest = new ServletRequestContext(null, request, null);
    webRequest.setBindingContext(new BindingContext());
    ResolvableMethodParameter param = testMethod.annotNotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
    Object actual = resolver.resolveArgument(webRequest, param);
    boolean condition = actual instanceof List;
    assertThat(condition).isTrue();
    assertThat(((List<?>) actual).get(0)).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) List(java.util.List) BindingContext(cn.taketoday.web.BindingContext) Test(org.junit.jupiter.api.Test)

Example 30 with BindingContext

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

the class RequestParamMethodArgumentResolverTests method resolvePartNotAnnot.

@Test
public void resolvePartNotAnnot() throws Throwable {
    MockPart expected = new MockPart("part", "Hello World".getBytes());
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContentType("multipart/form-data");
    request.addPart(expected);
    webRequest = new ServletRequestContext(null, request, null);
    webRequest.setBindingContext(new BindingContext());
    ResolvableMethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(Part.class);
    Object result = resolver.resolveArgument(webRequest, param);
    boolean condition = result instanceof Part;
    assertThat(condition).isTrue();
    assertThat(result).as("Invalid result").isEqualTo(expected);
}
Also used : RequestParam(cn.taketoday.web.annotation.RequestParam) MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) RequestPart(cn.taketoday.web.annotation.RequestPart) MvcAnnotationPredicates.requestPart(cn.taketoday.web.bind.resolver.MvcAnnotationPredicates.requestPart) Part(jakarta.servlet.http.Part) MockPart(cn.taketoday.web.testfixture.servlet.MockPart) 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)

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