|
| 1 | +/* |
| 2 | + * Copyright 2002-2019 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 | + * http://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.authentication.logout; |
| 18 | + |
| 19 | +import org.junit.Before; |
| 20 | +import org.junit.Rule; |
| 21 | +import org.junit.Test; |
| 22 | +import org.junit.rules.ExpectedException; |
| 23 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 24 | +import org.springframework.mock.web.MockHttpServletResponse; |
| 25 | +import org.springframework.security.core.Authentication; |
| 26 | +import org.springframework.security.web.header.writers.ClearSiteDataHeaderWriter; |
| 27 | + |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; |
| 29 | +import static org.mockito.Mockito.mock; |
| 30 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; |
| 31 | + |
| 32 | +/** |
| 33 | + * |
| 34 | + * @author Rafiullah Hamedy |
| 35 | + * |
| 36 | + * @see {@link HeaderWriterLogoutHandler} |
| 37 | + */ |
| 38 | +public class HeaderWriterLogoutHandlerTests { |
| 39 | + private static final String HEADER_NAME = "Clear-Site-Data"; |
| 40 | + |
| 41 | + private MockHttpServletResponse response; |
| 42 | + private MockHttpServletRequest request; |
| 43 | + |
| 44 | + @Rule |
| 45 | + public ExpectedException thrown = ExpectedException.none(); |
| 46 | + |
| 47 | + @Before |
| 48 | + public void setup() { |
| 49 | + this.response = new MockHttpServletResponse(); |
| 50 | + this.request = new MockHttpServletRequest(); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void createInstanceWhenHeaderWriterIsNullThenThrowsException() { |
| 55 | + this.thrown.expect(IllegalArgumentException.class); |
| 56 | + this.thrown.expectMessage("headerWriter cannot be null."); |
| 57 | + |
| 58 | + new HeaderWriterLogoutHandler(null); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void createInstanceWhenSourceIsNullThenThrowsException() { |
| 63 | + this.thrown.expect(IllegalArgumentException.class); |
| 64 | + this.thrown.expectMessage("Sources cannot be empty or null."); |
| 65 | + |
| 66 | + new HeaderWriterLogoutHandler(new ClearSiteDataHeaderWriter()); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void logoutWhenRequestIsNotSecureThenHeaderIsNotPresent() { |
| 71 | + HeaderWriterLogoutHandler handler = new HeaderWriterLogoutHandler( |
| 72 | + new ClearSiteDataHeaderWriter("cache")); |
| 73 | + |
| 74 | + handler.logout(request, response, mock(Authentication.class)); |
| 75 | + |
| 76 | + assertThat(header().doesNotExist(HEADER_NAME)); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void logoutWhenRequestIsSecureThenHeaderIsPresentMatchesWildCardSource() { |
| 81 | + HeaderWriterLogoutHandler handler = new HeaderWriterLogoutHandler( |
| 82 | + new ClearSiteDataHeaderWriter("*")); |
| 83 | + |
| 84 | + this.request.setSecure(true); |
| 85 | + |
| 86 | + handler.logout(request, response, mock(Authentication.class)); |
| 87 | + |
| 88 | + assertThat(header().stringValues(HEADER_NAME, "\"*\"")); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void logoutWhenRequestIsSecureThenHeaderValueMatchesSource() { |
| 93 | + HeaderWriterLogoutHandler handler = new HeaderWriterLogoutHandler( |
| 94 | + new ClearSiteDataHeaderWriter("cache", "cookies", "storage", |
| 95 | + "executionContexts")); |
| 96 | + |
| 97 | + this.request.setSecure(true); |
| 98 | + |
| 99 | + handler.logout(request, response, mock(Authentication.class)); |
| 100 | + |
| 101 | + assertThat(header().stringValues(HEADER_NAME, "\"cache\", \"cookies\", \"storage\", " |
| 102 | + + "\"executionContexts\"")); |
| 103 | + } |
| 104 | +} |
0 commit comments