use of cn.taketoday.web.testfixture.servlet.MockPart 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.testfixture.servlet.MockPart 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);
}
use of cn.taketoday.web.testfixture.servlet.MockPart in project today-framework by TAKETODAY.
the class RequestParamMethodArgumentResolverTests method resolvePartArrayMissing.
@Test
public void resolvePartArrayMissing() throws Throwable {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("POST");
request.setContentType("multipart/form-data");
request.addPart(new MockPart("other", "Hello World 3".getBytes()));
ServletRequestContext webRequest = new ServletRequestContext(null, request, new MockHttpServletResponse());
webRequest.setBindingContext(new BindingContext());
ResolvableMethodParameter param = testMethod.annotPresent(RequestParam.class).arg(Part[].class);
assertThatExceptionOfType(MissingRequestPartException.class).isThrownBy(() -> resolver.resolveArgument(webRequest, param));
}
use of cn.taketoday.web.testfixture.servlet.MockPart in project today-framework by TAKETODAY.
the class RequestParamMethodArgumentResolverTests method resolvePartArray.
@Test
public void resolvePartArray() throws Throwable {
MockHttpServletRequest request = new MockHttpServletRequest();
MockPart expected1 = new MockPart("pfilearray", "Hello World 1".getBytes());
MockPart expected2 = new MockPart("pfilearray", "Hello World 2".getBytes());
request.setMethod("POST");
request.setContentType("multipart/form-data");
request.addPart(expected1);
request.addPart(expected2);
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(Part[].class);
Object result = resolver.resolveArgument(webRequest, param);
boolean condition = result instanceof Part[];
assertThat(condition).isTrue();
Part[] parts = (Part[]) result;
assertThat(parts.length).isEqualTo(2);
assertThat(expected1).isEqualTo(parts[0]);
assertThat(expected2).isEqualTo(parts[1]);
}
use of cn.taketoday.web.testfixture.servlet.MockPart in project today-framework by TAKETODAY.
the class RequestParamMapMethodArgumentResolverTests method resolveMultiValueMapOfPart.
@Test
@SuppressWarnings("unchecked")
public void resolveMultiValueMapOfPart() throws Throwable {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setContentType("multipart/form-data");
Part expected1 = new MockPart("mfilelist", "Hello World 1".getBytes());
Part expected2 = new MockPart("mfilelist", "Hello World 2".getBytes());
Part expected3 = new MockPart("other", "Hello World 3".getBytes());
request.addPart(expected1);
request.addPart(expected2);
request.addPart(expected3);
request.setMethod("POST");
webRequest = new ServletRequestContext(null, request, null);
ResolvableMethodParameter param = this.testMethod.annot(requestParam().noName()).arg(MultiValueMap.class, String.class, Part.class);
Object result = resolver.resolveArgument(webRequest, param);
boolean condition = result instanceof MultiValueMap;
assertThat(condition).isTrue();
MultiValueMap<String, Part> resultMap = (MultiValueMap<String, Part>) 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);
}
Aggregations