Add static factory method to AntPathRequestMather and RegexRequestMatcher
Closes gh-11938
This commit is contained in:
committed by
Marcus Hert Da Coregio
parent
37fa49b32d
commit
4b6fed0667
+37
@@ -67,6 +67,43 @@ public final class AntPathRequestMatcher implements RequestMatcher, RequestVaria
|
||||
|
||||
private final UrlPathHelper urlPathHelper;
|
||||
|
||||
/**
|
||||
* Creates a matcher with the specific pattern which will match all HTTP methods in a
|
||||
* case-sensitive manner.
|
||||
* @param pattern the ant pattern to use for matching
|
||||
* @since 5.8
|
||||
*/
|
||||
public static AntPathRequestMatcher antMatcher(String pattern) {
|
||||
Assert.hasText(pattern, "pattern cannot be empty");
|
||||
return new AntPathRequestMatcher(pattern);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a matcher that will match all request with the supplied HTTP method in a
|
||||
* case-sensitive manner.
|
||||
* @param method the HTTP method. The {@code matches} method will return false if the
|
||||
* incoming request doesn't have the same method.
|
||||
* @since 5.8
|
||||
*/
|
||||
public static AntPathRequestMatcher antMatcher(HttpMethod method) {
|
||||
Assert.notNull(method, "method cannot be null");
|
||||
return new AntPathRequestMatcher(MATCH_ALL, method.name());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a matcher with the supplied pattern and HTTP method in a case-sensitive
|
||||
* manner.
|
||||
* @param method the HTTP method. The {@code matches} method will return false if the
|
||||
* incoming request doesn't have the same method.
|
||||
* @param pattern the ant pattern to use for matching
|
||||
* @since 5.8
|
||||
*/
|
||||
public static AntPathRequestMatcher antMatcher(HttpMethod method, String pattern) {
|
||||
Assert.notNull(method, "method cannot be null");
|
||||
Assert.hasText(pattern, "pattern cannot be empty");
|
||||
return new AntPathRequestMatcher(pattern, method.name());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a matcher with the specific pattern which will match all HTTP methods in a
|
||||
* case sensitive manner.
|
||||
|
||||
+33
@@ -25,6 +25,7 @@ import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.core.log.LogMessage;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
@@ -53,6 +54,38 @@ public final class RegexRequestMatcher implements RequestMatcher {
|
||||
|
||||
private final HttpMethod httpMethod;
|
||||
|
||||
/**
|
||||
* Creates a case-sensitive {@code Pattern} instance to match against the request.
|
||||
* @param pattern the regular expression to compile into a pattern.
|
||||
* @since 5.8
|
||||
*/
|
||||
public static RegexRequestMatcher regexMatcher(String pattern) {
|
||||
Assert.hasText(pattern, "pattern cannot be empty");
|
||||
return new RegexRequestMatcher(pattern, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an instance that matches to all requests with the same {@link HttpMethod}.
|
||||
* @param method the HTTP method to match. Must not be null.
|
||||
* @since 5.8
|
||||
*/
|
||||
public static RegexRequestMatcher regexMatcher(HttpMethod method) {
|
||||
Assert.notNull(method, "method cannot be null");
|
||||
return new RegexRequestMatcher(".*", method.name());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a case-sensitive {@code Pattern} instance to match against the request.
|
||||
* @param method the HTTP method to match. May be null to match all methods.
|
||||
* @param pattern the regular expression to compile into a pattern.
|
||||
* @since 5.8
|
||||
*/
|
||||
public static RegexRequestMatcher regexMatcher(HttpMethod method, String pattern) {
|
||||
Assert.notNull(method, "method cannot be null");
|
||||
Assert.hasText(pattern, "pattern cannot be empty");
|
||||
return new RegexRequestMatcher(pattern, method.name());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a case-sensitive {@code Pattern} instance to match against the request.
|
||||
* @param pattern the regular expression to compile into a pattern.
|
||||
|
||||
+46
@@ -23,11 +23,15 @@ import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.web.util.UrlPathHelper;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.springframework.security.web.util.matcher.AntPathRequestMatcher.antMatcher;
|
||||
|
||||
/**
|
||||
* @author Luke Taylor
|
||||
@@ -205,6 +209,48 @@ public class AntPathRequestMatcherTests {
|
||||
assertThat(matcher.matcher(request).isMatch()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void staticAntMatcherWhenPatternProvidedThenPattern() {
|
||||
AntPathRequestMatcher matcher = antMatcher("/path");
|
||||
assertThat(matcher.getPattern()).isEqualTo("/path");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void staticAntMatcherWhenMethodProvidedThenMatchAll() {
|
||||
AntPathRequestMatcher matcher = antMatcher(HttpMethod.GET);
|
||||
assertThat(ReflectionTestUtils.getField(matcher, "httpMethod")).isEqualTo(HttpMethod.GET);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void staticAntMatcherWhenMethodAndPatternProvidedThenMatchAll() {
|
||||
AntPathRequestMatcher matcher = antMatcher(HttpMethod.POST, "/path");
|
||||
assertThat(matcher.getPattern()).isEqualTo("/path");
|
||||
assertThat(ReflectionTestUtils.getField(matcher, "httpMethod")).isEqualTo(HttpMethod.POST);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void staticAntMatcherWhenMethodNullThenException() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> antMatcher((HttpMethod) null))
|
||||
.withMessage("method cannot be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void staticAntMatcherWhenPatternNullThenException() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> antMatcher((String) null))
|
||||
.withMessage("pattern cannot be empty");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void forMethodWhenMethodThenMatches() {
|
||||
AntPathRequestMatcher matcher = antMatcher(HttpMethod.POST);
|
||||
MockHttpServletRequest request = createRequest("/path");
|
||||
assertThat(matcher.matches(request)).isTrue();
|
||||
request.setServletPath("/another-path/second");
|
||||
assertThat(matcher.matches(request)).isTrue();
|
||||
request.setMethod("GET");
|
||||
assertThat(matcher.matches(request)).isFalse();
|
||||
}
|
||||
|
||||
private HttpServletRequest createRequestWithNullMethod(String path) {
|
||||
given(this.request.getServletPath()).willReturn(path);
|
||||
return this.request;
|
||||
|
||||
+43
@@ -23,10 +23,13 @@ import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.springframework.security.web.util.matcher.RegexRequestMatcher.regexMatcher;
|
||||
|
||||
/**
|
||||
* @author Luke Taylor
|
||||
@@ -123,6 +126,46 @@ public class RegexRequestMatcherTests {
|
||||
assertThat(matcher.toString()).isEqualTo("Regex [pattern='/blah', GET]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchesWhenRequestUriMatchesThenMatchesTrue() {
|
||||
RegexRequestMatcher matcher = regexMatcher(".*");
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/something/anything");
|
||||
assertThat(matcher.matches(request)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchesWhenRequestUriDontMatchThenMatchesFalse() {
|
||||
RegexRequestMatcher matcher = regexMatcher(".*\\?param=value");
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/something/anything");
|
||||
assertThat(matcher.matches(request)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchesWhenRequestMethodMatchesThenMatchesTrue() {
|
||||
RegexRequestMatcher matcher = regexMatcher(HttpMethod.GET);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/something/anything");
|
||||
assertThat(matcher.matches(request)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchesWhenRequestMethodDontMatchThenMatchesFalse() {
|
||||
RegexRequestMatcher matcher = regexMatcher(HttpMethod.POST);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/something/anything");
|
||||
assertThat(matcher.matches(request)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void staticRegexMatcherWhenNoPatternThenException() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> regexMatcher((String) null))
|
||||
.withMessage("pattern cannot be empty");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void staticRegexMatcherNoMethodThenException() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> regexMatcher((HttpMethod) null))
|
||||
.withMessage("method cannot be null");
|
||||
}
|
||||
|
||||
private HttpServletRequest createRequestWithNullMethod(String path) {
|
||||
given(this.request.getQueryString()).willReturn("doesntMatter");
|
||||
given(this.request.getServletPath()).willReturn(path);
|
||||
|
||||
Reference in New Issue
Block a user