Search in sources :

Example 11 with MockHttpServletRequest

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

the class ServletRequestDataBinderTests method testMultipleValuesForParameter.

@Test
public void testMultipleValuesForParameter() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    String[] original = new String[] { "Tony", "Rod" };
    request.addParameter("forname", original);
    ServletRequestParameterPropertyValues pvs = new ServletRequestParameterPropertyValues(request);
    assertTrue("Found 1 parameter", pvs.getPropertyValues().length == 1);
    assertTrue("Found array value", pvs.getPropertyValue("forname").getValue() instanceof String[]);
    String[] values = (String[]) pvs.getPropertyValue("forname").getValue();
    assertEquals("Correct values", Arrays.asList(values), Arrays.asList(original));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Example 12 with MockHttpServletRequest

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

the class ServletRequestUtilsTests method testGetStringParameterWithDefaultValueHandlingIsFastEnough.

@Test
public void testGetStringParameterWithDefaultValueHandlingIsFastEnough() {
    Assume.group(TestGroup.PERFORMANCE);
    MockHttpServletRequest request = new MockHttpServletRequest();
    StopWatch sw = new StopWatch();
    sw.start();
    for (int i = 0; i < 1000000; i++) {
        ServletRequestUtils.getStringParameter(request, "nonExistingParam", "defaultValue");
    }
    sw.stop();
    System.out.println(sw.getTotalTimeMillis());
    assertTrue("getStringParameter took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 250);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) StopWatch(org.springframework.util.StopWatch) Test(org.junit.Test)

Example 13 with MockHttpServletRequest

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

the class ServletRequestUtilsTests method testStringParameter.

@Test
public void testStringParameter() throws ServletRequestBindingException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("param1", "str");
    request.addParameter("paramEmpty", "");
    assertEquals("str", ServletRequestUtils.getStringParameter(request, "param1"));
    assertEquals("str", ServletRequestUtils.getStringParameter(request, "param1", "string"));
    assertEquals("str", ServletRequestUtils.getRequiredStringParameter(request, "param1"));
    assertEquals(null, ServletRequestUtils.getStringParameter(request, "param3"));
    assertEquals("string", ServletRequestUtils.getStringParameter(request, "param3", "string"));
    assertNull(ServletRequestUtils.getStringParameter(request, "param3", null));
    try {
        ServletRequestUtils.getRequiredStringParameter(request, "param3");
        fail("Should have thrown ServletRequestBindingException");
    } catch (ServletRequestBindingException ex) {
    // expected
    }
    assertEquals("", ServletRequestUtils.getStringParameter(request, "paramEmpty"));
    assertEquals("", ServletRequestUtils.getRequiredStringParameter(request, "paramEmpty"));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Example 14 with MockHttpServletRequest

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

the class ServletRequestUtilsTests method testGetIntParameterWithDefaultValueHandlingIsFastEnough.

@Test
public void testGetIntParameterWithDefaultValueHandlingIsFastEnough() {
    Assume.group(TestGroup.PERFORMANCE);
    MockHttpServletRequest request = new MockHttpServletRequest();
    StopWatch sw = new StopWatch();
    sw.start();
    for (int i = 0; i < 1000000; i++) {
        ServletRequestUtils.getIntParameter(request, "nonExistingParam", 0);
    }
    sw.stop();
    System.out.println(sw.getTotalTimeMillis());
    assertTrue("getStringParameter took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 250);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) StopWatch(org.springframework.util.StopWatch) Test(org.junit.Test)

Example 15 with MockHttpServletRequest

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

the class WebRequestDataBinderTests method testCollectionFieldsDefault.

// SPR-13502
@Test
public void testCollectionFieldsDefault() throws Exception {
    TestBean target = new TestBean();
    target.setSomeSet(null);
    target.setSomeList(null);
    target.setSomeMap(null);
    WebRequestDataBinder binder = new WebRequestDataBinder(target);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("_someSet", "visible");
    request.addParameter("_someList", "visible");
    request.addParameter("_someMap", "visible");
    binder.bind(new ServletWebRequest(request));
    assertThat(target.getSomeSet(), notNullValue());
    assertThat(target.getSomeSet(), isA(Set.class));
    assertThat(target.getSomeList(), notNullValue());
    assertThat(target.getSomeList(), isA(List.class));
    assertThat(target.getSomeMap(), notNullValue());
    assertThat(target.getSomeMap(), isA(Map.class));
}
Also used : Set(java.util.Set) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) List(java.util.List) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) HashMap(java.util.HashMap) Map(java.util.Map) 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