Search in sources :

Example 16 with MockHttpServletRequest

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

the class WebRequestDataBinderTests method testWithCommaSeparatedStringArray.

@Test
public void testWithCommaSeparatedStringArray() throws Exception {
    TestBean target = new TestBean();
    WebRequestDataBinder binder = new WebRequestDataBinder(target);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("stringArray", "bar");
    request.addParameter("stringArray", "abc");
    request.addParameter("stringArray", "123,def");
    binder.bind(new ServletWebRequest(request));
    assertEquals("Expected all three items to be bound", 3, target.getStringArray().length);
    request.removeParameter("stringArray");
    request.addParameter("stringArray", "123,def");
    binder.bind(new ServletWebRequest(request));
    assertEquals("Expected only 1 item to be bound", 1, target.getStringArray().length);
}
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 17 with MockHttpServletRequest

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

the class WebRequestDataBinderTests method testFieldPrefixCausesFieldResetWithIgnoreUnknownFields.

@Test
public void testFieldPrefixCausesFieldResetWithIgnoreUnknownFields() throws Exception {
    TestBean target = new TestBean();
    WebRequestDataBinder binder = new WebRequestDataBinder(target);
    binder.setIgnoreUnknownFields(false);
    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 18 with MockHttpServletRequest

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

the class WebRequestDataBinderTests method testEnumBinding.

@Test
public void testEnumBinding() {
    EnumHolder target = new EnumHolder();
    WebRequestDataBinder binder = new WebRequestDataBinder(target);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("myEnum", "FOO");
    binder.bind(new ServletWebRequest(request));
    assertEquals(MyEnum.FOO, target.getMyEnum());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 19 with MockHttpServletRequest

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

the class WebRequestDataBinderTests method testFieldDefaultWithNestedProperty.

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

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

the class WebRequestDataBinderTests method testPrefix.

@Test
public void testPrefix() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("test_forname", "Tony");
    request.addParameter("test_surname", "Blair");
    request.addParameter("test_age", "" + 50);
    ServletRequestParameterPropertyValues pvs = new ServletRequestParameterPropertyValues(request);
    assertTrue("Didn't fidn normal when given prefix", !pvs.contains("forname"));
    assertTrue("Did treat prefix as normal when not given prefix", pvs.contains("test_forname"));
    pvs = new ServletRequestParameterPropertyValues(request, "test");
    doTestTony(pvs);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ServletRequestParameterPropertyValues(org.springframework.web.bind.ServletRequestParameterPropertyValues) 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