Search in sources :

Example 1 with MockPart

use of org.springframework.mock.web.test.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);
    assertTrue(result instanceof Part);
    assertEquals("Invalid result", expected, result);
}
Also used : RequestParam(org.springframework.web.bind.annotation.RequestParam) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RequestPart(org.springframework.web.bind.annotation.RequestPart) MvcAnnotationPredicates.requestPart(org.springframework.web.method.MvcAnnotationPredicates.requestPart) MockPart(org.springframework.mock.web.test.MockPart) Part(javax.servlet.http.Part) MockPart(org.springframework.mock.web.test.MockPart) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 2 with MockPart

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

the class RequestParamMethodArgumentResolverTests method resolvePart.

@Test
public void resolvePart() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockPart expected = new MockPart("pfile", "Hello World".getBytes());
    request.setMethod("POST");
    request.setContentType("multipart/form-data");
    request.addPart(expected);
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Part.class);
    Object result = resolver.resolveArgument(param, null, webRequest, null);
    assertTrue(result instanceof Part);
    assertEquals("Invalid result", expected, result);
}
Also used : RequestParam(org.springframework.web.bind.annotation.RequestParam) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RequestPart(org.springframework.web.bind.annotation.RequestPart) MvcAnnotationPredicates.requestPart(org.springframework.web.method.MvcAnnotationPredicates.requestPart) MockPart(org.springframework.mock.web.test.MockPart) Part(javax.servlet.http.Part) MockPart(org.springframework.mock.web.test.MockPart) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 3 with MockPart

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

the class RequestPartMethodArgumentResolverTests method resolvePartArgument.

@Test
public void resolvePartArgument() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContentType("multipart/form-data");
    MockPart expected = new MockPart("part", "Hello World".getBytes());
    request.addPart(expected);
    request.addPart(new MockPart("otherPart", "Hello World".getBytes()));
    webRequest = new ServletWebRequest(request);
    Object result = resolver.resolveArgument(paramPart, null, webRequest, null);
    assertTrue(result instanceof Part);
    assertEquals("Invalid result", expected, result);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RequestPart(org.springframework.web.bind.annotation.RequestPart) MockPart(org.springframework.mock.web.test.MockPart) Part(javax.servlet.http.Part) MockPart(org.springframework.mock.web.test.MockPart) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 4 with MockPart

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

the class RequestPartMethodArgumentResolverTests method resolvePartArrayArgument.

@Test
public void resolvePartArrayArgument() 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(paramPartArray, null, webRequest, null);
    assertTrue(result instanceof Part[]);
    Part[] parts = (Part[]) result;
    assertEquals(2, parts.length);
    assertEquals(parts[0], part1);
    assertEquals(parts[1], part2);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RequestPart(org.springframework.web.bind.annotation.RequestPart) MockPart(org.springframework.mock.web.test.MockPart) Part(javax.servlet.http.Part) MockPart(org.springframework.mock.web.test.MockPart) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 5 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