|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 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.context; |
| 18 | + |
| 19 | +import java.util.function.Supplier; |
| 20 | + |
| 21 | +import javax.servlet.http.HttpServletRequest; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import org.springframework.security.core.context.SecurityContext; |
| 26 | +import org.springframework.security.core.context.SecurityContextImpl; |
| 27 | + |
| 28 | +import static org.mockito.ArgumentMatchers.any; |
| 29 | +import static org.mockito.BDDMockito.given; |
| 30 | +import static org.mockito.Mockito.mock; |
| 31 | +import static org.mockito.Mockito.spy; |
| 32 | +import static org.mockito.Mockito.verify; |
| 33 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 34 | + |
| 35 | +/** |
| 36 | + * @author Rob Winch |
| 37 | + */ |
| 38 | +class SecurityContextRepositoryTests { |
| 39 | + |
| 40 | + SecurityContextRepository repository = spy(SecurityContextRepository.class); |
| 41 | + |
| 42 | + @Test |
| 43 | + void loadContextHttpRequestResponseHolderWhenInvokeSupplierTwiceThenOnlyInvokesLoadContextOnce() { |
| 44 | + given(this.repository.loadContext(any(HttpRequestResponseHolder.class))).willReturn(new SecurityContextImpl()); |
| 45 | + Supplier<SecurityContext> deferredContext = this.repository.loadContext(mock(HttpServletRequest.class)); |
| 46 | + verify(this.repository).loadContext(any(HttpServletRequest.class)); |
| 47 | + deferredContext.get(); |
| 48 | + verify(this.repository).loadContext(any(HttpRequestResponseHolder.class)); |
| 49 | + deferredContext.get(); |
| 50 | + verifyNoMoreInteractions(this.repository); |
| 51 | + } |
| 52 | + |
| 53 | +} |
0 commit comments