Search in sources :

Example 51 with ServletWebRequest

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

the class WebRequestDataBinderTests method testMultipartFileAsString.

@Test
public void testMultipartFileAsString() {
    TestBean target = new TestBean();
    WebRequestDataBinder binder = new WebRequestDataBinder(target);
    binder.registerCustomEditor(String.class, new StringMultipartFileEditor());
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    request.addFile(new MockMultipartFile("name", "Juergen".getBytes()));
    binder.bind(new ServletWebRequest(request));
    assertEquals("Juergen", target.getName());
}
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 52 with ServletWebRequest

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

the class WebRequestDataBinderTests method testBindingWithNestedObjectCreation.

@Test
public void testBindingWithNestedObjectCreation() throws Exception {
    TestBean tb = new TestBean();
    WebRequestDataBinder binder = new WebRequestDataBinder(tb, "person");
    binder.registerCustomEditor(ITestBean.class, new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue(new TestBean());
        }
    });
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("spouse", "someValue");
    request.addParameter("spouse.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) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Example 53 with ServletWebRequest

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

the class WebRequestDataBinderTests method testFieldDefaultPreemptsFieldMarker.

@Test
public void testFieldDefaultPreemptsFieldMarker() throws Exception {
    TestBean target = new TestBean();
    WebRequestDataBinder binder = new WebRequestDataBinder(target);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("!postProcessed", "on");
    request.addParameter("_postProcessed", "visible");
    request.addParameter("postProcessed", "on");
    binder.bind(new ServletWebRequest(request));
    assertTrue(target.isPostProcessed());
    request.removeParameter("postProcessed");
    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 54 with ServletWebRequest

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

the class ModelFactoryOrderingTests method setup.

@Before
public void setup() {
    this.sessionAttributeStore = new DefaultSessionAttributeStore();
    this.webRequest = new ServletWebRequest(new MockHttpServletRequest(), new MockHttpServletResponse());
    this.mavContainer = new ModelAndViewContainer();
    this.mavContainer.addAttribute("methods", new ArrayList<String>());
}
Also used : ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) DefaultSessionAttributeStore(org.springframework.web.bind.support.DefaultSessionAttributeStore) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Before(org.junit.Before)

Example 55 with ServletWebRequest

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

the class ModelFactoryTests method setUp.

@Before
public void setUp() throws Exception {
    this.webRequest = new ServletWebRequest(new MockHttpServletRequest());
    this.attributeStore = new DefaultSessionAttributeStore();
    this.attributeHandler = new SessionAttributesHandler(TestController.class, this.attributeStore);
    this.controller = new TestController();
    this.mavContainer = new ModelAndViewContainer();
}
Also used : ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) DefaultSessionAttributeStore(org.springframework.web.bind.support.DefaultSessionAttributeStore) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Before(org.junit.Before)

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