Skip to content

Commit e4aee91

Browse files
author
awu
committed
fix: correct port handling in WaitForHttp and enhance test coverage
1 parent f5718c9 commit e4aee91

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

src/Wait/WaitForHttp.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public function wait(StartedTestContainer $container): void
104104
$this->resolvePort($container);
105105
$containerAddress = $container->getHost();
106106

107-
$url = sprintf('%s://%s:%d%s', $this->protocol, $containerAddress, $container->getMappedPort((int) $this->port), $this->path);
107+
$url = sprintf('%s://%s:%d%s', $this->protocol, $containerAddress, $this->port, $this->path);
108108
$responseCode = $this->makeHttpRequest($url);
109109

110110
if ($responseCode === $this->expectedStatusCode) {
@@ -147,8 +147,8 @@ private function makeHttpRequest(string $url): int
147147

148148
private function resolvePort(StartedTestContainer $container): void
149149
{
150-
if ($this->port === null) {
151-
$this->port = $container->getFirstMappedPort();
152-
}
150+
$this->port = $this->port === null
151+
? $container->getFirstMappedPort()
152+
: $container->getMappedPort($this->port);
153153
}
154154
}

tests/Integration/GenericContainerTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public function testShouldReturnFirstMappedPortWithWaitForHttp(): void
129129
{
130130
$container = (new GenericContainer('nginx'))
131131
->withExposedPorts(80)
132-
->withWait((new WaitForHttp(80))->withPath('/'))
132+
->withWait((new WaitForHttp())->withMethod('GET')->withPath('/'))
133133
->start();
134134
$firstMappedPort = $container->getFirstMappedPort();
135135

@@ -138,6 +138,19 @@ public function testShouldReturnFirstMappedPortWithWaitForHttp(): void
138138
$container->stop();
139139
}
140140

141+
public function testShouldReturnMappedPortWithWaitForHttp(): void
142+
{
143+
$container = (new GenericContainer('nginx'))
144+
->withExposedPorts(80)
145+
->withWait((new WaitForHttp(80))->withMethod('GET')->withPath('/'))
146+
->start();
147+
$mappedPort = $container->getMappedPort(80);
148+
149+
self::assertSame($mappedPort, $container->getFirstMappedPort());
150+
151+
$container->stop();
152+
}
153+
141154
public function testShouldSetLabels(): void
142155
{
143156
$labels = [

0 commit comments

Comments
 (0)