Skip to content

feat: add getMappedPort(int, InternetProtocol) overload to ContainerState#11780

Open
suryateja-g13 wants to merge 1 commit into
testcontainers:mainfrom
suryateja-g13:feat/554-getMappedPort-with-protocol
Open

feat: add getMappedPort(int, InternetProtocol) overload to ContainerState#11780
suryateja-g13 wants to merge 1 commit into
testcontainers:mainfrom
suryateja-g13:feat/554-getMappedPort-with-protocol

Conversation

@suryateja-g13
Copy link
Copy Markdown

Summary

Closes #554

The existing getMappedPort(int) always performs a TCP lookup. There is no typed API to retrieve the mapped port for a UDP-exposed container port — callers must walk the full getContainerInfo() chain manually, as noted in the original issue.

Changes

ContainerState

  • Added getMappedPort(int originalPort, InternetProtocol protocol) — looks up the mapped host port for any InternetProtocol (TCP or UDP).
  • Reimplemented getMappedPort(int) to delegate to the new overload with InternetProtocol.TCP, so both paths share the same logic.
  • Error message now includes the protocol (e.g. "Requested port (53/udp) is not mapped").

Tests

Added three unit tests to ContainerStateTest (no Docker required, Mockito-only):

  • getMappedPortWithTcpProtocolReturnsMappedPort — verifies TCP lookup returns the correct mapped port via both the no-arg and protocol overloads.
  • getMappedPortWithUdpProtocolReturnsMappedPort — verifies UDP port lookup returns the correct mapped port.
  • getMappedPortThrowsWhenPortNotMapped — verifies IllegalArgumentException is thrown for an unmapped UDP port, with the port/protocol in the message.

All 9 tests in ContainerStateTest pass.

…tate

The existing getMappedPort(int) always looks up TCP bindings. UDP-exposed
ports cannot be queried without manually traversing getContainerInfo().
Add an overload that accepts an InternetProtocol argument so callers can
look up both TCP and UDP mapped ports through the same typed API.

The no-arg overload is reimplemented to delegate to the new method with
InternetProtocol.TCP so the two code paths share the same logic.

Closes testcontainers#554

Signed-off-by: Gorre Surya <suryateja.g13@gmail.com>
@suryateja-g13 suryateja-g13 requested a review from a team as a code owner May 26, 2026 02:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add getMappedPort(Integer originalPort, InternetProtocol protocol)

1 participant