use of org.springframework.web.testfixture.servlet.MockPart in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method dataClassBindingWithServletPart.
@PathPatternsParameterizedTest
void dataClassBindingWithServletPart(boolean usePathPatterns) throws Exception {
initDispatcherServlet(ServletPartDataClassController.class, usePathPatterns);
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/bind");
request.setContentType("multipart/form-data");
request.addPart(new MockPart("param1", "value1".getBytes(StandardCharsets.UTF_8)));
request.addParameter("param2", "true");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("value1-true-0");
}
use of org.springframework.web.testfixture.servlet.MockPart in project spring-framework by spring-projects.
the class RequestPartMethodArgumentResolverTests method resolvePartArrayArgument.
@Test
public void resolvePartArrayArgument() 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(paramPartArray, null, webRequest, null);
assertThat(result instanceof Part[]).isTrue();
Part[] parts = (Part[]) result;
assertThat(parts.length).isEqualTo(2);
assertThat(part1).isEqualTo(parts[0]);
assertThat(part2).isEqualTo(parts[1]);
}
use of org.springframework.web.testfixture.servlet.MockPart in project spring-framework by spring-projects.
the class RequestPartMethodArgumentResolverTests method resolvePartArgument.
@Test
public void resolvePartArgument() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("POST");
request.setContentType("multipart/form-data");
MockPart expected = new MockPart("part", "Hello World".getBytes());
request.addPart(expected);
request.addPart(new MockPart("otherPart", "Hello World".getBytes()));
webRequest = new ServletWebRequest(request);
Object result = resolver.resolveArgument(paramPart, null, webRequest, null);
assertThat(result instanceof Part).isTrue();
assertThat(result).as("Invalid result").isEqualTo(expected);
}
use of org.springframework.web.testfixture.servlet.MockPart in project spring-framework by spring-projects.
the class RequestPartServletServerHttpRequestTests method getBodyViaRequestPart.
// gh-25829
@Test
public void getBodyViaRequestPart() throws Exception {
byte[] bytes = "content".getBytes("UTF-8");
MockPart mockPart = new MockPart("part", bytes);
mockPart.getHeaders().setContentType(MediaType.APPLICATION_JSON);
this.mockRequest.addPart(mockPart);
ServerHttpRequest request = new RequestPartServletServerHttpRequest(this.mockRequest, "part");
byte[] result = FileCopyUtils.copyToByteArray(request.getBody());
assertThat(result).isEqualTo(bytes);
}
use of org.springframework.web.testfixture.servlet.MockPart in project spring-framework by spring-projects.
the class RequestParamMethodArgumentResolverTests method resolvePart.
@Test
public void resolvePart() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
MockPart expected = new MockPart("pfile", "Hello World".getBytes());
request.setMethod("POST");
request.setContentType("multipart/form-data");
request.addPart(expected);
webRequest = new ServletWebRequest(request);
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Part.class);
Object result = resolver.resolveArgument(param, null, webRequest, null);
boolean condition = result instanceof Part;
assertThat(condition).isTrue();
assertThat(result).as("Invalid result").isEqualTo(expected);
}
Aggregations