|
| 1 | +/* |
| 2 | + * Copyright 2002-2020 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.web.servlet.mvc.method.annotation; |
| 18 | + |
| 19 | +import java.lang.annotation.ElementType; |
| 20 | +import java.lang.annotation.Retention; |
| 21 | +import java.lang.annotation.RetentionPolicy; |
| 22 | +import java.lang.annotation.Target; |
| 23 | +import java.lang.reflect.Method; |
| 24 | +import java.security.Principal; |
| 25 | + |
| 26 | +import javax.servlet.ServletRequest; |
| 27 | + |
| 28 | +import org.junit.jupiter.api.BeforeEach; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | + |
| 31 | +import org.springframework.core.MethodParameter; |
| 32 | +import org.springframework.web.context.request.ServletWebRequest; |
| 33 | +import org.springframework.web.method.support.ModelAndViewContainer; |
| 34 | +import org.springframework.web.testfixture.servlet.MockHttpServletRequest; |
| 35 | +import org.springframework.web.testfixture.servlet.MockHttpServletResponse; |
| 36 | + |
| 37 | +import static org.assertj.core.api.Assertions.assertThat; |
| 38 | + |
| 39 | +/** |
| 40 | + * Unit tests for {@link PrincipalMethodArgumentResolver}. |
| 41 | + * |
| 42 | + * @author Rossen Stoyanchev |
| 43 | + */ |
| 44 | +public class PrincipalMethodArgumentResolverTests { |
| 45 | + |
| 46 | + private PrincipalMethodArgumentResolver resolver; |
| 47 | + |
| 48 | + private ModelAndViewContainer mavContainer; |
| 49 | + |
| 50 | + private MockHttpServletRequest servletRequest; |
| 51 | + |
| 52 | + private ServletWebRequest webRequest; |
| 53 | + |
| 54 | + private Method method; |
| 55 | + |
| 56 | + |
| 57 | + @BeforeEach |
| 58 | + public void setup() throws Exception { |
| 59 | + resolver = new PrincipalMethodArgumentResolver(); |
| 60 | + mavContainer = new ModelAndViewContainer(); |
| 61 | + servletRequest = new MockHttpServletRequest("GET", ""); |
| 62 | + webRequest = new ServletWebRequest(servletRequest, new MockHttpServletResponse()); |
| 63 | + |
| 64 | + method = getClass().getMethod("supportedParams", ServletRequest.class, Principal.class); |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + @Test |
| 69 | + public void principal() throws Exception { |
| 70 | + Principal principal = () -> "Foo"; |
| 71 | + servletRequest.setUserPrincipal(principal); |
| 72 | + |
| 73 | + MethodParameter principalParameter = new MethodParameter(method, 1); |
| 74 | + assertThat(resolver.supportsParameter(principalParameter)).as("Principal not supported").isTrue(); |
| 75 | + |
| 76 | + Object result = resolver.resolveArgument(principalParameter, null, webRequest, null); |
| 77 | + assertThat(result).as("Invalid result").isSameAs(principal); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void principalAsNull() throws Exception { |
| 82 | + MethodParameter principalParameter = new MethodParameter(method, 1); |
| 83 | + assertThat(resolver.supportsParameter(principalParameter)).as("Principal not supported").isTrue(); |
| 84 | + |
| 85 | + Object result = resolver.resolveArgument(principalParameter, null, webRequest, null); |
| 86 | + assertThat(result).as("Invalid result").isNull(); |
| 87 | + } |
| 88 | + |
| 89 | + @Test // gh-25780 |
| 90 | + public void annotatedPrincipal() throws Exception { |
| 91 | + Principal principal = () -> "Foo"; |
| 92 | + servletRequest.setUserPrincipal(principal); |
| 93 | + Method principalMethod = getClass().getMethod("supportedParamsWithAnnotatedPrincipal", Principal.class); |
| 94 | + |
| 95 | + MethodParameter principalParameter = new MethodParameter(principalMethod, 0); |
| 96 | + assertThat(resolver.supportsParameter(principalParameter)).isTrue(); |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + @SuppressWarnings("unused") |
| 101 | + public void supportedParams(ServletRequest p0, Principal p1) {} |
| 102 | + |
| 103 | + @Target({ ElementType.PARAMETER }) |
| 104 | + @Retention(RetentionPolicy.RUNTIME) |
| 105 | + public @interface AuthenticationPrincipal {} |
| 106 | + |
| 107 | + @SuppressWarnings("unused") |
| 108 | + public void supportedParamsWithAnnotatedPrincipal(@AuthenticationPrincipal Principal p) {} |
| 109 | + |
| 110 | +} |
0 commit comments