|
| 1 | +/* |
| 2 | + * Copyright 2002-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.web.server.ui; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | + |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.BeforeEach; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import org.springframework.http.HttpStatus; |
| 27 | +import org.springframework.http.MediaType; |
| 28 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 29 | +import org.springframework.mock.web.MockHttpServletResponse; |
| 30 | +import org.springframework.mock.web.MockServletContext; |
| 31 | +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; |
| 32 | + |
| 33 | +public class FormRedirectStrategyTest { |
| 34 | + |
| 35 | + private FormRedirectStrategy formRedirectStrategy; |
| 36 | + |
| 37 | + private MockHttpServletRequest request; |
| 38 | + |
| 39 | + private MockHttpServletResponse response; |
| 40 | + |
| 41 | + @BeforeEach |
| 42 | + public void beforeEach() { |
| 43 | + formRedirectStrategy = new FormRedirectStrategy(); |
| 44 | + final MockServletContext mockServletContext = new MockServletContext(); |
| 45 | + mockServletContext.setContextPath("/contextPath"); |
| 46 | + // the request URL doesn't matter |
| 47 | + request = MockMvcRequestBuilders.get("http://localhost").buildRequest(mockServletContext); |
| 48 | + response = new MockHttpServletResponse(); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void absoluteUrlNoParametersRedirect() throws IOException { |
| 53 | + formRedirectStrategy.sendRedirect(request, response, "http://example.com"); |
| 54 | + assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); |
| 55 | + assertThat(response.getContentType()).isEqualTo(MediaType.TEXT_HTML_VALUE); |
| 56 | + assertThat(response.getContentAsString()).contains("action=\"http://example.com\""); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void rootRelativeUrlNoParametersRedirect() throws IOException { |
| 61 | + formRedirectStrategy.sendRedirect(request, response, "/test"); |
| 62 | + assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); |
| 63 | + assertThat(response.getContentType()).isEqualTo(MediaType.TEXT_HTML_VALUE); |
| 64 | + assertThat(response.getContentAsString()).contains("action=\"/test\""); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void relativeUrlNoParametersRedirect() throws IOException { |
| 69 | + formRedirectStrategy.sendRedirect(request, response, "test"); |
| 70 | + assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); |
| 71 | + assertThat(response.getContentType()).isEqualTo(MediaType.TEXT_HTML_VALUE); |
| 72 | + assertThat(response.getContentAsString()).contains("action=\"test\""); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void absoluteUrlWithFragmentRedirect() throws IOException { |
| 77 | + formRedirectStrategy.sendRedirect(request, response, "http://example.com/path#fragment"); |
| 78 | + assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); |
| 79 | + assertThat(response.getContentType()).isEqualTo(MediaType.TEXT_HTML_VALUE); |
| 80 | + assertThat(response.getContentAsString()).contains("action=\"http://example.com/path#fragment\""); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void absoluteUrlWithQueryParamsRedirect() throws IOException { |
| 85 | + formRedirectStrategy.sendRedirect(request, response, "http://example.com/path?param1=one¶m2=two#fragment"); |
| 86 | + assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); |
| 87 | + assertThat(response.getContentType()).isEqualTo(MediaType.TEXT_HTML_VALUE); |
| 88 | + assertThat(response.getContentAsString()).contains("action=\"http://example.com/path#fragment\""); |
| 89 | + assertThat(response.getContentAsString()).contains("<input name=\"param1\" type=\"hidden\" value=\"one\" />"); |
| 90 | + assertThat(response.getContentAsString()).contains("<input name=\"param2\" type=\"hidden\" value=\"two\" />"); |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments