Search in sources :

Example 1 with MockPart

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");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockPart(org.springframework.web.testfixture.servlet.MockPart) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 2 with MockPart

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]);
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) RequestPart(org.springframework.web.bind.annotation.RequestPart) MockPart(org.springframework.web.testfixture.servlet.MockPart) Part(jakarta.servlet.http.Part) MockPart(org.springframework.web.testfixture.servlet.MockPart) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Example 3 with MockPart

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);
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) RequestPart(org.springframework.web.bind.annotation.RequestPart) MockPart(org.springframework.web.testfixture.servlet.MockPart) Part(jakarta.servlet.http.Part) MockPart(org.springframework.web.testfixture.servlet.MockPart) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Example 4 with MockPart

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);
}
Also used : ServerHttpRequest(org.springframework.http.server.ServerHttpRequest) MockPart(org.springframework.web.testfixture.servlet.MockPart) Test(org.junit.jupiter.api.Test)

Example 5 with MockPart

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);
}
Also used : RequestParam(org.springframework.web.bind.annotation.RequestParam) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockPart(org.springframework.web.testfixture.servlet.MockPart) RequestPart(org.springframework.web.bind.annotation.RequestPart) MvcAnnotationPredicates.requestPart(org.springframework.web.testfixture.method.MvcAnnotationPredicates.requestPart) Part(jakarta.servlet.http.Part) MockPart(org.springframework.web.testfixture.servlet.MockPart) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Aggregations

MockPart (org.springframework.web.testfixture.servlet.MockPart)18 Test (org.junit.jupiter.api.Test)16 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)16 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)14 Part (jakarta.servlet.http.Part)9 MethodParameter (org.springframework.core.MethodParameter)9 RequestParam (org.springframework.web.bind.annotation.RequestParam)6 RequestPart (org.springframework.web.bind.annotation.RequestPart)6 MvcAnnotationPredicates.requestPart (org.springframework.web.testfixture.method.MvcAnnotationPredicates.requestPart)4 List (java.util.List)2 Optional (java.util.Optional)2 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)2 MultiValueMap (org.springframework.util.MultiValueMap)2 MissingServletRequestPartException (org.springframework.web.multipart.support.MissingServletRequestPartException)2 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)2 Map (java.util.Map)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)1 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)1 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)1