Search in sources :

Example 26 with MockHttpServletRequest

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);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) StopWatch(org.springframework.util.StopWatch) Test(org.junit.Test)

Example 27 with MockHttpServletRequest

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
    }
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Example 28 with MockHttpServletRequest

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);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Before(org.junit.Before)

Example 29 with MockHttpServletRequest

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);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) AsyncTaskExecutor(org.springframework.core.task.AsyncTaskExecutor) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Before(org.junit.Before)

Example 30 with MockHttpServletRequest

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);
    }
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) 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