Search in sources :

Example 61 with ServletWebRequest

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

the class ModelAttributeMethodProcessorTests method setUp.

@Before
public void setUp() throws Exception {
    this.request = new ServletWebRequest(new MockHttpServletRequest());
    this.container = new ModelAndViewContainer();
    this.processor = new ModelAttributeMethodProcessor(false);
    Method method = ModelAttributeHandler.class.getDeclaredMethod("modelAttribute", TestBean.class, Errors.class, int.class, TestBean.class, TestBean.class, TestBean.class);
    this.paramNamedValidModelAttr = new SynthesizingMethodParameter(method, 0);
    this.paramErrors = new SynthesizingMethodParameter(method, 1);
    this.paramInt = new SynthesizingMethodParameter(method, 2);
    this.paramModelAttr = new SynthesizingMethodParameter(method, 3);
    this.paramBindingDisabledAttr = new SynthesizingMethodParameter(method, 4);
    this.paramNonSimpleType = new SynthesizingMethodParameter(method, 5);
    method = getClass().getDeclaredMethod("annotatedReturnValue");
    this.returnParamNamedModelAttr = new MethodParameter(method, -1);
    method = getClass().getDeclaredMethod("notAnnotatedReturnValue");
    this.returnParamNonSimpleType = new MethodParameter(method, -1);
}
Also used : SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Before(org.junit.Before)

Example 62 with ServletWebRequest

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

the class RequestParamMethodArgumentResolverTests method isMultipartRequestHttpPut.

// SPR-9079
@Test
public void isMultipartRequestHttpPut() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected = new MockMultipartFile("multipartFileList", "Hello World".getBytes());
    request.addFile(expected);
    request.setMethod("PUT");
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
    Object actual = resolver.resolveArgument(param, null, webRequest, null);
    assertTrue(actual instanceof List);
    assertEquals(expected, ((List<?>) actual).get(0));
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) RequestParam(org.springframework.web.bind.annotation.RequestParam) List(java.util.List) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 63 with ServletWebRequest

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

the class RequestParamMethodArgumentResolverTests method resolveMultipartFileArray.

@Test
public void resolveMultipartFileArray() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected1 = new MockMultipartFile("mfilearray", "Hello World 1".getBytes());
    MultipartFile expected2 = new MockMultipartFile("mfilearray", "Hello World 2".getBytes());
    request.addFile(expected1);
    request.addFile(expected2);
    request.addFile(new MockMultipartFile("other", "Hello World 3".getBytes()));
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile[].class);
    Object result = resolver.resolveArgument(param, null, webRequest, null);
    assertTrue(result instanceof MultipartFile[]);
    MultipartFile[] parts = (MultipartFile[]) result;
    assertEquals(2, parts.length);
    assertEquals(parts[0], expected1);
    assertEquals(parts[1], expected2);
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) RequestParam(org.springframework.web.bind.annotation.RequestParam) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 64 with ServletWebRequest

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

the class RequestParamMethodArgumentResolverTests method resolveMultipartFileListNotannot.

@Test
public void resolveMultipartFileListNotannot() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected1 = new MockMultipartFile("multipartFileList", "Hello World 1".getBytes());
    MultipartFile expected2 = new MockMultipartFile("multipartFileList", "Hello World 2".getBytes());
    request.addFile(expected1);
    request.addFile(expected2);
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
    Object result = resolver.resolveArgument(param, null, webRequest, null);
    assertTrue(result instanceof List);
    assertEquals(Arrays.asList(expected1, expected2), result);
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) RequestParam(org.springframework.web.bind.annotation.RequestParam) List(java.util.List) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 65 with ServletWebRequest

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

the class RequestParamMethodArgumentResolverTests method resolvePartArray.

@Test
public void resolvePartArray() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockPart expected1 = new MockPart("pfilearray", "Hello World 1".getBytes());
    MockPart expected2 = new MockPart("pfilearray", "Hello World 2".getBytes());
    request.setMethod("POST");
    request.setContentType("multipart/form-data");
    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(Part[].class);
    Object result = resolver.resolveArgument(param, null, webRequest, null);
    assertTrue(result instanceof Part[]);
    Part[] parts = (Part[]) result;
    assertEquals(2, parts.length);
    assertEquals(parts[0], expected1);
    assertEquals(parts[1], expected2);
}
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)

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