Add toString() to IpAddressMatcher.java

Closes gh-16795

Signed-off-by: wtigerhyunsu <jack951@naver.com>
This commit is contained in:
wtigerhyunsu 2025-03-25 11:05:58 +09:00 committed by Josh Cummings
parent 40b84d3e44
commit bdbf6a2be3
2 changed files with 21 additions and 0 deletions

View File

@ -130,4 +130,11 @@ public final class IpAddressMatcher implements RequestMatcher {
}
}
@Override
public String toString() {
String hostAddress = this.requiredAddress.getHostAddress();
return (this.nMaskBits < 0) ? "IpAddress [" + hostAddress + "]"
: "IpAddress [" + hostAddress + "/" + this.nMaskBits + "]";
}
}

View File

@ -153,4 +153,18 @@ public class IpAddressMatcherTests {
.withMessage("ipAddress cannot be empty");
}
// gh-16795
@Test
public void toStringWhenCidrIsProvidedThenReturnsIpAddressWithCidr() {
IpAddressMatcher matcher = new IpAddressMatcher("192.168.1.0/24");
assertThat(matcher.toString()).hasToString("IpAddress [192.168.1.0/24]");
}
// gh-16795
@Test
public void toStringWhenOnlyIpIsProvidedThenReturnsIpAddressOnly() {
IpAddressMatcher matcher = new IpAddressMatcher("127.0.0.1");
assertThat(matcher.toString()).hasToString("IpAddress [127.0.0.1]");
}
}