Search in sources :

Example 16 with MockPortResolver

use of org.springframework.security.MockPortResolver in project spring-security by spring-projects.

the class LoginUrlAuthenticationEntryPointTests method testHttpsOperationFromOriginalHttpUrl.

@Test
public void testHttpsOperationFromOriginalHttpUrl() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRequestURI("/some_path");
    request.setScheme("http");
    request.setServerName("www.example.com");
    request.setContextPath("/bigWebApp");
    request.setServerPort(80);
    MockHttpServletResponse response = new MockHttpServletResponse();
    LoginUrlAuthenticationEntryPoint ep = new LoginUrlAuthenticationEntryPoint("/hello");
    ep.setPortMapper(new PortMapperImpl());
    ep.setForceHttps(true);
    ep.setPortMapper(new PortMapperImpl());
    ep.setPortResolver(new MockPortResolver(80, 443));
    ep.afterPropertiesSet();
    ep.commence(request, response, null);
    assertThat(response.getRedirectedUrl()).isEqualTo("https://www.example.com/bigWebApp/hello");
    request.setServerPort(8080);
    response = new MockHttpServletResponse();
    ep.setPortResolver(new MockPortResolver(8080, 8443));
    ep.commence(request, response, null);
    assertThat(response.getRedirectedUrl()).isEqualTo("https://www.example.com:8443/bigWebApp/hello");
    // Now test an unusual custom HTTP:HTTPS is handled properly
    request.setServerPort(8888);
    response = new MockHttpServletResponse();
    ep.commence(request, response, null);
    assertThat(response.getRedirectedUrl()).isEqualTo("https://www.example.com:8443/bigWebApp/hello");
    PortMapperImpl portMapper = new PortMapperImpl();
    Map<String, String> map = new HashMap<String, String>();
    map.put("8888", "9999");
    portMapper.setPortMappings(map);
    response = new MockHttpServletResponse();
    ep = new LoginUrlAuthenticationEntryPoint("/hello");
    ep.setPortMapper(new PortMapperImpl());
    ep.setForceHttps(true);
    ep.setPortMapper(portMapper);
    ep.setPortResolver(new MockPortResolver(8888, 9999));
    ep.afterPropertiesSet();
    ep.commence(request, response, null);
    assertThat(response.getRedirectedUrl()).isEqualTo("https://www.example.com:9999/bigWebApp/hello");
}
Also used : MockPortResolver(org.springframework.security.MockPortResolver) PortMapperImpl(org.springframework.security.web.PortMapperImpl) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 17 with MockPortResolver

use of org.springframework.security.MockPortResolver in project spring-security by spring-projects.

the class DefaultSavedRequestTests method headersAreCaseInsensitive.

// SEC-308, SEC-315
@Test
public void headersAreCaseInsensitive() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("USER-aGenT", "Mozilla");
    DefaultSavedRequest saved = new DefaultSavedRequest(request, new MockPortResolver(8080, 8443));
    assertThat(saved.getHeaderValues("user-agent").get(0)).isEqualTo("Mozilla");
}
Also used : MockPortResolver(org.springframework.security.MockPortResolver) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) DefaultSavedRequest(org.springframework.security.web.savedrequest.DefaultSavedRequest) Test(org.junit.Test)

Example 18 with MockPortResolver

use of org.springframework.security.MockPortResolver in project spring-security by spring-projects.

the class DefaultSavedRequestTests method parametersAreCaseSensitive.

// SEC-3082
@Test
public void parametersAreCaseSensitive() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("AnotHerTest", "Hi dad");
    request.addParameter("thisisatest", "Hi mom");
    DefaultSavedRequest saved = new DefaultSavedRequest(request, new MockPortResolver(8080, 8443));
    assertThat(saved.getParameterValues("thisisatest")[0]).isEqualTo("Hi mom");
    assertThat(saved.getParameterValues("anothertest")).isNull();
}
Also used : MockPortResolver(org.springframework.security.MockPortResolver) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) DefaultSavedRequest(org.springframework.security.web.savedrequest.DefaultSavedRequest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)18 MockPortResolver (org.springframework.security.MockPortResolver)18 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)16 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)13 PortMapperImpl (org.springframework.security.web.PortMapperImpl)13 RetryWithHttpsEntryPoint (org.springframework.security.web.access.channel.RetryWithHttpsEntryPoint)5 DefaultSavedRequest (org.springframework.security.web.savedrequest.DefaultSavedRequest)3 HashMap (java.util.HashMap)2 FilterChain (javax.servlet.FilterChain)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)1 HttpSessionRequestCache (org.springframework.security.web.savedrequest.HttpSessionRequestCache)1