Skip to content

Commit 559c30d

Browse files
lulhumclue
authored andcommitted
Allow underscore character in Uri host
1 parent fc09a64 commit 559c30d

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/Message/Uri.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ final class Uri implements UriInterface
4646
public function __construct($uri)
4747
{
4848
$parts = \parse_url($uri);
49-
if ($parts === false || (isset($parts['scheme']) && !\preg_match('#^[a-z]+$#i', $parts['scheme'])) || (isset($parts['host']) && \preg_match('#[\s_%+]#', $parts['host']))) {
49+
if ($parts === false || (isset($parts['scheme']) && !\preg_match('#^[a-z]+$#i', $parts['scheme'])) || (isset($parts['host']) && \preg_match('#[\s%+]#', $parts['host']))) {
5050
throw new \InvalidArgumentException('Invalid URI given');
5151
}
5252

@@ -164,7 +164,7 @@ public function withHost($host)
164164
return $this;
165165
}
166166

167-
if (\preg_match('#[\s_%+]#', $host) || ($host !== '' && \parse_url('http://' . $host, \PHP_URL_HOST) !== $host)) {
167+
if (\preg_match('#[\s%+]#', $host) || ($host !== '' && \parse_url('http://' . $host, \PHP_URL_HOST) !== $host)) {
168168
throw new \InvalidArgumentException('Invalid URI host given');
169169
}
170170

tests/Message/UriTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ public static function provideValidUris()
120120
yield [
121121
'http://user%20name:pass%20word@localhost/path%20name?query%20name#frag%20ment'
122122
];
123+
yield [
124+
'http://docker_container/'
125+
];
123126
}
124127

125128
/**
@@ -329,6 +332,16 @@ public function testWithHostReturnsNewInstanceWhenHostIsChanged()
329332
$this->assertEquals('localhost', $uri->getHost());
330333
}
331334

335+
public function testWithHostReturnsNewInstanceWhenHostIsChangedWithUnderscore()
336+
{
337+
$uri = new Uri('http://localhost');
338+
339+
$new = $uri->withHost('docker_container');
340+
$this->assertNotSame($uri, $new);
341+
$this->assertEquals('docker_container', $new->getHost());
342+
$this->assertEquals('localhost', $uri->getHost());
343+
}
344+
332345
public function testWithHostReturnsNewInstanceWithHostToLowerCaseWhenHostIsChangedWithUpperCase()
333346
{
334347
$uri = new Uri('http://localhost');

0 commit comments

Comments
 (0)