Search in sources :

Example 71 with MockHttpServletRequest

use of org.springframework.mock.web.MockHttpServletRequest in project grails-core by grails.

the class GrailsResourceUtilsTests method testGetPluginContextPath.

public void testGetPluginContextPath() throws Exception {
    MockServletContext servletContext = new MockServletContext("/myapp");
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
    request.setContextPath("/myapp");
    assertEquals("", GrailsResourceUtils.getStaticResourcePathForResource(new UrlResource(TEST_CONTROLLER_URL), null));
    assertEquals("plugins/myplugin", GrailsResourceUtils.getStaticResourcePathForResource(new UrlResource(TEST_PLUGIN_CTRL), null));
    assertEquals("", GrailsResourceUtils.getStaticResourcePathForResource(new UrlResource(WEBINF_CONTROLLER), null));
    assertEquals("plugins/myplugin", GrailsResourceUtils.getStaticResourcePathForResource(new UrlResource(WEBINF_PLUGIN_CTRL), null));
    assertEquals("/myapp", GrailsResourceUtils.getStaticResourcePathForResource(new UrlResource(WEBINF_CONTROLLER), request.getContextPath()));
    assertEquals("/myapp/plugins/myplugin", GrailsResourceUtils.getStaticResourcePathForResource(new UrlResource(WEBINF_PLUGIN_CTRL), request.getContextPath()));
}
Also used : UrlResource(org.grails.io.support.UrlResource) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockServletContext(org.springframework.mock.web.MockServletContext)

Example 72 with MockHttpServletRequest

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

the class OAuth2ClientAuthenticationProcessingFilterTests method testAuthenticationWithTokenType.

@Test
public void testAuthenticationWithTokenType() throws Exception {
    filter.setRestTemplate(restTemplate);
    filter.setTokenServices(tokenServices);
    DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken("FOO");
    token.setTokenType("foo");
    Mockito.when(restTemplate.getAccessToken()).thenReturn(token);
    Set<String> scopes = new HashSet<String>();
    scopes.addAll(Arrays.asList("read", "write"));
    OAuth2Request storedOAuth2Request = RequestTokenFactory.createOAuth2Request("client", false, scopes);
    this.authentication = new OAuth2Authentication(storedOAuth2Request, null);
    Mockito.when(tokenServices.loadAuthentication("FOO")).thenReturn(authentication);
    Authentication authentication = filter.attemptAuthentication(new MockHttpServletRequest(), null);
    assertEquals("foo", ((OAuth2AuthenticationDetails) authentication.getDetails()).getTokenType());
}
Also used : OAuth2Request(org.springframework.security.oauth2.provider.OAuth2Request) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 73 with MockHttpServletRequest

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

the class OAuth2AuthenticationManagerTests method testDetailsEnhancedOnce.

@Test
public void testDetailsEnhancedOnce() throws Exception {
    authentication.setDetails("DETAILS");
    Mockito.when(tokenServices.loadAuthentication("FOO")).thenReturn(authentication);
    PreAuthenticatedAuthenticationToken request = new PreAuthenticatedAuthenticationToken("FOO", "");
    MockHttpServletRequest servletRequest = new MockHttpServletRequest();
    servletRequest.setAttribute(OAuth2AuthenticationDetails.ACCESS_TOKEN_VALUE, "BAR");
    OAuth2AuthenticationDetails details = new OAuth2AuthenticationDetails(servletRequest);
    request.setDetails(details);
    Authentication result = manager.authenticate(request);
    // Authenticate the same request again to simulate what happens if the app is caching the result from
    // tokenServices.loadAuthentication():
    result = manager.authenticate(request);
    assertEquals(authentication, result);
    assertEquals("BAR", ((OAuth2AuthenticationDetails) result.getDetails()).getTokenValue());
    assertEquals("DETAILS", ((OAuth2AuthenticationDetails) result.getDetails()).getDecodedDetails());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) PreAuthenticatedAuthenticationToken(org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken) Test(org.junit.Test)

Example 74 with MockHttpServletRequest

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

the class OAuth2ClientContextFilterTests method testCurrentUriWithLegalSpaces.

@Test
public void testCurrentUriWithLegalSpaces() throws Exception {
    OAuth2ClientContextFilter filter = new OAuth2ClientContextFilter();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setQueryString("foo=bar%20spam");
    assertEquals("http://localhost?foo=bar%20spam", filter.calculateCurrentUri(request));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Test(org.junit.Test)

Example 75 with MockHttpServletRequest

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

the class OAuth2ClientContextFilterTests method testCurrentUriWithNoQuery.

@Test
public void testCurrentUriWithNoQuery() throws Exception {
    OAuth2ClientContextFilter filter = new OAuth2ClientContextFilter();
    MockHttpServletRequest request = new MockHttpServletRequest();
    assertEquals("http://localhost", filter.calculateCurrentUri(request));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Test(org.junit.Test)

Aggregations

MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3144 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)1989 Test (org.junit.jupiter.api.Test)1907 lombok.val (lombok.val)1124 Test (org.junit.Test)752 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)482 MockServletContext (org.springframework.mock.web.MockServletContext)471 MockRequestContext (org.springframework.webflow.test.MockRequestContext)468 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)245 MockFilterChain (org.springframework.mock.web.MockFilterChain)238 JEEContext (org.pac4j.core.context.JEEContext)156 Authentication (org.springframework.security.core.Authentication)144 BeforeEach (org.junit.jupiter.api.BeforeEach)132 HashMap (java.util.HashMap)123 FilterChain (jakarta.servlet.FilterChain)117 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)108 MockHttpSession (org.springframework.mock.web.MockHttpSession)98 MockTicketGrantingTicket (org.apereo.cas.mock.MockTicketGrantingTicket)96 Before (org.junit.Before)82 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)78