Search in sources :

Example 11 with MockMultipartFile

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

the class RequestPartServletServerHttpRequestTests method getURI.

@Test
public void getURI() throws Exception {
    this.mockRequest.addFile(new MockMultipartFile("part", "", "application/json", "content".getBytes("UTF-8")));
    ServerHttpRequest request = new RequestPartServletServerHttpRequest(this.mockRequest, "part");
    URI uri = new URI("https://example.com/path?query");
    this.mockRequest.setScheme(uri.getScheme());
    this.mockRequest.setServerName(uri.getHost());
    this.mockRequest.setServerPort(uri.getPort());
    this.mockRequest.setRequestURI(uri.getPath());
    this.mockRequest.setQueryString(uri.getQuery());
    assertThat(request.getURI()).isEqualTo(uri);
}
Also used : MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) ServerHttpRequest(org.springframework.http.server.ServerHttpRequest) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 12 with MockMultipartFile

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

the class RequestParamMethodArgumentResolverTests method resolveMultipartFileArrayMissing.

@Test
public void resolveMultipartFileArrayMissing() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    request.addFile(new MockMultipartFile("other", "Hello World 3".getBytes()));
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile[].class);
    assertThatExceptionOfType(MissingServletRequestPartException.class).isThrownBy(() -> resolver.resolveArgument(param, null, webRequest, null));
}
Also used : MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) MissingServletRequestPartException(org.springframework.web.multipart.support.MissingServletRequestPartException) MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) MockMultipartHttpServletRequest(org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest) RequestParam(org.springframework.web.bind.annotation.RequestParam) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Example 13 with MockMultipartFile

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

the class RequestParamMethodArgumentResolverTests method resolveMultipartFileListMissing.

@Test
public void resolveMultipartFileListMissing() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    request.addFile(new MockMultipartFile("other", "Hello World 3".getBytes()));
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
    assertThatExceptionOfType(MissingServletRequestPartException.class).isThrownBy(() -> resolver.resolveArgument(param, null, webRequest, null));
}
Also used : MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) MissingServletRequestPartException(org.springframework.web.multipart.support.MissingServletRequestPartException) MockMultipartHttpServletRequest(org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest) RequestParam(org.springframework.web.bind.annotation.RequestParam) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Example 14 with MockMultipartFile

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

the class RequestParamMethodArgumentResolverTests method resolveMultipartFile.

@Test
public void resolveMultipartFile() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected = new MockMultipartFile("mfile", "Hello World".getBytes());
    request.addFile(expected);
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile.class);
    Object result = resolver.resolveArgument(param, null, webRequest, null);
    boolean condition = result instanceof MultipartFile;
    assertThat(condition).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) RequestParam(org.springframework.web.bind.annotation.RequestParam) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Example 15 with MockMultipartFile

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

the class RequestParamMethodArgumentResolverTests method resolveMultipartFileNotAnnot.

@Test
public void resolveMultipartFileNotAnnot() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected = new MockMultipartFile("multipartFileNotAnnot", "Hello World".getBytes());
    request.addFile(expected);
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annotNotPresent().arg(MultipartFile.class);
    Object result = resolver.resolveArgument(param, null, webRequest, null);
    boolean condition = result instanceof MultipartFile;
    assertThat(condition).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) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Aggregations

MockMultipartFile (org.springframework.web.testfixture.servlet.MockMultipartFile)29 MockMultipartHttpServletRequest (org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest)25 Test (org.junit.jupiter.api.Test)24 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)18 MultipartFile (org.springframework.web.multipart.MultipartFile)16 MethodParameter (org.springframework.core.MethodParameter)11 RequestParam (org.springframework.web.bind.annotation.RequestParam)8 ServerHttpRequest (org.springframework.http.server.ServerHttpRequest)5 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)5 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 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)2 MultiValueMap (org.springframework.util.MultiValueMap)2 HandlerMethod (org.springframework.web.method.HandlerMethod)2 InvocableHandlerMethod (org.springframework.web.method.support.InvocableHandlerMethod)2 MissingServletRequestPartException (org.springframework.web.multipart.support.MissingServletRequestPartException)2