Search in sources :

Example 16 with MockPart

use of org.springframework.web.testfixture.servlet.MockPart in project spring-framework by spring-projects.

the class RequestParamMethodArgumentResolverTests method resolvePartList.

@Test
public void resolvePartList() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContentType("multipart/form-data");
    MockPart expected1 = new MockPart("pfilelist", "Hello World 1".getBytes());
    MockPart expected2 = new MockPart("pfilelist", "Hello World 2".getBytes());
    request.addPart(expected1);
    request.addPart(expected2);
    request.addPart(new MockPart("other", "Hello World 3".getBytes()));
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, Part.class);
    Object result = resolver.resolveArgument(param, null, webRequest, null);
    boolean condition = result instanceof List;
    assertThat(condition).isTrue();
    assertThat(result).isEqualTo(Arrays.asList(expected1, expected2));
}
Also used : RequestParam(org.springframework.web.bind.annotation.RequestParam) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockPart(org.springframework.web.testfixture.servlet.MockPart) List(java.util.List) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Example 17 with MockPart

use of org.springframework.web.testfixture.servlet.MockPart in project spring-framework by spring-projects.

the class RequestParamMethodArgumentResolverTests method resolvePartArray.

@Test
public void resolvePartArray() throws Exception {
    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 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();
    Part[] parts = (Part[]) result;
    assertThat(parts.length).isEqualTo(2);
    assertThat(expected1).isEqualTo(parts[0]);
    assertThat(expected2).isEqualTo(parts[1]);
}
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)

Example 18 with MockPart

use of org.springframework.web.testfixture.servlet.MockPart in project spring-framework by spring-projects.

the class DefaultServerRequestTests method multipartData.

@Test
void multipartData() throws Exception {
    MockPart formPart = new MockPart("form", "foo".getBytes(UTF_8));
    MockPart filePart = new MockPart("file", "foo.txt", "foo".getBytes(UTF_8));
    MockHttpServletRequest servletRequest = PathPatternsTestUtils.initRequest("POST", "/", true);
    servletRequest.addPart(formPart);
    servletRequest.addPart(filePart);
    DefaultServerRequest request = new DefaultServerRequest(servletRequest, this.messageConverters);
    MultiValueMap<String, Part> result = request.multipartData();
    assertThat(result).hasSize(2);
    assertThat(result.get("form")).containsExactly(formPart);
    assertThat(result.get("file")).containsExactly(filePart);
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockPart(org.springframework.web.testfixture.servlet.MockPart) Part(jakarta.servlet.http.Part) MockPart(org.springframework.web.testfixture.servlet.MockPart) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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