use of org.springframework.mock.web.test.MockHttpServletRequest in project spring-framework by spring-projects.
the class ServletRequestUtilsTests method testGetBooleanParameterWithDefaultValueHandlingIsFastEnough.
@Test
public void testGetBooleanParameterWithDefaultValueHandlingIsFastEnough() {
Assume.group(TestGroup.PERFORMANCE);
MockHttpServletRequest request = new MockHttpServletRequest();
StopWatch sw = new StopWatch();
sw.start();
for (int i = 0; i < 1000000; i++) {
ServletRequestUtils.getBooleanParameter(request, "nonExistingParam", false);
}
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 testLongParameter.
@Test
public void testLongParameter() throws ServletRequestBindingException {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addParameter("param1", "5");
request.addParameter("param2", "e");
request.addParameter("paramEmpty", "");
assertEquals(ServletRequestUtils.getLongParameter(request, "param1"), new Long(5L));
assertEquals(ServletRequestUtils.getLongParameter(request, "param1", 6L), 5L);
assertEquals(ServletRequestUtils.getRequiredIntParameter(request, "param1"), 5L);
assertEquals(ServletRequestUtils.getLongParameter(request, "param2", 6L), 6L);
try {
ServletRequestUtils.getRequiredLongParameter(request, "param2");
fail("Should have thrown ServletRequestBindingException");
} catch (ServletRequestBindingException ex) {
// expected
}
assertEquals(ServletRequestUtils.getLongParameter(request, "param3"), null);
assertEquals(ServletRequestUtils.getLongParameter(request, "param3", 6L), 6L);
try {
ServletRequestUtils.getRequiredLongParameter(request, "param3");
fail("Should have thrown ServletRequestBindingException");
} catch (ServletRequestBindingException ex) {
// expected
}
try {
ServletRequestUtils.getRequiredLongParameter(request, "paramEmpty");
fail("Should have thrown ServletRequestBindingException");
} catch (ServletRequestBindingException ex) {
// expected
}
}
use of org.springframework.mock.web.test.MockHttpServletRequest in project spring-framework by spring-projects.
the class WebAsyncManagerTests method setup.
@Before
public void setup() {
this.servletRequest = new MockHttpServletRequest();
this.asyncManager = WebAsyncUtils.getAsyncManager(servletRequest);
this.asyncManager.setTaskExecutor(new SyncTaskExecutor());
this.asyncWebRequest = mock(AsyncWebRequest.class);
this.asyncManager.setAsyncWebRequest(this.asyncWebRequest);
verify(this.asyncWebRequest).addCompletionHandler((Runnable) notNull());
reset(this.asyncWebRequest);
}
use of org.springframework.mock.web.test.MockHttpServletRequest in project spring-framework by spring-projects.
the class WebAsyncManagerTimeoutTests method setup.
@Before
public void setup() {
this.servletRequest = new MockHttpServletRequest("GET", "/test");
this.servletRequest.setAsyncSupported(true);
this.servletResponse = new MockHttpServletResponse();
this.asyncWebRequest = new StandardServletAsyncWebRequest(servletRequest, servletResponse);
AsyncTaskExecutor executor = mock(AsyncTaskExecutor.class);
this.asyncManager = WebAsyncUtils.getAsyncManager(servletRequest);
this.asyncManager.setTaskExecutor(executor);
this.asyncManager.setAsyncWebRequest(this.asyncWebRequest);
}
use of org.springframework.mock.web.test.MockHttpServletRequest in project spring-framework by spring-projects.
the class WebApplicationContextScopeTests method testRequestScope.
@Test
public void testRequestScope() {
WebApplicationContext ac = initApplicationContext(WebApplicationContext.SCOPE_REQUEST);
MockHttpServletRequest request = new MockHttpServletRequest();
ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
RequestContextHolder.setRequestAttributes(requestAttributes);
try {
assertNull(request.getAttribute(NAME));
DerivedTestBean bean = ac.getBean(NAME, DerivedTestBean.class);
assertSame(bean, request.getAttribute(NAME));
assertSame(bean, ac.getBean(NAME));
requestAttributes.requestCompleted();
assertTrue(bean.wasDestroyed());
} finally {
RequestContextHolder.setRequestAttributes(null);
}
}
Aggregations