Search in sources :

Example 6 with MockPart

use of org.springframework.mock.web.test.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);
    assertTrue(actualValue instanceof Optional);
    assertEquals("Invalid result", expected, ((Optional) actualValue).get());
    actualValue = resolver.resolveArgument(optionalPart, null, webRequest, null);
    assertTrue(actualValue instanceof Optional);
    assertEquals("Invalid result", 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)

Example 7 with MockPart

use of org.springframework.mock.web.test.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);
    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 8 with MockPart

use of org.springframework.mock.web.test.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);
    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)

Example 9 with MockPart

use of org.springframework.mock.web.test.MockPart in project spring-framework by spring-projects.

the class RequestParamMethodArgumentResolverTests method resolvePartList.

@Test
public void resolvePartList() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContentType("multipart/form-data");
    MockPart expected1 = new MockPart("pfilelist", "Hello World 1".getBytes());
    MockPart expected2 = new MockPart("pfilelist", "Hello World 2".getBytes());
    request.addPart(expected1);
    request.addPart(expected2);
    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);
    Object result = resolver.resolveArgument(param, null, webRequest, null);
    assertTrue(result instanceof List);
    assertEquals(Arrays.asList(expected1, expected2), result);
}
Also used : RequestParam(org.springframework.web.bind.annotation.RequestParam) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockPart(org.springframework.mock.web.test.MockPart) List(java.util.List) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)9 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)9 MockPart (org.springframework.mock.web.test.MockPart)9 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)9 Part (javax.servlet.http.Part)5 RequestPart (org.springframework.web.bind.annotation.RequestPart)5 MethodParameter (org.springframework.core.MethodParameter)4 RequestParam (org.springframework.web.bind.annotation.RequestParam)4 MvcAnnotationPredicates.requestPart (org.springframework.web.method.MvcAnnotationPredicates.requestPart)3 List (java.util.List)2 Optional (java.util.Optional)2