Skip to content

Add meaningful toString() to IpAddressMatcher.java #16779

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,25 @@ private InetAddress parseAddress(String address) {
}
}

/**
* Returns the IP address this Matcher is matching for without mask bits.
* @return a string representation of the IP address
*/
public String getHostAddress() {
return this.requiredAddress.getHostAddress();
}

/**
* Returns the mask bits of the IP address this Matcher is matching for
* @return a string representation of the mask bits
*/
public String getMaskBits() {
return String.valueOf(this.nMaskBits);
}

@Override
public String toString() {
return "IpAddressMatcher[" + getHostAddress() + "/" + getMaskBits() + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@

import org.springframework.mock.web.MockHttpServletRequest;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.*;

/**
* @author Luke Taylor
Expand Down Expand Up @@ -153,4 +151,32 @@ public void constructorWhenRequiredAddressIsEmptyThenThrowsIllegalArgumentExcept
.withMessage("ipAddress cannot be empty");
}

// gh-16693
@Test
public void getHostAddressReturnsCorrectIPv4String() {
IpAddressMatcher matcher = new IpAddressMatcher("192.168.1.0/24");
assertThat(matcher.getHostAddress()).isEqualTo("192.168.1.0");
}

// gh-16693
@Test
public void getMaskBitsIp4ReturnsCorrectString() {
IpAddressMatcher matcher = new IpAddressMatcher("192.168.1.0/24");
assertThat(matcher.getMaskBits()).isEqualTo("24");
}

// gh-16693
@Test
public void getHostAddressReturnsCorrectIPv6String() {
IpAddressMatcher matcher = new IpAddressMatcher("fe80::21f:5bff:fe33:bd68");
assertThat(matcher.getHostAddress()).isEqualTo("fe80:0:0:0:21f:5bff:fe33:bd68");
}

// gh-16693
@Test
public void getMaskBitsIp6ReturnsCorrectString() {
IpAddressMatcher matcher = new IpAddressMatcher("2001:DB8::/48");
assertThat(matcher.getMaskBits()).isEqualTo("48");
}

}