Search in sources :

Example 56 with MockHttpServletRequest

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

the class SessionLocaleResolverTests method testSetAndResolveLocale.

@Test
public void testSetAndResolveLocale() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    SessionLocaleResolver resolver = new SessionLocaleResolver();
    resolver.setLocale(request, response, Locale.GERMAN);
    assertEquals(Locale.GERMAN, resolver.resolveLocale(request));
    HttpSession session = request.getSession();
    request = new MockHttpServletRequest();
    request.setSession(session);
    resolver = new SessionLocaleResolver();
    assertEquals(Locale.GERMAN, resolver.resolveLocale(request));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) HttpSession(javax.servlet.http.HttpSession) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 57 with MockHttpServletRequest

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

the class SessionLocaleResolverTests method testSetLocaleToNullLocale.

@Test
public void testSetLocaleToNullLocale() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addPreferredLocale(Locale.TAIWAN);
    request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, Locale.GERMAN);
    MockHttpServletResponse response = new MockHttpServletResponse();
    SessionLocaleResolver resolver = new SessionLocaleResolver();
    resolver.setLocale(request, response, null);
    Locale locale = (Locale) request.getSession().getAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME);
    assertNull(locale);
    HttpSession session = request.getSession();
    request = new MockHttpServletRequest();
    request.addPreferredLocale(Locale.TAIWAN);
    request.setSession(session);
    resolver = new SessionLocaleResolver();
    assertEquals(Locale.TAIWAN, resolver.resolveLocale(request));
}
Also used : Locale(java.util.Locale) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) HttpSession(javax.servlet.http.HttpSession) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 58 with MockHttpServletRequest

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

the class ControllerTests method parameterizableViewController.

@Test
public void parameterizableViewController() throws Exception {
    String viewName = "viewName";
    ParameterizableViewController pvc = new ParameterizableViewController();
    pvc.setViewName(viewName);
    // We don't care about the params.
    ModelAndView mv = pvc.handleRequest(new MockHttpServletRequest("GET", "foo.html"), new MockHttpServletResponse());
    assertTrue("model has no data", mv.getModel().size() == 0);
    assertTrue("model has correct viewname", mv.getViewName().equals(viewName));
    assertTrue("getViewName matches", pvc.getViewName().equals(viewName));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ModelAndView(org.springframework.web.servlet.ModelAndView) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 59 with MockHttpServletRequest

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

the class ControllerTests method servletWrappingController.

@Test
public void servletWrappingController() throws Exception {
    HttpServletRequest request = new MockHttpServletRequest("GET", "/somePath");
    HttpServletResponse response = new MockHttpServletResponse();
    ServletWrappingController swc = new ServletWrappingController();
    swc.setServletClass(TestServlet.class);
    swc.setServletName("action");
    Properties props = new Properties();
    props.setProperty("config", "myValue");
    swc.setInitParameters(props);
    swc.afterPropertiesSet();
    assertNotNull(TestServlet.config);
    assertEquals("action", TestServlet.config.getServletName());
    assertEquals("myValue", TestServlet.config.getInitParameter("config"));
    assertNull(TestServlet.request);
    assertFalse(TestServlet.destroyed);
    assertNull(swc.handleRequest(request, response));
    assertEquals(request, TestServlet.request);
    assertEquals(response, TestServlet.response);
    assertFalse(TestServlet.destroyed);
    swc.destroy();
    assertTrue(TestServlet.destroyed);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) Properties(java.util.Properties) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 60 with MockHttpServletRequest

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

the class AcceptHeaderLocaleResolverTests method request.

private HttpServletRequest request(Locale... locales) {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setPreferredLocales(Arrays.asList(locales));
    return request;
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest)

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