|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2022 the original author or authors. |
| 2 | + * Copyright 2002-2023 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
@@ -141,4 +141,64 @@ public void containsContextWhenFirstDelegatesReturnTrueThenReturnsTrue() {
|
141 | 141 | verifyNoInteractions(delegates.get(2));
|
142 | 142 | }
|
143 | 143 |
|
| 144 | + // gh-12314 |
| 145 | + @Test |
| 146 | + public void loadContextWhenSecondDelegateReturnsThenContextFromSecondDelegate() { |
| 147 | + SecurityContextRepository delegate1 = mock(SecurityContextRepository.class); |
| 148 | + SecurityContextRepository delegate2 = mock(SecurityContextRepository.class); |
| 149 | + HttpRequestResponseHolder holder = new HttpRequestResponseHolder(this.request, this.response); |
| 150 | + SecurityContext securityContext1 = mock(SecurityContext.class); |
| 151 | + SecurityContext securityContext2 = mock(SecurityContext.class); |
| 152 | + |
| 153 | + given(delegate1.loadContext(holder)).willReturn(securityContext1); |
| 154 | + given(delegate1.containsContext(holder.getRequest())).willReturn(false); |
| 155 | + given(delegate2.loadContext(holder)).willReturn(securityContext2); |
| 156 | + given(delegate2.containsContext(holder.getRequest())).willReturn(true); |
| 157 | + |
| 158 | + DelegatingSecurityContextRepository repository = new DelegatingSecurityContextRepository(delegate1, delegate2); |
| 159 | + SecurityContext returnedSecurityContext = repository.loadContext(holder); |
| 160 | + |
| 161 | + assertThat(returnedSecurityContext).isSameAs(securityContext2); |
| 162 | + } |
| 163 | + |
| 164 | + // gh-12314 |
| 165 | + @Test |
| 166 | + public void loadContextWhenBothDelegateReturnsThenContextFromSecondDelegate() { |
| 167 | + SecurityContextRepository delegate1 = mock(SecurityContextRepository.class); |
| 168 | + SecurityContextRepository delegate2 = mock(SecurityContextRepository.class); |
| 169 | + HttpRequestResponseHolder holder = new HttpRequestResponseHolder(this.request, this.response); |
| 170 | + SecurityContext securityContext1 = mock(SecurityContext.class); |
| 171 | + SecurityContext securityContext2 = mock(SecurityContext.class); |
| 172 | + |
| 173 | + given(delegate1.loadContext(holder)).willReturn(securityContext1); |
| 174 | + given(delegate1.containsContext(holder.getRequest())).willReturn(true); |
| 175 | + given(delegate2.loadContext(holder)).willReturn(securityContext2); |
| 176 | + given(delegate2.containsContext(holder.getRequest())).willReturn(true); |
| 177 | + |
| 178 | + DelegatingSecurityContextRepository repository = new DelegatingSecurityContextRepository(delegate1, delegate2); |
| 179 | + SecurityContext returnedSecurityContext = repository.loadContext(holder); |
| 180 | + |
| 181 | + assertThat(returnedSecurityContext).isSameAs(securityContext2); |
| 182 | + } |
| 183 | + |
| 184 | + // gh-12314 |
| 185 | + @Test |
| 186 | + public void loadContextWhenFirstDelegateReturnsThenContextFromFirstDelegate() { |
| 187 | + SecurityContextRepository delegate1 = mock(SecurityContextRepository.class); |
| 188 | + SecurityContextRepository delegate2 = mock(SecurityContextRepository.class); |
| 189 | + HttpRequestResponseHolder holder = new HttpRequestResponseHolder(this.request, this.response); |
| 190 | + SecurityContext securityContext1 = mock(SecurityContext.class); |
| 191 | + SecurityContext securityContext2 = mock(SecurityContext.class); |
| 192 | + |
| 193 | + given(delegate1.loadContext(holder)).willReturn(securityContext1); |
| 194 | + given(delegate1.containsContext(holder.getRequest())).willReturn(true); |
| 195 | + given(delegate2.loadContext(holder)).willReturn(securityContext2); |
| 196 | + given(delegate2.containsContext(holder.getRequest())).willReturn(false); |
| 197 | + |
| 198 | + DelegatingSecurityContextRepository repository = new DelegatingSecurityContextRepository(delegate1, delegate2); |
| 199 | + SecurityContext returnedSecurityContext = repository.loadContext(holder); |
| 200 | + |
| 201 | + assertThat(returnedSecurityContext).isSameAs(securityContext1); |
| 202 | + } |
| 203 | + |
144 | 204 | }
|
0 commit comments