Search in sources :

Example 31 with MockHttpServletRequest

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

the class SecurityMockMvcRequestBuildersFormLoginTests method usesAcceptMediaForContentNegotiation.

// gh-3920
@Test
public void usesAcceptMediaForContentNegotiation() throws Exception {
    MockHttpServletRequest request = formLogin("/login").user("username", "admin").password("password", "secret").buildRequest(this.servletContext);
    assertThat(request.getHeader("Accept")).isEqualTo(MediaType.APPLICATION_FORM_URLENCODED_VALUE);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Test(org.junit.Test)

Example 32 with MockHttpServletRequest

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

the class SecurityMockMvcRequestBuildersFormLoginTests method defaults.

@Test
public void defaults() throws Exception {
    MockHttpServletRequest request = formLogin().buildRequest(this.servletContext);
    CsrfToken token = (CsrfToken) request.getAttribute(CsrfRequestPostProcessor.TestCsrfTokenRepository.TOKEN_ATTR_NAME);
    assertThat(request.getParameter("username")).isEqualTo("user");
    assertThat(request.getParameter("password")).isEqualTo("password");
    assertThat(request.getMethod()).isEqualTo("POST");
    assertThat(request.getParameter(token.getParameterName())).isEqualTo(token.getToken());
    assertThat(request.getRequestURI()).isEqualTo("/login");
    assertThat(request.getParameter("_csrf")).isNotNull();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) CsrfToken(org.springframework.security.web.csrf.CsrfToken) Test(org.junit.Test)

Example 33 with MockHttpServletRequest

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

the class SecurityMockMvcRequestBuildersFormLoginTests method custom.

@Test
public void custom() throws Exception {
    MockHttpServletRequest request = formLogin("/login").user("username", "admin").password("password", "secret").buildRequest(this.servletContext);
    CsrfToken token = (CsrfToken) request.getAttribute(CsrfRequestPostProcessor.TestCsrfTokenRepository.TOKEN_ATTR_NAME);
    assertThat(request.getParameter("username")).isEqualTo("admin");
    assertThat(request.getParameter("password")).isEqualTo("secret");
    assertThat(request.getMethod()).isEqualTo("POST");
    assertThat(request.getParameter(token.getParameterName())).isEqualTo(token.getToken());
    assertThat(request.getRequestURI()).isEqualTo("/login");
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) CsrfToken(org.springframework.security.web.csrf.CsrfToken) Test(org.junit.Test)

Example 34 with MockHttpServletRequest

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

the class SecurityMockMvcRequestBuildersFormLogoutTests method defaults.

@Test
public void defaults() throws Exception {
    MockHttpServletRequest request = logout().buildRequest(servletContext);
    CsrfToken token = (CsrfToken) request.getAttribute(CsrfRequestPostProcessor.TestCsrfTokenRepository.TOKEN_ATTR_NAME);
    assertThat(request.getMethod()).isEqualTo("POST");
    assertThat(request.getParameter(token.getParameterName())).isEqualTo(token.getToken());
    assertThat(request.getRequestURI()).isEqualTo("/logout");
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) CsrfToken(org.springframework.security.web.csrf.CsrfToken) Test(org.junit.Test)

Example 35 with MockHttpServletRequest

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

the class SecurityMockMvcRequestBuildersFormLogoutTests method custom.

@Test
public void custom() throws Exception {
    MockHttpServletRequest request = logout("/admin/logout").buildRequest(servletContext);
    CsrfToken token = (CsrfToken) request.getAttribute(CsrfRequestPostProcessor.TestCsrfTokenRepository.TOKEN_ATTR_NAME);
    assertThat(request.getMethod()).isEqualTo("POST");
    assertThat(request.getParameter(token.getParameterName())).isEqualTo(token.getToken());
    assertThat(request.getRequestURI()).isEqualTo("/admin/logout");
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) CsrfToken(org.springframework.security.web.csrf.CsrfToken) Test(org.junit.Test)

Aggregations

MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1136 Test (org.junit.Test)882 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)605 Before (org.junit.Before)107 MockFilterChain (org.springframework.mock.web.MockFilterChain)93 MockServletContext (org.springframework.mock.web.MockServletContext)82 FilterChain (javax.servlet.FilterChain)79 Authentication (org.springframework.security.core.Authentication)70 HttpServletRequest (javax.servlet.http.HttpServletRequest)62 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)60 MockRequestContext (org.springframework.webflow.test.MockRequestContext)53 MockHttpSession (org.springframework.mock.web.MockHttpSession)51 HttpServletResponse (javax.servlet.http.HttpServletResponse)48 Cookie (javax.servlet.http.Cookie)46 HashMap (java.util.HashMap)39 TicketGrantingTicket (org.apereo.cas.ticket.TicketGrantingTicket)33 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)32 ModelAndView (org.springframework.web.servlet.ModelAndView)32 RegisteredService (org.apereo.cas.services.RegisteredService)30 MockPortletWindowId (org.apereo.portal.mock.portlet.om.MockPortletWindowId)30