Merge branch '5.8.x'

Closes gh-11978
This commit is contained in:
Marcus Da Coregio
2022-10-10 09:24:50 -03:00
6 changed files with 292 additions and 0 deletions

View File

@@ -22,11 +22,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
@@ -204,6 +208,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;

View File

@@ -22,10 +22,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
@@ -122,6 +125,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);