Add equals and hashCode to HttpMethodRequestMatcher

Closes gh-18911

Signed-off-by: Tran Ngoc Nhan <ngocnhan.tran1996@gmail.com>
This commit is contained in:
Tran Ngoc Nhan 2026-03-17 09:42:38 +07:00 committed by Josh Cummings
parent 9fed1ac8c3
commit 62f33d3fcf
2 changed files with 24 additions and 0 deletions

View File

@ -305,6 +305,19 @@ public final class PathPatternRequestMatcher implements RequestMatcher {
return this.method.name().equals(request.getMethod());
}
@Override
public boolean equals(Object o) {
if (!(o instanceof HttpMethodRequestMatcher that)) {
return false;
}
return Objects.equals(this.method, that.method);
}
@Override
public int hashCode() {
return Objects.hash(this.method);
}
@Override
public String toString() {
return "HttpMethod [" + this.method + "]";

View File

@ -145,6 +145,17 @@ public class PathPatternRequestMatcherTests {
assertThat(matcher.matches(mock)).isTrue();
}
// gh-18911
@Test
void testEqualsWithSameAndDifferentHttpMethod() {
PathPatternRequestMatcher.Builder builder = PathPatternRequestMatcher.withDefaults();
PathPatternRequestMatcher matcher1 = builder.matcher(HttpMethod.GET, "/foo");
PathPatternRequestMatcher matcher2 = builder.matcher(HttpMethod.GET, "/foo");
PathPatternRequestMatcher matcher3 = builder.matcher(HttpMethod.POST, "/foo");
assertThat(matcher1).isEqualTo(matcher2);
assertThat(matcher1).isNotEqualTo(matcher3);
}
MockHttpServletRequest request(String uri) {
MockHttpServletRequest request = new MockHttpServletRequest("GET", uri);
ServletRequestPathUtils.parseAndCache(request);