From 559c30d44811a1f048c91c7ea297c0bfa0433368 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ana=C3=AFs=20Babel?= Date: Mon, 22 Apr 2024 13:22:45 +0200 Subject: [PATCH] Allow underscore character in Uri host --- src/Message/Uri.php | 4 ++-- tests/Message/UriTest.php | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Message/Uri.php b/src/Message/Uri.php index 84fc38d8..661f90c4 100644 --- a/src/Message/Uri.php +++ b/src/Message/Uri.php @@ -46,7 +46,7 @@ final class Uri implements UriInterface public function __construct($uri) { $parts = \parse_url($uri); - if ($parts === false || (isset($parts['scheme']) && !\preg_match('#^[a-z]+$#i', $parts['scheme'])) || (isset($parts['host']) && \preg_match('#[\s_%+]#', $parts['host']))) { + if ($parts === false || (isset($parts['scheme']) && !\preg_match('#^[a-z]+$#i', $parts['scheme'])) || (isset($parts['host']) && \preg_match('#[\s%+]#', $parts['host']))) { throw new \InvalidArgumentException('Invalid URI given'); } @@ -164,7 +164,7 @@ public function withHost($host) return $this; } - if (\preg_match('#[\s_%+]#', $host) || ($host !== '' && \parse_url('http://' . $host, \PHP_URL_HOST) !== $host)) { + if (\preg_match('#[\s%+]#', $host) || ($host !== '' && \parse_url('http://' . $host, \PHP_URL_HOST) !== $host)) { throw new \InvalidArgumentException('Invalid URI host given'); } diff --git a/tests/Message/UriTest.php b/tests/Message/UriTest.php index adaee94b..1774953f 100644 --- a/tests/Message/UriTest.php +++ b/tests/Message/UriTest.php @@ -120,6 +120,9 @@ public static function provideValidUris() yield [ 'http://user%20name:pass%20word@localhost/path%20name?query%20name#frag%20ment' ]; + yield [ + 'http://docker_container/' + ]; } /** @@ -329,6 +332,16 @@ public function testWithHostReturnsNewInstanceWhenHostIsChanged() $this->assertEquals('localhost', $uri->getHost()); } + public function testWithHostReturnsNewInstanceWhenHostIsChangedWithUnderscore() + { + $uri = new Uri('http://localhost'); + + $new = $uri->withHost('docker_container'); + $this->assertNotSame($uri, $new); + $this->assertEquals('docker_container', $new->getHost()); + $this->assertEquals('localhost', $uri->getHost()); + } + public function testWithHostReturnsNewInstanceWithHostToLowerCaseWhenHostIsChangedWithUpperCase() { $uri = new Uri('http://localhost');