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());
}
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));
}
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));
}
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();
}
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();
}
Aggregations