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));
}
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);
}
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"));
}
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);
}
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));
}
Aggregations