use of org.springframework.web.testfixture.servlet.MockPart in project spring-framework by spring-projects.
the class RequestPartMethodArgumentResolverTests method resolveOptionalPartList.
@Test
public void resolveOptionalPartList() throws Exception {
MockPart expected = new MockPart("requestPart", "Hello World".getBytes());
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("POST");
request.setContentType("multipart/form-data");
request.addPart(expected);
request.addPart(new MockPart("otherPart", "Hello World".getBytes()));
webRequest = new ServletWebRequest(request);
Object actualValue = resolver.resolveArgument(optionalPartList, null, webRequest, null);
boolean condition1 = actualValue instanceof Optional;
assertThat(condition1).isTrue();
assertThat(((Optional<?>) actualValue).get()).as("Invalid result").isEqualTo(Collections.singletonList(expected));
actualValue = resolver.resolveArgument(optionalPartList, null, webRequest, null);
assertThat(actualValue instanceof Optional).isTrue();
assertThat(((Optional<?>) actualValue).get()).as("Invalid result").isEqualTo(Collections.singletonList(expected));
}
use of org.springframework.web.testfixture.servlet.MockPart in project spring-framework by spring-projects.
the class RequestPartMethodArgumentResolverTests method resolvePartListArgument.
@Test
public void resolvePartListArgument() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("POST");
request.setContentType("multipart/form-data");
MockPart part1 = new MockPart("requestPart", "Hello World 1".getBytes());
MockPart part2 = new MockPart("requestPart", "Hello World 2".getBytes());
request.addPart(part1);
request.addPart(part2);
request.addPart(new MockPart("otherPart", "Hello World".getBytes()));
webRequest = new ServletWebRequest(request);
Object result = resolver.resolveArgument(paramPartList, null, webRequest, null);
assertThat(result instanceof List).isTrue();
assertThat(result).isEqualTo(Arrays.asList(part1, part2));
}
use of org.springframework.web.testfixture.servlet.MockPart in project spring-framework by spring-projects.
the class RequestPartMethodArgumentResolverTests method resolveRequestPartWithoutContentType.
// gh-26501
@Test
public void resolveRequestPartWithoutContentType() throws Exception {
MockMultipartHttpServletRequest servletRequest = new MockMultipartHttpServletRequest();
servletRequest.addPart(new MockPart("requestPartString", "part value".getBytes(StandardCharsets.UTF_8)));
ServletWebRequest webRequest = new ServletWebRequest(servletRequest, new MockHttpServletResponse());
List<HttpMessageConverter<?>> converters = Collections.singletonList(new StringHttpMessageConverter());
RequestPartMethodArgumentResolver resolver = new RequestPartMethodArgumentResolver(converters);
MethodParameter parameter = ResolvableMethod.on(getClass()).named("handle").build().arg(String.class);
Object actualValue = resolver.resolveArgument(parameter, new ModelAndViewContainer(), webRequest, new ValidatingBinderFactory());
assertThat(actualValue).isEqualTo("part value");
}
use of org.springframework.web.testfixture.servlet.MockPart in project spring-framework by spring-projects.
the class RequestParamMapMethodArgumentResolverTests method resolveMultiValueMapOfPart.
@Test
@SuppressWarnings("unchecked")
public void resolveMultiValueMapOfPart() throws Exception {
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);
webRequest = new ServletWebRequest(request);
MethodParameter param = this.testMethod.annot(requestParam().noName()).arg(MultiValueMap.class, String.class, Part.class);
Object result = resolver.resolveArgument(param, null, webRequest, null);
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);
}
use of org.springframework.web.testfixture.servlet.MockPart in project spring-framework by spring-projects.
the class RequestParamMapMethodArgumentResolverTests method resolveMapOfPart.
@Test
@SuppressWarnings("unchecked")
public void resolveMapOfPart() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setContentType("multipart/form-data");
Part expected1 = new MockPart("mfile", "Hello World".getBytes());
Part expected2 = new MockPart("other", "Hello World 3".getBytes());
request.addPart(expected1);
request.addPart(expected2);
webRequest = new ServletWebRequest(request);
MethodParameter param = this.testMethod.annot(requestParam().noName()).arg(Map.class, String.class, Part.class);
Object result = resolver.resolveArgument(param, null, webRequest, null);
boolean condition = result instanceof Map;
assertThat(condition).isTrue();
Map<String, Part> resultMap = (Map<String, Part>) result;
assertThat(resultMap.size()).isEqualTo(2);
assertThat(resultMap.get("mfile")).isEqualTo(expected1);
assertThat(resultMap.get("other")).isEqualTo(expected2);
}
Aggregations