/** * Asserts the request was redirected to the given URL. * <p>This method accepts only exact matches. * @param expectedUrl the exact URL expected */ public static ResultMatcher redirectedUrl(String expectedUrl) { return result -> assertEquals("Redirected URL", expectedUrl, result.getResponse().getRedirectedUrl()); }
private List<NameValuePair> responseHeaders() { Collection<String> headerNames = this.response.getHeaderNames(); List<NameValuePair> responseHeaders = new ArrayList<>(headerNames.size()); for (String headerName : headerNames) { List<Object> headerValues = this.response.getHeaderValues(headerName); for (Object value : headerValues) { responseHeaders.add(new NameValuePair(headerName, String.valueOf(value))); } } String location = this.response.getRedirectedUrl(); if (location != null) { responseHeaders.add(new NameValuePair("Location", location)); } return responseHeaders; }
private WebResponseData webResponseData() throws IOException { List<NameValuePair> responseHeaders = responseHeaders(); int statusCode = (this.response.getRedirectedUrl() != null ? HttpStatus.MOVED_PERMANENTLY.value() : this.response.getStatus()); String statusMessage = statusMessage(statusCode); return new WebResponseData(this.response.getContentAsByteArray(), statusCode, statusMessage, responseHeaders); }
/** * Asserts the request was redirected to the given URL. * <p>This method accepts {@link org.springframework.util.AntPathMatcher} * patterns. * @param urlPattern an AntPath pattern to match against * @since 4.0 * @see org.springframework.util.AntPathMatcher */ public static ResultMatcher redirectedUrlPattern(String urlPattern) { return result -> { assertTrue("No Ant-style path pattern", pathMatcher.isPattern(urlPattern)); String url = result.getResponse().getRedirectedUrl(); assertTrue("Redirected URL does not match the expected URL pattern", (url != null && pathMatcher.match(urlPattern, url))); }; }
@Test public void locationHeaderUpdatesGetRedirectedUrl() { String redirectUrl = "/redirect"; response.setHeader("Location", redirectUrl); assertEquals(redirectUrl, response.getRedirectedUrl()); }
@Test public void readmeSampleWhenRequestSecureResourceThenRedirectToLogin() throws Exception { this.spring.register(SampleWebSecurityConfigurerAdapter.class).autowire(); this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain); assertThat(this.response.getRedirectedUrl()).isEqualTo("http://localhost/login"); }
@Test public void multiHttpSampleWhenRequestLoginWithoutCredentialsThenRedirectToLogin() throws Exception { this.spring.register(SampleMultiHttpSecurityConfig.class).autowire(); this.request.setServletPath("/login"); this.request.setMethod("POST"); this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain); assertThat(this.response.getRedirectedUrl()).isEqualTo("/login?error"); }
@Test public void readmeSampleWhenRequestLoginWithoutCredentialsThenRedirectToLogin() throws Exception { this.spring.register(SampleWebSecurityConfigurerAdapter.class).autowire(); this.request.setServletPath("/login"); this.request.setMethod("POST"); this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain); assertThat(this.response.getRedirectedUrl()).isEqualTo("/login?error"); }
@Test public void customConfiguerCustomizeFormLogin() throws Exception { loadContext(ConfigCustomize.class); request.setPathInfo("/requires-authentication"); springSecurityFilterChain.doFilter(request, response, chain); assertThat(response.getRedirectedUrl()).endsWith("/other"); }
@Test public void customConfiguerFormLogin() throws Exception { loadContext(Config.class); request.setPathInfo("/requires-authentication"); springSecurityFilterChain.doFilter(request, response, chain); assertThat(response.getRedirectedUrl()).endsWith("/custom"); }
@Test public void helloWorldSampleWhenRequestLoginWithoutCredentialsThenRedirectToLogin() throws Exception { this.spring.register(HelloWorldWebSecurityConfigurerAdapter.class).autowire(); this.request.setServletPath("/login"); this.request.setMethod("POST"); this.request.addHeader("Accept", "text/html"); this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain); assertThat(this.response.getRedirectedUrl()).isEqualTo("/login?error"); }
@Test public void oauth2LoginWithCustomLoginPageThenRedirectCustomLoginPage() throws Exception { loadConfig(OAuth2LoginConfigCustomLoginPage.class); String requestUri = "/"; this.request = new MockHttpServletRequest("GET", requestUri); this.request.setServletPath(requestUri); this.springSecurityFilterChain.doFilter(this.request, this.response, this.filterChain); assertThat(this.response.getRedirectedUrl()).matches("http://localhost/custom-login"); }
@Test public void oauth2LoginWithOneClientConfiguredThenRedirectForAuthorization() throws Exception { loadConfig(OAuth2LoginConfig.class); String requestUri = "/"; this.request = new MockHttpServletRequest("GET", requestUri); this.request.setServletPath(requestUri); this.springSecurityFilterChain.doFilter(this.request, this.response, this.filterChain); assertThat(this.response.getRedirectedUrl()).matches("http://localhost/oauth2/authorization/google"); }
@Test public void oauth2LoginWithMultipleClientsConfiguredThenRedirectDefaultLoginPage() throws Exception { loadConfig(OAuth2LoginConfigMultipleClients.class); String requestUri = "/"; this.request = new MockHttpServletRequest("GET", requestUri); this.request.setServletPath(requestUri); this.springSecurityFilterChain.doFilter(this.request, this.response, this.filterChain); assertThat(this.response.getRedirectedUrl()).matches("http://localhost/login"); }
@Test public void readmeSampleWhenRequestLoginWithValidCredentialsThenRedirectToIndex() throws Exception { this.spring.register(SampleWebSecurityConfigurerAdapter.class).autowire(); this.request.setServletPath("/login"); this.request.setMethod("POST"); this.request.addParameter("username", "user"); this.request.addParameter("password", "password"); this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain); assertThat(this.response.getRedirectedUrl()).isEqualTo("/"); }
@Test public void multiHttpSampleWhenRequestLoginWithValidCredentialsThenRedirectToIndex() throws Exception { this.spring.register(SampleMultiHttpSecurityConfig.class).autowire(); this.request.setServletPath("/login"); this.request.setMethod("POST"); this.request.addParameter("username", "user"); this.request.addParameter("password", "password"); this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain); assertThat(this.response.getRedirectedUrl()).isEqualTo("/"); }
@Test public void helloWorldSampleWhenRequestLoginWithValidCredentialsThenRedirectToIndex() throws Exception { this.spring.register(HelloWorldWebSecurityConfigurerAdapter.class).autowire(); this.request.setServletPath("/login"); this.request.setMethod("POST"); this.request.addHeader("Accept", "text/html"); this.request.addParameter("username", "user"); this.request.addParameter("password", "password"); this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain); assertThat(this.response.getRedirectedUrl()).isEqualTo("/"); }
/** * Print the response. */ protected void printResponse(MockHttpServletResponse response) throws Exception { String body = (response.getCharacterEncoding() != null ? response.getContentAsString() : MISSING_CHARACTER_ENCODING); this.printer.printValue("Status", response.getStatus()); this.printer.printValue("Error message", response.getErrorMessage()); this.printer.printValue("Headers", getResponseHeaders(response)); this.printer.printValue("Content type", response.getContentType()); this.printer.printValue("Body", body); this.printer.printValue("Forwarded URL", response.getForwardedUrl()); this.printer.printValue("Redirected URL", response.getRedirectedUrl()); printCookies(response.getCookies()); }
@Test public void sendRedirect() throws IOException { String redirectUrl = "/redirect"; response.sendRedirect(redirectUrl); assertEquals(HttpServletResponse.SC_MOVED_TEMPORARILY, response.getStatus()); assertEquals(redirectUrl, response.getHeader("Location")); assertEquals(redirectUrl, response.getRedirectedUrl()); assertTrue(response.isCommitted()); }
@Test public void oauth2LoginWithOneClientConfiguredAndRequestFaviconNotAuthenticatedThenRedirectDefaultLoginPage() throws Exception { loadConfig(OAuth2LoginConfig.class); String requestUri = "/favicon.ico"; this.request = new MockHttpServletRequest("GET", requestUri); this.request.setServletPath(requestUri); this.request.addHeader(HttpHeaders.ACCEPT, new MediaType("image", "*").toString()); this.springSecurityFilterChain.doFilter(this.request, this.response, this.filterChain); assertThat(this.response.getRedirectedUrl()).matches("http://localhost/login"); }