Example usage for org.springframework.mock.web MockHttpServletRequest getSession

List of usage examples for org.springframework.mock.web MockHttpServletRequest getSession

Introduction

In this page you can find the example usage for org.springframework.mock.web MockHttpServletRequest getSession.

Prototype

@Override
    @Nullable
    public HttpSession getSession() 

Source Link

Usage

From source file:org.ppwcode.vernacular.l10n_III.dojo.DojoDjConfigFilterTest.java

private void testDojoDjConfigFilterHelper(String type, String input, String output, String bestLocale)
        throws UnsupportedEncodingException, IOException, ServletException {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/index.html");
    MockHttpServletResponse response = new MockHttpServletResponse();

    Servlet testServlet = new SimpleServlet(type, input);
    PassThroughFilterChain chain = new PassThroughFilterChain(testServlet);

    HttpSession session = request.getSession();
    session.setAttribute(HttpRequestLocaleFilter.ATTRIBUTE_PREFERRED_LOCALE,
            LocaleHelpers.constructLocaleFromString(bestLocale));

    Filter djConfigFilter = new DojoDjConfigFilter();
    djConfigFilter.doFilter(request, response, chain);

    LOG.debug("input\n" + input);
    LOG.debug("output\n" + response.getContentAsString());

    Assert.assertEquals(output, response.getContentAsString());
    Assert.assertEquals(output.length(), response.getContentLength());
}

From source file:org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilterTests.java

@Test
public void testNormalOperationWithDefaultFilterProcessesUrl() throws Exception {
    // Setup our HTTP request
    MockHttpServletRequest request = createMockAuthenticationRequest();
    HttpSession sessionPreAuth = request.getSession();

    // Setup our filter configuration
    MockFilterConfig config = new MockFilterConfig(null, null);

    // Setup our expectation that the filter chain will not be invoked, as we redirect
    // to defaultTargetUrl
    MockFilterChain chain = new MockFilterChain(false);
    MockHttpServletResponse response = new MockHttpServletResponse();

    // Setup our test object, to grant access
    MockAuthenticationFilter filter = new MockAuthenticationFilter(true);

    filter.setFilterProcessesUrl("/j_mock_post");
    filter.setSessionAuthenticationStrategy(mock(SessionAuthenticationStrategy.class));
    filter.setAuthenticationSuccessHandler(successHandler);
    filter.setAuthenticationFailureHandler(failureHandler);
    filter.setAuthenticationManager(mock(AuthenticationManager.class));
    filter.afterPropertiesSet();/*  ww  w  . j a v a  2 s  . co  m*/

    // Test
    filter.doFilter(request, response, chain);
    assertThat(response.getRedirectedUrl()).isEqualTo("/mycontext/logged_in.jsp");
    assertThat(SecurityContextHolder.getContext().getAuthentication()).isNotNull();
    assertThat(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString())
            .isEqualTo("test");
    // Should still have the same session
    assertThat(request.getSession()).isEqualTo(sessionPreAuth);
}