Search in sources :

Example 6 with MockPart

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

the class RequestParamMethodArgumentResolverTests method resolvePartNotAnnot.

@Test
public void resolvePartNotAnnot() throws Exception {
    MockPart expected = new MockPart("part", "Hello World".getBytes());
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContentType("multipart/form-data");
    request.addPart(expected);
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annotNotPresent(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)

Example 7 with MockPart

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

the class RequestParamMethodArgumentResolverTests method resolvePartListMissing.

@Test
public void resolvePartListMissing() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContentType("multipart/form-data");
    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);
    assertThatExceptionOfType(MissingServletRequestPartException.class).isThrownBy(() -> resolver.resolveArgument(param, null, webRequest, null));
}
Also used : MissingServletRequestPartException(org.springframework.web.multipart.support.MissingServletRequestPartException) RequestParam(org.springframework.web.bind.annotation.RequestParam) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) 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 8 with MockPart

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

the class RequestParamMethodArgumentResolverTests method resolvePartArrayMissing.

@Test
public void resolvePartArrayMissing() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContentType("multipart/form-data");
    request.addPart(new MockPart("other", "Hello World 3".getBytes()));
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Part[].class);
    assertThatExceptionOfType(MissingServletRequestPartException.class).isThrownBy(() -> resolver.resolveArgument(param, null, webRequest, null));
}
Also used : MissingServletRequestPartException(org.springframework.web.multipart.support.MissingServletRequestPartException) 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 9 with MockPart

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

the class StandardMultipartHttpServletRequestTests method requestWithPart.

private StandardMultipartHttpServletRequest requestWithPart(String name, String disposition, String content) {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockPart part = new MockPart(name, null, content.getBytes(StandardCharsets.UTF_8));
    part.getHeaders().set("Content-Disposition", disposition);
    request.addPart(part);
    return new StandardMultipartHttpServletRequest(request);
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockPart(org.springframework.web.testfixture.servlet.MockPart)

Example 10 with MockPart

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

the class RequestPartMethodArgumentResolverTests method resolveOptionalPartArgument.

@Test
public void resolveOptionalPartArgument() throws Exception {
    MockPart expected = new MockPart("optionalPart", "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(optionalPart, null, webRequest, null);
    boolean condition1 = actualValue instanceof Optional;
    assertThat(condition1).isTrue();
    assertThat(((Optional<?>) actualValue).get()).as("Invalid result").isEqualTo(expected);
    actualValue = resolver.resolveArgument(optionalPart, null, webRequest, null);
    assertThat(actualValue instanceof Optional).isTrue();
    assertThat(((Optional<?>) actualValue).get()).as("Invalid result").isEqualTo(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)

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