Skip to content

Commit

Permalink
Added getFirstMappedPort method (#377)
Browse files Browse the repository at this point in the history
* Added no-args `getMappedPort` overloaded method (will use first exposed port)
  • Loading branch information
bsideup authored and rnorth committed Jun 27, 2017
1 parent 3e513be commit ed63472
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file.

### Changed
- Added support for Docker networks (#372)
- Added `getFirstMappedPort` method

## [1.3.1] - 2017-06-22
### Fixed
Expand Down
14 changes: 14 additions & 0 deletions core/src/main/java/org/testcontainers/containers/Container.java
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,20 @@ default SELF withClasspathResourceMapping(final String resourcePath, final Strin
*/
Boolean isRunning();

/**
* Get the actual mapped port for a first port exposed by the container.
*
* @return the port that the exposed port is mapped to
* @throws IllegalStateException if there are no exposed ports
*/
default Integer getFirstMappedPort() {
return getExposedPorts()
.stream()
.findFirst()
.map(this::getMappedPort)
.orElseThrow(() -> new IllegalStateException("Container doesn't expose any ports"));
}

/**
* Get the actual mapped port for a given port exposed by the container.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ private BufferedReader getReaderForContainerPort80(GenericContainer container) {
return Unreliables.retryUntilSuccess(10, TimeUnit.SECONDS, () -> {
Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);

Socket socket = new Socket(container.getContainerIpAddress(), container.getMappedPort(80));
Socket socket = new Socket(container.getContainerIpAddress(), container.getFirstMappedPort());
return new BufferedReader(new InputStreamReader(socket.getInputStream()));
});
}
Expand Down

0 comments on commit ed63472

Please sign in to comment.