Search in sources :

Example 16 with MockMultipartHttpServletRequest

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

the class RequestPartMethodArgumentResolverTests method resolveOptionalMultipartFileArgument.

@Test
public void resolveOptionalMultipartFileArgument() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected = new MockMultipartFile("optionalMultipartFile", "Hello World".getBytes());
    request.addFile(expected);
    request.addFile(new MockMultipartFile("otherPart", "", "text/plain", "Hello World".getBytes()));
    webRequest = new ServletWebRequest(request);
    Object actualValue = resolver.resolveArgument(optionalMultipartFile, null, webRequest, null);
    boolean condition1 = actualValue instanceof Optional;
    assertThat(condition1).isTrue();
    assertThat(((Optional<?>) actualValue).get()).as("Invalid result").isEqualTo(expected);
    actualValue = resolver.resolveArgument(optionalMultipartFile, null, webRequest, null);
    assertThat(actualValue instanceof Optional).isTrue();
    assertThat(((Optional<?>) actualValue).get()).as("Invalid result").isEqualTo(expected);
}
Also used : MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) MockMultipartHttpServletRequest(org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest) Optional(java.util.Optional) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Example 17 with MockMultipartHttpServletRequest

use of org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest 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 18 with MockMultipartHttpServletRequest

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

the class RequestMappingHandlerAdapterIntegrationTests method handleRequestPart.

@Test
public void handleRequestPart() throws Exception {
    MockMultipartHttpServletRequest multipartRequest = new MockMultipartHttpServletRequest();
    multipartRequest.addFile(new MockMultipartFile("requestPart", "", "text/plain", "content".getBytes("UTF-8")));
    HandlerMethod handlerMethod = handlerMethod("handleRequestPart", String.class, Model.class);
    ModelAndView mav = handlerAdapter.handle(multipartRequest, response, handlerMethod);
    assertThat(mav).isNotNull();
    assertThat(mav.getModelMap().get("requestPart")).isEqualTo("content");
}
Also used : MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) MockMultipartHttpServletRequest(org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest) ModelAndView(org.springframework.web.servlet.ModelAndView) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.jupiter.api.Test)

Example 19 with MockMultipartHttpServletRequest

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

the class RequestPartMethodArgumentResolverTests method resolveOptionalMultipartFileArgumentNotPresent.

@Test
public void resolveOptionalMultipartFileArgumentNotPresent() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    webRequest = new ServletWebRequest(request);
    Object actualValue = resolver.resolveArgument(optionalMultipartFile, null, webRequest, null);
    assertThat(actualValue).as("Invalid argument value").isEqualTo(Optional.empty());
    actualValue = resolver.resolveArgument(optionalMultipartFile, null, webRequest, null);
    assertThat(actualValue).as("Invalid argument value").isEqualTo(Optional.empty());
}
Also used : MockMultipartHttpServletRequest(org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Example 20 with MockMultipartHttpServletRequest

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

the class RequestPartMethodArgumentResolverTests method resolveMultipartFileNotAnnotArgument.

@Test
public void resolveMultipartFileNotAnnotArgument() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected = new MockMultipartFile("multipartFileNotAnnot", "Hello World".getBytes());
    request.addFile(expected);
    request.addFile(new MockMultipartFile("otherPart", "", "text/plain", "Hello World".getBytes()));
    webRequest = new ServletWebRequest(request);
    Object result = resolver.resolveArgument(paramMultipartFileNotAnnot, null, webRequest, null);
    assertThat(result instanceof MultipartFile).isTrue();
    assertThat(result).as("Invalid result").isEqualTo(expected);
}
Also used : MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) MockMultipartHttpServletRequest(org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Aggregations

MockMultipartHttpServletRequest (org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest)29 Test (org.junit.jupiter.api.Test)24 MockMultipartFile (org.springframework.web.testfixture.servlet.MockMultipartFile)24 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)21 MultipartFile (org.springframework.web.multipart.MultipartFile)13 MethodParameter (org.springframework.core.MethodParameter)12 RequestParam (org.springframework.web.bind.annotation.RequestParam)8 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)6 PathPatternsParameterizedTest (org.springframework.web.servlet.handler.PathPatternsParameterizedTest)4 List (java.util.List)3 Optional (java.util.Optional)3 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)3 TestBean (org.springframework.beans.testfixture.beans.TestBean)3 StringMultipartFileEditor (org.springframework.web.multipart.support.StringMultipartFileEditor)3 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)2 HttpHeaders (org.springframework.http.HttpHeaders)2 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)2 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)2 ServerHttpRequest (org.springframework.http.server.ServerHttpRequest)2 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)2