Search in sources :

Example 11 with MockPart

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));
}
Also used : Optional(java.util.Optional) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockPart(org.springframework.web.testfixture.servlet.MockPart) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Example 12 with MockPart

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));
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockPart(org.springframework.web.testfixture.servlet.MockPart) List(java.util.List) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Example 13 with MockPart

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");
}
Also used : MockMultipartHttpServletRequest(org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) MockPart(org.springframework.web.testfixture.servlet.MockPart) MethodParameter(org.springframework.core.MethodParameter) SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) Test(org.junit.jupiter.api.Test)

Example 14 with MockPart

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);
}
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) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.jupiter.api.Test)

Example 15 with MockPart

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);
}
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) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MultiValueMap(org.springframework.util.MultiValueMap) Map(java.util.Map) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) 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