Search in sources :

Example 21 with MockHttpServletRequest

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

the class WebRequestDataBinderTests method testNoParameters.

@Test
public void testNoParameters() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    ServletRequestParameterPropertyValues pvs = new ServletRequestParameterPropertyValues(request);
    assertTrue("Found no parameters", pvs.getPropertyValues().length == 0);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ServletRequestParameterPropertyValues(org.springframework.web.bind.ServletRequestParameterPropertyValues) Test(org.junit.Test)

Example 22 with MockHttpServletRequest

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

the class ServletRequestDataBinderTests method testFieldDefaultPreemptsFieldMarker.

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

Example 23 with MockHttpServletRequest

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

the class ServletRequestDataBinderTests method testFieldDefaultNonBoolean.

@Test
public void testFieldDefaultNonBoolean() throws Exception {
    TestBean target = new TestBean();
    ServletRequestDataBinder binder = new ServletRequestDataBinder(target);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("!name", "anonymous");
    request.addParameter("name", "Scott");
    binder.bind(request);
    assertEquals("Scott", target.getName());
    request.removeParameter("name");
    binder.bind(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) Test(org.junit.Test)

Example 24 with MockHttpServletRequest

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

the class ServletRequestDataBinderTests method testBindingWithNestedObjectCreation.

@Test
public void testBindingWithNestedObjectCreation() throws Exception {
    TestBean tb = new TestBean();
    ServletRequestDataBinder binder = new ServletRequestDataBinder(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(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) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Example 25 with MockHttpServletRequest

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

the class ServletRequestUtilsTests method testDoubleParameters.

@Test
public void testDoubleParameters() throws ServletRequestBindingException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("param", new String[] { "1.5", "2.5", "3" });
    request.addParameter("param2", "1.5");
    request.addParameter("param2", "2");
    request.addParameter("param2", "bogus");
    double[] array = new double[] { 1.5, 2.5, 3 };
    double[] values = ServletRequestUtils.getRequiredDoubleParameters(request, "param");
    assertEquals(3, values.length);
    for (int i = 0; i < array.length; i++) {
        assertEquals(array[i], values[i], 0);
    }
    try {
        ServletRequestUtils.getRequiredDoubleParameters(request, "param2");
        fail("Should have thrown ServletRequestBindingException");
    } catch (ServletRequestBindingException ex) {
    // expected
    }
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Aggregations

MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)646 Test (org.junit.Test)540 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)330 Before (org.junit.Before)78 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)77 MockServletContext (org.springframework.mock.web.test.MockServletContext)56 TestBean (org.springframework.tests.sample.beans.TestBean)43 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)41 HttpServletResponse (javax.servlet.http.HttpServletResponse)40 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)37 HttpServletRequest (javax.servlet.http.HttpServletRequest)35 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)30 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)29 ITestBean (org.springframework.tests.sample.beans.ITestBean)28 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)28 ServletException (javax.servlet.ServletException)27 HashMap (java.util.HashMap)26 FilterChain (javax.servlet.FilterChain)26 ModelAndViewContainer (org.springframework.web.method.support.ModelAndViewContainer)23 Cookie (javax.servlet.http.Cookie)22