Search in sources :

Example 46 with ServletWebRequest

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

the class WebRequestDataBinderTests method testMultipartFilesAsStringArray.

@Test
public void testMultipartFilesAsStringArray() {
    TestBean target = new TestBean();
    WebRequestDataBinder binder = new WebRequestDataBinder(target);
    binder.registerCustomEditor(String.class, new StringMultipartFileEditor());
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    request.addFile(new MockMultipartFile("stringArray", "Juergen".getBytes()));
    request.addFile(new MockMultipartFile("stringArray", "Eva".getBytes()));
    binder.bind(new ServletWebRequest(request));
    assertEquals(2, target.getStringArray().length);
    assertEquals("Juergen", target.getStringArray()[0]);
    assertEquals("Eva", target.getStringArray()[1]);
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) StringMultipartFileEditor(org.springframework.web.multipart.support.StringMultipartFileEditor) MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 47 with ServletWebRequest

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

the class WebRequestDataBinderTests method testBindingWithNestedObjectCreationThroughAutoGrow.

@Test
public void testBindingWithNestedObjectCreationThroughAutoGrow() throws Exception {
    TestBean tb = new TestBeanWithConcreteSpouse();
    WebRequestDataBinder binder = new WebRequestDataBinder(tb, "person");
    binder.setIgnoreUnknownFields(false);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("concreteSpouse.name", "test");
    binder.bind(new ServletWebRequest(request));
    assertNotNull(tb.getSpouse());
    assertEquals("test", tb.getSpouse().getName());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 48 with ServletWebRequest

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

the class WebRequestDataBinderTests method testFieldDefaultNonBoolean.

@Test
public void testFieldDefaultNonBoolean() throws Exception {
    TestBean target = new TestBean();
    WebRequestDataBinder binder = new WebRequestDataBinder(target);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("!name", "anonymous");
    request.addParameter("name", "Scott");
    binder.bind(new ServletWebRequest(request));
    assertEquals("Scott", target.getName());
    request.removeParameter("name");
    binder.bind(new ServletWebRequest(request));
    assertEquals("anonymous", target.getName());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 49 with ServletWebRequest

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

the class WebRequestDataBinderTests method testFieldPrefixCausesFieldReset.

@Test
public void testFieldPrefixCausesFieldReset() throws Exception {
    TestBean target = new TestBean();
    WebRequestDataBinder binder = new WebRequestDataBinder(target);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("_postProcessed", "visible");
    request.addParameter("postProcessed", "on");
    binder.bind(new ServletWebRequest(request));
    assertTrue(target.isPostProcessed());
    request.removeParameter("postProcessed");
    binder.bind(new ServletWebRequest(request));
    assertFalse(target.isPostProcessed());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 50 with ServletWebRequest

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

the class WebRequestDataBinderTests method testFieldDefault.

@Test
public void testFieldDefault() throws Exception {
    TestBean target = new TestBean();
    WebRequestDataBinder binder = new WebRequestDataBinder(target);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("!postProcessed", "off");
    request.addParameter("postProcessed", "on");
    binder.bind(new ServletWebRequest(request));
    assertTrue(target.isPostProcessed());
    request.removeParameter("postProcessed");
    binder.bind(new ServletWebRequest(request));
    assertFalse(target.isPostProcessed());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) 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