|
| 1 | +/* |
| 2 | + * Copyright 2002-2025 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.servlet.util.matcher; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import org.springframework.http.HttpMethod; |
| 22 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 23 | +import org.springframework.security.web.util.matcher.RequestMatcher; |
| 24 | +import org.springframework.security.web.util.matcher.RequestMatchers; |
| 25 | +import org.springframework.web.util.ServletRequestPathUtils; |
| 26 | + |
| 27 | +import static org.assertj.core.api.Assertions.assertThat; |
| 28 | + |
| 29 | +/** |
| 30 | + * Tests for {@link PathPatternRequestMatcher} |
| 31 | + */ |
| 32 | +public class PathPatternRequestMatcherTests { |
| 33 | + |
| 34 | + @Test |
| 35 | + void matcherWhenPatternMatchesRequestThenMatchResult() { |
| 36 | + RequestMatcher matcher = PathPatternRequestMatcher.pathPattern("/uri"); |
| 37 | + assertThat(matcher.matches(request("/uri"))).isTrue(); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + void matcherWhenPatternContainsPlaceholdersThenMatchResult() { |
| 42 | + RequestMatcher matcher = PathPatternRequestMatcher.pathPattern("/uri/{username}"); |
| 43 | + assertThat(matcher.matcher(request("/uri/bob")).getVariables()).containsEntry("username", "bob"); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void matcherWhenOnlyPathInfoMatchesThenMatches() { |
| 48 | + RequestMatcher matcher = PathPatternRequestMatcher.pathPattern("/uri"); |
| 49 | + assertThat(matcher.matches(request("GET", "/mvc/uri", "/mvc"))).isTrue(); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void matcherWhenUriContainsServletPathThenNoMatch() { |
| 54 | + RequestMatcher matcher = PathPatternRequestMatcher.pathPattern("/mvc/uri"); |
| 55 | + assertThat(matcher.matches(request("GET", "/mvc/uri", "/mvc"))).isFalse(); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void matcherWhenSameMethodThenMatchResult() { |
| 60 | + RequestMatcher matcher = RequestMatchers.request().methods(HttpMethod.GET).uris("/uri").matcher(); |
| 61 | + assertThat(matcher.matches(request("/uri"))).isTrue(); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void matcherWhenDifferentPathThenNoMatch() { |
| 66 | + RequestMatcher matcher = RequestMatchers.request().methods(HttpMethod.GET).uris("/uri").matcher(); |
| 67 | + assertThat(matcher.matches(request("GET", "/urj", ""))).isFalse(); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void matcherWhenDifferentMethodThenNoMatch() { |
| 72 | + RequestMatcher matcher = RequestMatchers.request().methods(HttpMethod.GET).uris("/uri").matcher(); |
| 73 | + assertThat(matcher.matches(request("POST", "/mvc/uri", "/mvc"))).isFalse(); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void matcherWhenNoMethodThenMatches() { |
| 78 | + RequestMatcher matcher = PathPatternRequestMatcher.pathPattern("/uri"); |
| 79 | + assertThat(matcher.matches(request("POST", "/uri", ""))).isTrue(); |
| 80 | + assertThat(matcher.matches(request("GET", "/uri", ""))).isTrue(); |
| 81 | + } |
| 82 | + |
| 83 | + MockHttpServletRequest request(String uri) { |
| 84 | + MockHttpServletRequest request = new MockHttpServletRequest("GET", uri); |
| 85 | + ServletRequestPathUtils.parseAndCache(request); |
| 86 | + return request; |
| 87 | + } |
| 88 | + |
| 89 | + MockHttpServletRequest request(String method, String uri, String servletPath) { |
| 90 | + MockHttpServletRequest request = new MockHttpServletRequest(method, uri); |
| 91 | + request.setServletPath(servletPath); |
| 92 | + ServletRequestPathUtils.parseAndCache(request); |
| 93 | + return request; |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments