Skip to content

Commit 5792a01

Browse files
committed
Add port if specified
1 parent 4e3a44b commit 5792a01

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/Rule/Host.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ public function __invoke(ServerRequestInterface $request, Route $route)
5555
return true;
5656
}
5757

58+
$hostWithPort = $request->getUri()->getPort() ? $request->getUri()->getHost() . ':' . $request->getUri()->getPort() : $request->getUri()->getHost();
5859
$match = preg_match(
5960
$this->buildRegex($route),
60-
$request->getUri()->getHost(),
61+
$hostWithPort,
6162
$matches
6263
);
6364

tests/Rule/HostTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,23 @@ public function testIsMatchOnDynamicHost()
4545
$this->assertIsMatch($request, $route);
4646
$this->assertEquals(['subdomain' => null, 'domain' => 'example'], $route->attributes);
4747
}
48+
49+
public function testIsMatchOnHostWithPort()
50+
{
51+
$proto = $this->newRoute("/foo/bar/baz")->host("127.0.0.1:8080");
52+
53+
// right host
54+
$route = clone $proto;
55+
$request = $this->newRequest("/foo/bar/baz", [
56+
"HTTP_HOST" => "127.0.0.1:8080",
57+
]);
58+
$this->assertIsMatch($request, $route);
59+
60+
// wrong host
61+
$route = clone $proto;
62+
$request = $this->newRequest("/foo/bar/baz", [
63+
"HTTP_HOST" => "127.0.0.1:9090",
64+
]);
65+
$this->assertIsNotMatch($request, $route);
66+
}
4867
}

0 commit comments

Comments
 (0)