Search in sources :

Example 81 with ServletWebRequest

use of org.springframework.web.context.request.ServletWebRequest in project spring-framework by spring-projects.

the class RequestPartMethodArgumentResolverTests method resolveOptionalPartArgumentWithoutMultipartRequest.

@Test
public void resolveOptionalPartArgumentWithoutMultipartRequest() throws Exception {
    webRequest = new ServletWebRequest(new MockHttpServletRequest());
    Object actualValue = resolver.resolveArgument(optionalPart, null, webRequest, null);
    assertEquals("Invalid argument value", Optional.empty(), actualValue);
    actualValue = resolver.resolveArgument(optionalPart, null, webRequest, null);
    assertEquals("Invalid argument value", Optional.empty(), actualValue);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 82 with ServletWebRequest

use of org.springframework.web.context.request.ServletWebRequest in project spring-framework by spring-projects.

the class RequestPartMethodArgumentResolverTests method isMultipartRequest.

@Test(expected = MultipartException.class)
public void isMultipartRequest() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    resolver.resolveArgument(paramMultipartFile, new ModelAndViewContainer(), new ServletWebRequest(request), null);
}
Also used : ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 83 with ServletWebRequest

use of org.springframework.web.context.request.ServletWebRequest 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);
    assertTrue(result instanceof List);
    assertEquals(Arrays.asList(part1, part2), result);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockPart(org.springframework.mock.web.test.MockPart) List(java.util.List) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 84 with ServletWebRequest

use of org.springframework.web.context.request.ServletWebRequest 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);
    assertEquals("Invalid argument value", Optional.empty(), actualValue);
    actualValue = resolver.resolveArgument(optionalMultipartFile, null, webRequest, null);
    assertEquals("Invalid argument value", Optional.empty(), actualValue);
}
Also used : MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 85 with ServletWebRequest

use of org.springframework.web.context.request.ServletWebRequest 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);
    assertTrue(actualValue instanceof Optional);
    assertEquals("Invalid result", Collections.singletonList(expected), ((Optional) actualValue).get());
    actualValue = resolver.resolveArgument(optionalPartList, null, webRequest, null);
    assertTrue(actualValue instanceof Optional);
    assertEquals("Invalid result", Collections.singletonList(expected), ((Optional) actualValue).get());
}
Also used : Optional(java.util.Optional) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockPart(org.springframework.mock.web.test.MockPart) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Aggregations

ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)114 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)76 Test (org.junit.Test)49 Before (org.junit.Before)45 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)29 MethodParameter (org.springframework.core.MethodParameter)26 ModelAndViewContainer (org.springframework.web.method.support.ModelAndViewContainer)25 Method (java.lang.reflect.Method)20 MockMultipartHttpServletRequest (org.springframework.mock.web.test.MockMultipartHttpServletRequest)16 MockMultipartFile (org.springframework.mock.web.test.MockMultipartFile)14 ITestBean (org.springframework.tests.sample.beans.ITestBean)13 TestBean (org.springframework.tests.sample.beans.TestBean)13 RequestParam (org.springframework.web.bind.annotation.RequestParam)10 MultipartFile (org.springframework.web.multipart.MultipartFile)10 MockPart (org.springframework.mock.web.test.MockPart)9 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)8 List (java.util.List)6 Optional (java.util.Optional)6 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)5 IOException (java.io.IOException)4